【问题标题】:Django Authentication: Getting a blank screenDjango 身份验证:得到一个空白屏幕
【发布时间】:2011-12-05 13:35:41
【问题描述】:

我正在构建我的第一个使用用户身份验证的 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'}),
)

--------- 模板/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 %}

【问题讨论】:

  • 我不明白您重现“空白屏幕”所采取的步骤。您访问的是哪个网址?你在点击什么?作为旁注:您有一些缩进错误,它们可能是问题所在:在views.mylogin 中,“if request.method == 'POST':”之后没有缩进。在这个视图的正下方有一些不属于任何视图的行 (?): "print "calling: direct_to_template('login.html')" return direct_to_template(request, 'login.html')"

标签: django templates authentication


【解决方案1】:

如果你的模板是这样的,那就错了

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

您的模板为空白似乎很合乎逻辑 -_-

更多.. 200 不是 HTTP 错误,它意味着 200 OK:成功 HTTP 请求的标准响应。

【讨论】:

  • 是的,他是对的。如果我在第 2 行下面放一些东西,它会打印在页面上。但是在没有进行身份验证的情况下,如何对用户进行身份验证呢?
  • 这可能是愚蠢而明显的,但你确定你没有在管理员中登录吗? -__-
【解决方案2】:

我已为您的问题添加了一条评论,以询问更多详细信息。但是如果没有这些细节,我的猜测是您需要一个视图来显示您的“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 请求来显示登录表单)。 但正如我所说,处理它的方法很少,我的只是一个建议,我不会参与你的任何设计决策:)

【讨论】:

  • 当我尝试你的代码时,屏幕仍然是空白的。这让我认为这与身份验证无关,而是模板问题。我也收到 200 错误。 __ "GET /login/ HTTP/1.1" 200 37__
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 2017-01-19
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
相关资源
最近更新 更多