I am building my first django app that uses user authentication and I m using
some examples I found on the web for reference. My examples use a method direct_to_template .
The problem is that I get a blank screen when I use this. I know that the
template is in my templates directory.
我为什么在原木上接受一个空白的屏幕? 我如何确定这一点?
例子一米:
- Example #1: https://docs.djangoproject.com/en/dev/topics/auth/ Example #2: http://www.nerdydork.com/django-login-form-on-every-page.html
我的法典如下:
- 传真: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 )
- 导游 页: 1
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 }),
)
- 表格/格式 页: 1
{% 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 %}