开发者

Django Authentication: Getting a blank screen

开发者 https://www.devze.com 2023-04-12 13:02 出处:网络
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\'.

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.

Why am I getting a blank screen at login? How can I fix this?

The examples I'm using:

  • Example #1: https://docs.djangoproject.com/en/dev/topics/auth/ Example #2: http://www.nerdydork.com/django-login-form-on-every-page.html

My code is below:

-------------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(req开发者_开发问答uest, '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 %}


Errr if your template is such

{% if user.is_authenticated %}
    <!-- Authenticate account menu -->
{% else %}
    stuff
{% endif %}

it seems pretty logical that your template is blank -_-

further more.. 200 is not an HTTP error it means 200 OK: Standard response for successful HTTP requests.


I have added a comment for you question asking for more details. But without those details my wild guess is that you need a view to display your "login.html" template.

You can write a sepearate view for that and put it in the urls.py. You can use the generic direct_to_template view in urls.py. Or you can modify your current "mylogin" view, for example:

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')

The difference is in the indendation and in the last line (no POST data, means it's a GET request to display the login form). But as I said, there are w few ways to handle it, mine is only a suggestion and I'm not going into any of your design decisions :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号