0

I'm trying to get a simple input (which would be an integer) from the user, and use it as a variable in the next python function. But whenever I try to print the input it returns None. Here's my code:

HTML

  <form action="/./" method="POST">{% csrf_token %}
        <input type="text" name="quantity" placeholder="Repeat" size=2>
        <input type="submit" value="Save!">
      </form>

Python Django (This is just part of the function.)

def saveCriteria(request):
    title = request.POST.get('quantity')
    context = {}
    print(title)
    return render(request, "home.html", context)

Any help or advice would be greatly appreciated!

2
  • It prints an empty list "[]" Commented Apr 12, 2020 at 17:34
  • No JS involved. Pure html Commented Apr 12, 2020 at 18:12

2 Answers 2

2

You should add if statement before that:

def saveCriteria(request):
    context = {}
    if request.method == "POST":
        title = request.POST.get('quantity')
        print(title)
    return render(request, "home.html", context)
Sign up to request clarification or add additional context in comments.

Comments

0

You should refer to the view function in the html template:

 <form action="url of saveCriteria" method="POST"> 
        {% csrf_token %}
        <input type="text" name="quantity" placeholder="Repeat" size=2>
        <input type="submit" value="Save!">
 </form>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.