I am working in a Django project and a part of the functionality is user login, which is not working. The same piece of code was working perfectly 10 minutes ago.
A csrf_token was used in a template but the context did not provide the value. 模板中使用了csrf_token,但上下文未提供该值。
FYI, I have already tried using render()
and render_to_template()
but nothing works. The files
def login(request):
context=RequestContext(request)
if request.method=='POST':
username=request.POST.get('username')
password=request.POST.get('password')
user=auth.authenticate(username=username, password=password)
if user:
if user.is_active:
auth.login(request,user)
return render_to_response('ProjectLogging/main.html',{'user':user, 'project_list':Project.objects.all()}, context)
else:
return HttpResponse("Your account is disabled.")
else:
return HttpResponse("Invalid login credentials.")
return render_to_response('ProjectLogging/login.html')
urlpatterns=patterns('ProjectLogging',
url(r'^$', 'views.login', name="index"),
url(r'^login/$', 'views.login', name="login"),
url(r'^logout/$', 'views.logout', name="logout"),
url(r'^main/$','views.main', name="main"),
url(r'^(user/?P<username>\w+)$', 'views.main'),
)
{%extends "base.html"%}
{%block content%}
<h1>Sign In {%if user.is_authenticated%}{{user.username}} {%else%} user {%endif%}</h1>
{%if form.errors%}
{%for error in form.errors%}
<p class="error">error</p>
{%endfor%}
{%endif%}
<form action="/login/" method="post">
{% csrf_token %}
<p>
<label for="username">Username: </label>
<input type="text" name="username" value="" id="username">
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" value="" id="password">
</p>
<p><input type="submit" value="Log In"></p>
</form>
{%endblock%}
Thank you.
You should provide the RequestContext
to the render_to_response()
call or, as the better option, use the render()
shortcut:
from django.shortcuts import render
return render(request, 'ProjectLogging/login.html')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.