English 中文(简体)
Django Authentication: 选用空白屏
原标题:Django Authentication: Getting a blank screen

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.

我为什么在原木上接受一个空白的屏幕? 我如何确定这一点?

例子一米:

我的法典如下:

- 传真: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 %}
最佳回答

如果贵国的模板是这样的话,

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

看来很合乎逻辑的是,你的模板是空白的。 -

更进一步。 200起并不是吉大港山区开发方案的错误,它意味着200 OK:对吉大港山区成功申请的标准反应。

问题回答

我给你增加了一个意见,要求提供更多细节。 但如果没有这些细节,我的野心是,你需要看到你的“log”模板。

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





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签