How to get the current url name using Django?

2019-01-13 00:12发布

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.

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-13 00:48

For those who namespace their url patterns, then you may be interested in the namespaced url name of the request. In this case, Django called it view_name instead.

request.resolver_match.view_name

# return: <namespace>:<url name>
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-13 00:48

This can be achieved via:

request.resolver_match.url_name

Django >1.8

查看更多
戒情不戒烟
4楼-- · 2019-01-13 00:54

As of Django 1.5, this can be accessed from the request object

    current_url = request.resolver_match.url_name

https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.resolver_match

查看更多
Animai°情兽
6楼-- · 2019-01-13 01:04

It's a little unclear from your question, but http://docs.djangoproject.com/en/dev/topics/http/urls/ will likely provide an explanation to what you're after.

Especially useful to note how Django processes requests:

When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute:

  1. Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has an attribute called urlconf (set by middleware request processing), its value will be used in place of the ROOT_URLCONF setting.
  2. Django loads that Python module and looks for the variable urlpatterns. This should be a Python list, in the format returned by the function django.conf.urls.defaults.patterns().
  3. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
  4. Once one of the regexes matches, Django imports and calls the given view, which is a simple Python function. The view gets passed an HttpRequest as its first argument and any values captured in the regex as remaining arguments.

If you're just after the full path, you can try:

http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_full_path

I hope that helps - it indicates how to use the URLconf module, and hopefully will help point you in the right direction.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-13 01:09

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
查看更多
登录 后发表回答