我正在构建我的第一个使用用户身份验证的django应用程序,我正在使用它 我在网上找到的一些例子可供参考。我的示例使用方法'direct_to_template' 问题是,当我使用它时,我得到一个空白的屏幕。我知道的 模板位于我的模板目录中。
为什么我在登录时出现空白屏幕?我该如何解决这个问题?
我正在使用的例子:
我的代码如下:
------------- ------------- base.html文件
Here is the trigger it's in the header bar.
<li><a href="/login">Log-In</a></li>
--------- views.py -----------------------
from django.template import Context, loader
from django.conf.urls import patterns, url, include
from django.views.generic.simple import direct_to_template
from django.http import HttpResponse
VER = "1a" # Global I like to print; making sure my latest code is running.
def mylogin(request):
print "mylogin called [%s] " % VER
if request.method == 'POST':
user = authenticate(username=request.POST['username'],
password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
# success
return HttpResponseRedirect('/')
else:
# disabled account
return direct_to_template(request, 'inactive_account.html')
else:
# invalid login
return direct_to_template(request, 'invalid_login.html')
# User just clicked login
# *** I know this is getting called and I get a blank screen here ***
print "calling: direct_to_template('login.html')"
return direct_to_template(request, 'login.html')
def mylogout(request):
print "mylogout called"
logout(request)
return direct_to_template(request, 'logged_out.html')
--------- urls.py -----------------------
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^customers/$', 'jim.views.index'),
(r'^customers/(?P<customer_id>\d+)/$', 'jim.views.detail'),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/media'}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/static'}),
(r'^login/$', 'jim.views.mylogin'),
(r'^logout/$', 'jim.views.mylogout'),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/media'}),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += patterns('django.views.generic.simple', (r'^accounts/login/$', 'direct_to_template', {'template': 'login_required.html'}),
)
--------- templates / login.html -----------------------
{% if user.is_authenticated %}
<!-- Authenticate account menu -->
{% else %}
<h3>Login</h3>
<form action="/login/" method="post" accept-charset="utf-8">
<label for="username">Username</label><input type="text" name="username" value="" id="username" />
<label for="password">Password</label><input type="password" name="password" value="" id="password" />
<p><input type="submit" value="Login"></p>
</form>
{% endif %}
答案 0 :(得分:1)
如果你的模板是这样的话,那就错误了
{% if user.is_authenticated %}
<!-- Authenticate account menu -->
{% else %}
stuff
{% endif %}
您的模板空白似乎很合乎逻辑 - _ -
更多.. 200不是HTTP错误,它意味着200 OK:成功HTTP请求的标准响应。
答案 1 :(得分:0)
我已经为您提出了一条评论,要求您提供更多详情。但是如果没有这些细节,我猜测你需要一个视图来显示你的“login.html”模板。
您可以为此编写一个单独的视图并将其放在urls.py中。您可以在urls.py中使用通用的direct_to_template视图。或者您可以修改当前的“mylogin”视图,例如:
def mylogin(request):
print "mylogin called [%s] " % VER
if request.method == 'POST':
user = authenticate(username=request.POST['username'],
password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
# success
return HttpResponseRedirect('/')
else:
# disabled account
return direct_to_template(request, 'inactive_account.html')
else:
# display login form
return direct_to_template(request, 'login.html')
区别在于压力和最后一行(没有POST数据,意味着它是显示登录表单的GET请求)。 但正如我所说,有几种方法可以处理它,我的只是一个建议,我不会做任何你的设计决定:))