How do I make my form send a POST request to a specified view?

35 views
Skip to first unread message

Tom Tanner

unread,
Nov 23, 2017, 5:12:06 PM11/23/17
to Django users
My page has a registration form at `"/login_register/"`. I want the form to send a POST request to `"/register/"`. Here's my code so far.

`urls.py`:
url("^login_register/$", views.login_register, name="login_register"),
url("^register/$", views.register, name="register"),


`views.py`:
def login_register(request, template="pages/login_register.html"):
 
'''
 Display registration and login forms
 '''

 registration_form
= RegisterForm()
 
return render(request, template, {"registration_form": registration_form})


def register(request):
 
'''
 Process user registration
 '''

 
if request.method=="POST":
   
# Some code here


`pages/login_register.html`:
 # For this example, I only include the registration form's HTML
 
<h2>Register</h2>
 <form method="post">
 {% csrf_token %}
 {{ registration_form.as_p }}
 <button type="submit">Register</
button>
 
</form>


When I hit "Register" on the form, my browser sends a POST request to "login_register/". I want it to send a POST request to "register/". How do I do this?

Dan Tagg

unread,
Nov 23, 2017, 5:18:15 PM11/23/17
to django...@googlegroups.com
You need an "action" attribute to your form



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72030063-8482-4723-9f2c-b25b17656a43%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: 28 Brock Street, Bath, United Kingdom, BA1 2LN, Company no: 05766374

Matemática A3K

unread,
Nov 23, 2017, 5:21:33 PM11/23/17
to django...@googlegroups.com
Because you are missing the form action, then the browser POSTs to the current url as default, see:
https://docs.djangoproject.com/en/1.11/intro/tutorial04/#write-a-simple-form
 
`pages/login_register.html`:
 # For this example, I only include the registration form's HTML
 
<h2>Register<
/h2>
 <form action="" method="post">

 {% csrf_token %}
 {{ registration_form.as_p }}
 <button type="submit">Register</
button>
 
</form>

mohammad k

unread,
Nov 23, 2017, 5:22:43 PM11/23/17
to django...@googlegroups.com
login_register.html :

 <form method="post" action="{% url 'register' %}">

 {% csrf_token %}
 {{ registration_form.as_p }}
 <button type="submit">Register</
button>
 </form>


--

Tom Tanner

unread,
Nov 23, 2017, 6:28:48 PM11/23/17
to Django users
Thanks, k2, this helped.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages