<form method="post" name="message_frm">{% csrf_token %}
<input type="hidden" name="post_id" value="{{post.id}}">
{{message_frm.as_p}}
<input type="submit" value="Reply"/
I just wanted to know how I can verify that the form that was sent during a POST request was a form with the name of "message_frm"
Thanks
Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.
Django pass POST data to view The input fields defined inside the form pass the data to the redirected URL. You can define this URL in the urls.py file. In this URLs file, you can define the function created inside the view and access the POST data as explained in the above method. You just have to use the HTTPRequest.
method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).
you can set name
in name attribute of submit
button like this:
<input type="submit" value="Reply" name ="message_frm">
and in views.py
you can recongnize form
like this:
if 'message_frm' in request.POST:
#do somethings
I'm assuming you want to check this in the view. I always do something like this to determine which form was used.
<form method="post" name="message_frm">{% csrf_token %}
<-- Add this input to all forms -->
<input type="hidden" name="name" value="message_frm">
<input type="hidden" name="post_id" value="{{post.id}}">
{{message_frm.as_p}}
<input type="submit" value="Reply"/
def viewFunc(request):
if request.method == 'POST':
name = request.POST.get('name')
if name == 'message_frm':
# Do something here.
elif name == 'other_frm':
# Do something else here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With