如何正确使用django内置的登录视图 [英] How to properly use the django built-in login view

查看:97
本文介绍了如何正确使用django内置的登录视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Django,而且我尽量使用内置的功能。因此,对于用户登录,我使用内置的登录视图,并将其分配给我的网站的基本URL:

I'm just getting started with Django, and I'm trying to use built-in features as much as possible. As such, for user login, I'm using the built-in login view, and assigning it to the base url of my site:

urlpatterns=patterns('django.contrib.auth.views',
    url(r'^/$','login',{'template':'mytemplate.html'}),

mytemplate.html看起来像这样:

mytemplate.html looks something like this:

<!DOCTYPE html>
<html>
<body>
    {%if form.errors %}
    <p> Invalid username/password combination, please try again </p>
    {% endif %}

    <h1>Welcome to My Site!</h1>
    <form action="{% url django.contrib.auth.views.login %}" method="post">
    {% csrf_token %}
        {{form.username.label_tag}}{{form.username}}
        {{form.password.label_tag}}{{form.password}}
        <input type="submit" id="submit" name="submit" value="Sign in" />
        <input type="hidden" name="next" value="{{ next }}" />        
    </form>
    <a href="password_reset/" id="forgot"> forgot username/password</a><br />
    <a href="register" id="new">new user</a>
</body>
</html>

我的问题是,模板似乎没有被传递任何上下文, 。在渲染的HTML中,我所有的变量标签都会消失(即不是被适当的值替代,而是被任何东西所取代)。

my problem is, the template doesn't appear to be getting passed any of the context it's supposed to. In the rendered HTML, all of my variable tags simply disappear (i.e. rather than being replaced by the appropriate values, thay are replaced with nothing).

我想我正在跳过一些关键的一步,但我不知道是什么。有任何想法吗?

I imagine I'm skipping some critical step, but I can't figure out what it is. Any ideas?

推荐答案

您需要从模板更改为template_name

You need to change from 'template' to 'template_name'

urlpatterns=patterns('django.contrib.auth.views',
    url(r'^/$','login',{'template_name':'mytemplate.html'}),

https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.views .login

这篇关于如何正确使用django内置的登录视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆