2

I need one help . I need to get all html field value when user will click on different button in views.py file using Django and Python. I am explaining my code below.

<div class="boxwidthinnerdiv">
        Nuclear Mini Plant<br /><br />
        <select>
            <option>Reactor 1</option>
            <option>Reactor 2</option>
            <option>Reactor 3</option>
        </select>
        <br /><br /><br /><br />
        <a href="{% url 'start' %}"><button class="buttondiv" type="button">Start</button></a>
        <a href="{% url 'shut' %}"><button class="buttondiv">Shut Down</button></a>
        <a href="{% url 'suspend' %}"><button class="buttondiv">Suspend</button></a>
    </div>

Here when user will click on start button the select field value should get in views.py file which is given below.

def start(request):
    status = 1

def shut(request):
    status = 0

def suspend(request):
    status = 2

Here inside each action I need to get the select field selected value. Please help me.

4
  • Why do you not want to use a form? You can set the action url of the form dynamically based on a button click, if you want each submit to go to a different URL. Take a look at this so answer Commented Jun 29, 2017 at 9:19
  • Well, this can't possibly work. Why don't you want to use a form? Commented Jun 29, 2017 at 9:20
  • @DanielRoseman : Can you please share your idea by a post ? Commented Jun 29, 2017 at 9:21
  • @DanielRoseman : Here also I need to set status different based on button . Commented Jun 29, 2017 at 9:22

1 Answer 1

1

You don't need multiple views or links for this. just add a name for button (name="type" for example) and value for each of them like this:

    <button name="type" value="start" class="buttondiv" type="button">Start</button>
    <button name="type" value="shut-down" class="buttondiv">Shut Down</button>
    <button name="type" value="suspend" class="buttondiv">Suspend</button>

then in your view check which one is pressed by getting the type value from post request. example:

#views.py

def your_view(request):
    if request.method == "POST":
        if request.POST.get('type') and request.POST.get('type') == "start":

            #do something when "Start" button pressed.

        elif request.POST.get('type') and request.POST.get('type') == "shut-down":

            #do something when "Shut Down" button pressed.

        elif request.POST.get('type') and request.POST.get('dl-type') == "suspend":

            #do something when "Suspend" button pressed.
    else:
        #do something when request is not post.
        #redirect or render a template for example.

all of your inputs like select will be available like a normal form.

You can print the post request to see what is available to use and get more understanding on how this works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.