Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the name of a form after a post request in django?

Tags:

python

django

<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

like image 817
user1631075 Avatar asked Sep 25 '14 21:09

user1631075


People also ask

How do you receive data from a Django form with a post request?

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.

How does Django read POST data?

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.

What is request method == POST in Django?

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).


2 Answers

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 
like image 75
Hasan Ramezani Avatar answered Sep 24 '22 19:09

Hasan Ramezani


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.
like image 28
daniel tiesling Avatar answered Sep 22 '22 19:09

daniel tiesling