How to get the URL to enter the template?

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

When I launch my site in debug mode, I see this in standard mode:

 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 | 

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

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

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

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))) ] 
+5
source share
3 answers

You should use the URL name, not its dotted path.

In this case, you have included url patterns from django.contrib.auth.urls . If you look at which the file refers to , you can see that the name is 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> 
+7
source

Take a look at urls.py

 url(r'^login/$', views.login, name='login'), 

you can refer to the name when using the URL

 {% url 'login' %} 

and

 {% url 'logout' %} 

or if you need to log out then

 <a href="{% url logout %}?next=/accounts/login/">Logout</a> 

Check this post 'django.contrib.auth.views.login'

+1
source

In urls.py add a name to each corresponding record (not those that contain other definitions, since the name will be ignored)

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

Then the url entry name is used in the templates as defined above ie

 <a href="{% url 'optional_template_namespace:entry_name' %}">The link text</a> 

In this case, the login and logout URLs come from the standard django.contrib.auth.urls , and their name is quite simple (see here for more information)

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

Source: https://habr.com/ru/post/1238832/


All Articles