如何在模板中正确获取登录视图的url? [英] How to properly get url for the login view in template?

查看:231
本文介绍了如何在模板中正确获取登录视图的url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,可以弄清楚如何在django模板中使用 {%url'something'%}

I have a small problem with figuring out how {% url 'something' %} works in django templates.

当我在调试模式下运行我的网站时,我在stdout看到这个:

When I run my website in debug mode, I see this in stdout:

web_1 | [21/Dec/2015 11:29:45] "GET /accounts/profile HTTP/1.1" 302 0
web_1 | /usr/local/lib/python3.5/site-packages/django/template/defaulttags.py:499: RemovedInDjango110Warning: Reversing by dotted path is deprecated (django.contrib.auth.views.login).
web_1 |   url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
web_1 | 

/ accounts / profile映射到一个模板,这个模板中唯一提及 django.contrib.auth.views.login 如下:

The /accounts/profile maps to a template, and the only place in this template that mentions django.contrib.auth.views.login is the following:

<a href="{% url 'django.contrib.auth.views.logout' %}?next={% url 'django.contrib.auth.views.login' %}">Log out</a>

所以,我想是因为某种原因,这不是正确的方式使用{%url%}命令。什么是正确的方法?如何摆脱这个警告?

So, I guess that for some reason this is not the proper way to use {% url %} command. What is the proper way? How to get rid of this warning?

这是我的urlpatterns:

Here are my urlpatterns:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^accounts/profile', views.profile_view),
    url(r'^$', RedirectView.as_view(url=reverse_lazy(views.profile_view)))
]


推荐答案

你应该使用 / em>,而不是其虚线路径。

You should use the name of the url, instead of its dotted path.

在这种情况下,您已经从 django.contrib.auth中添加了url模式.urls 。如果您查看该网址文件,可以看到视图的名称是登录注销

In this case, you have included the url patterns from django.contrib.auth.urls. If you look at that urls file, you can see that the name of the views are login and logout.

urlpatterns = [
    url(r'^login/$', views.login, name='login'),
    url(r'^logout/$', views.logout, name='logout'),
    ...
]

因此,请将您的链接更改为:

Therefore, change your link to:

<a href="{% url 'logout' %}?next={% url 'login' %}">Log out</a>

这篇关于如何在模板中正确获取登录视图的url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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