When I click on submit button on login page, its redirecting to an random csrf token page in django

131 views
Skip to first unread message

Srinivas Gadi

unread,
Sep 24, 2018, 8:52:33 AM9/24/18
to django...@googlegroups.com

When I clock on the login page, it supposed to save the data in DB but instead URL being redirected to csrf token page.

url.py

from django.conf.urls import url
from . import views
from django.contrib.auth.views import login, logout

urlpatterns = [
url(r'^$', views.home),
url(r'home',views.home, name='home page'),
url(r'login',login,{'template_name':'section/login.html'}),
url(r'logout',logout,{'template_name':'section/logout.html'}),
url(r'register',views.register, name='register')

] views.py

def register(request):
if  request.method == 'POST':
    form = UserCreationForm(request.POST)
    if form.is_valid:
        form.save()
        print('data has been saved')
        return redirect('section/home')

else:
    form = UserCreationForm()
    args={'form':form}
    return render(request,'section/reg_form.html',args)

reg_form.html

{% extends "base.html" %}
{% block body %}
<br><br><br>
<form action="post">
    {%  csrf_token %}
    {{ form.as_p }}
<button type='Submit'>Login</button>
</form>
{% endblock  %}
Please advise

--
Srinivas .G
      9966559383

Nelson Varela

unread,
Sep 24, 2018, 9:35:11 AM9/24/18
to Django users
Do you have 'django.middleware.csrf.CsrfViewMiddleware' in your list of middlewares in your settings?

Srinivas Gadi

unread,
Sep 25, 2018, 4:11:40 AM9/25/18
to django...@googlegroups.com
It was clarified that I suppose to the method instead of action in HTML form.

<form action="post">

--
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...@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/6be4f994-abdf-4045-af80-da17eabdf886%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Srinivas .G
      9966559383
Reply all
Reply to author
Forward
0 new messages