URL name

Django URL name details

from django.conf.urls import url
from django.contrib import admin
from calc import views as calc_views
 
 
urlpatterns = [
    url(r'^add/$', calc_views.add, name='add'),
    url(r'^add/(\d+)/(\d+)/$', calc_views.add2, name='add2'),
    url(r'^admin/', admin.site.urls),
]

What is name='add'  used for?

Simply put, name  can be used to get the corresponding URL in  templates, models, views  ..... , which is equivalent to giving the URL a name. As long as the name remains the same, the URL can also be obtained by name.

In order to further clarify this problem, first create a new home page view and  url

Modify  calc/views

from django.http import HttpResponse
from django.shortcuts import render
 
 
def index(request):
    return render(request, 'home.html')

render  is the rendering template.

Add   the  calc app  to  settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'calc',
]

In this way, when using  render  , Django  will find   the files in the templates  under  each app  listed in  INSTALL_APPS .

Tip, when DEBUG=True  , django can also automatically find static files (js, css, pictures and other resources) in the static folder under an app.

在 calc 这个 app 中新建一个 templates 文件夹,在 templates 中新建一个 home.html

<!DOCTYPE html>
<html>
<head>
    <title>计算器</title>
</head>
<body>
 
<a href="/add/4/5/">计算 4+5</a>
 
</body>
</html>

修改 urls.py

urlpatterns = [
    url(r'^$',calc_views.index,name='home'), # new
    url(r'^add/$',calc_views.add,name='add'),
    url(r'^add/(\d+)/(\d+)/$',calc_views.add2,name='add'),
    url(r'^admin/', admin.site.urls),
]

我们计算加法时候用的是 /add/4/5/ ,后来需求发生变化,比如改成 4_add_5 ,但是在网页中,代码中很多地方都写死的 /add/4/5/,比如模板中可能是这么写的

<a href="add/4/5/">计算4+5</a>

如果这样写死,会使得在修改网址(正则)后,模板(templates),视图(views.py,用以跳转),模型(models.py,可以用于获取对象对应得地址)用了此网址的,都得进行相应的更改,修改的代价很大。

有没有更优雅的方式来解决这个问题呢

先说一下如何使用 python 代码获取对应得网址(可以用在 views.py,model.py 等各种需要转换得到网址的地方 )

首先

python manage.py shell
>>> from django.urls import reverse 
>>> reverse('add2',args=(21312,321321))
u'/add/21312/321321/'
>>> reverse('home')
u'/'

reverse 接收 url 中的 name 作为第一个参数,在代码中可以通过 reverse() 来获取对应得网址(这个网址可以用来跳转,也可以计算相应页面的地址),只要对应的 url 的 name 不改,就不用改戴拿中的网址。

在网页模板中也是一样

# 不带参数的
{% url 'name' %}
# 带参数的:参数可以是变量名
{% url 'name' 参数 %}

# 例如
<a href="{% url 'add2' 4 5 %}">link</a>

上面代码渲染成的最终页面是

<a href="add/4/5/">link</a>

这样就可以通过 {% url 'add2' 4 5 %} 获取到对应得网址 /add/4/5/

当 urls.py 进行更改,前提是不改 name (这个参数获取后不要轻易改),获取的网址也会动态的跟着变,比如改成:

url(r'^new_add/(\d+)/(\d+)\$',calc_views.add2,name='add2'),

注意看 add 变成了 new_add ,但是后面的 name = 'add2' 没改,这时 {% url 'add2' 4 5 %} 就会渲染对应的网址成 /new_add/4/5/

用在 views.py 或 model.py 等地方的 reverse 函数,同样会根据 name 对应的 url 获取到新的网址。

想要改网址的时候,修改 urls.py 中的正则表达式的部分(url 参数第一部分),name 不变的前提下,其他地方不需要修改。

另外,如果用户收藏夹中的 url 是旧的,如何让以前的 /add/3/4/ 跳转到现在的新网址呢

Django 不会帮你做这个,需要自己来写一个跳转方法:

具体思路是,在 views.py 写一个跳转的函数

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse  # django 1.4.x - django 1.10.x
#  from django.urls import reverse  # new in django 1.10.x
 
def old_add2_redirect(request, a, b):
    return HttpResponseRedirect(
        reverse('add2', args=(a, b))
    )

urls.py 中

    url(r'^add/(\d+)/(\d+)/$', calc_views.old_add2_redirect),
    url(r'^new_add/(\d+)/(\d+)/$', calc_views.add2, name='add2'),

这样,假如用户收藏夹中有 /add/4/5/ ,访问时就会自动跳转到新的 /new_add/4/5/ 了

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325738572&siteId=291194637