user1631075
user1631075

Reputation: 355

How do I get the name of a form after a post request in 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

Upvotes: 6

Views: 20052

Answers (2)

daniel tiesling
daniel tiesling

Reputation: 111

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.

Upvotes: 7

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

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 

Upvotes: 15

Related Questions