skip to Main Content

I can’t seem to recognize the problem in my django project – I have a simple form with 2 "input" tags and I can’t identify in my code which one is being pressed.

front end:

<form method="POST">
    {% csrf_token %}
    <input type="image" value="accept" class="v" src="{% static 'main/assets/icons/v.png' %}">
    <input type="image" value="decline" class="x" src="{% static 'main/assets/icons/x.png' %}">
</form>

back end:

if response.method == "POST":
    if response.POST.get("accept"):
        return render(response, "main/calender-page.html", {})
    if response.POST.get("decline"):
        return render(response, "main/account-page.html", {})

I tried to search for answers in stack overflow and I never managed to find one that works.
Thanks for the help!

3

Answers


  1. add name=’accept’ and try again:

    <form method="POST">
        {% csrf_token %}
        <input type="image" value="accept" class="v" src="{% static 'main/assets/icons/v.png' name ="accept" %}">
        <input type="image" value="decline" class="x" name="decline" src="{% static 'main/assets/icons/x.png' %}">
    </form>
    
    Login or Signup to reply.
  2. you need html tag attribute ‘name’ to identify POST data key.

    Login or Signup to reply.
  3. i think u need radio input.

    front end:

    <form method="POST">
        {% csrf_token %}
    <input type="radio" name="accept" value="1" >
        <img class="v" src="{% static 'main/assets/icons/v.png' %}"/>
    </input>
    <input type="radio" name="accept" value="2" >
        <img  class="x" src="{% static 'main/assets/icons/x.png' %}"/>
    </input>
    </form>
    
    

    back end:

    if response.method == "POST":
        if response.POST.get("accept") == "1" :
            return render(response, "main/calender-page.html", {})
    
        elif request.POST.get("accept") == "2":
                
            return render(response, "main/account-page.html", {})
        else:
            #other
            return render(request, "main/error-page.html", {})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search