如何使用Django获取当前网址名称? [英] How to get the current url name using Django?

查看:139
本文介绍了如何使用Django获取当前网址名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须根据当前URL动态构建一个URL。使用 {%url%} 标记是最简单的方法,但是我需要当前的URL名称才能动态生成新的URL名称。



如何获取链接到指向当前视图的urlconf的URL名称?



EDIT:我知道我可以使用 get_absolute_url 手工制作url,但是我宁愿避免使用它,因为这是讲座的一部分,我只想演示一种构建url的方法。 / p>

学生知道如何使用 {%url%} 。他们知道当他们必须根据当前URL生成更完整的URL时遇到问题。最简单的方法是再次使用 {%url%} ,但会有一些变化。由于我们已命名url,因此我们需要知道如何获取调用当前视图的url的名称。



编辑2:另一个用例是根据基本模板不同地显示基本模板的各个部分。还有其他方法(使用CSS和{%block%},但有时如果视图名称与链接匹配,能够删除base.html菜单项的标签就很好了。

解决方案

我不知道此功能作为Django的一部分已有多长时间了,但作为以下文章显示,可以通过以下视图实现:

 从django.core.urlresolvers导入解析
current_url = resolve(request.path_info).url_name

如果每个模板都需要,则编写模板请求可能是适当的。



编辑:应用新的DJANGO更新



在当前Django更新之后:



Django 1.10 链接


django.core.urlresolvers 模块导入后,由于
的新位置而被弃用, django.urls


Django 2.0 (< a href = https://docs.djangoproject.com/zh-CN/2.0/releases/2.0/#features-removed-in-2-0 rel = noreferrer>链接)


django.core.urlresolvers 模块已被删除,以支持其新的
位置, django.urls


因此,正确的做法是这样的:

 从django.urls导入resolve 
current_url = resolve(request.path_info).url_name


I have to build an url dynamically according to the current url. Using the {% url %} tag is the easiest way to do it, but I need the current url name to generate the new one dynamically.

How can I get the url name attached to the urlconf that leads to the current view?

EDIT : I know I can manually handcraft the url using get_absolute_url but I'd rather avoid it since it's part of a lecture and I would like to demonstrate only one way to build urls.

The students know how to use {% url %}. They are know facing a problem when they have to generate a more complete url based on the current one. The easiest way is to use {% url %} again, with some variations. Since we have named url, we need to know how to get the name of the url that called the current view.

EDIT 2: another use case is to display parts of the base template différently according to the base template. There are other ways to do it (using CSS and {% block %}, but sometime it just nice to be able to remove the tag of the menu entry of base.html if the viewname match the link.

解决方案

I don't know how long this feature has been part of Django but as the following article shows, it can be achieved as follows in the view:

   from django.core.urlresolvers import resolve
   current_url = resolve(request.path_info).url_name

If you need that in every template, writing a template request can be appropriate.

Edit: APPLYING NEW DJANGO UPDATE

Following the current Django update:

Django 1.10 (link)

Importing from the django.core.urlresolvers module is deprecated in favor of its new location, django.urls

Django 2.0 (link)

The django.core.urlresolvers module is removed in favor of its new location, django.urls.

Thus, the right way to do is like this:

from django.urls import resolve
current_url = resolve(request.path_info).url_name

这篇关于如何使用Django获取当前网址名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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