Django 2.0 URL笔记

做项目时发现,Django2.0更新后,URL的写法有了很大的不同。

一、分发路由时

我的代码

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/', include('user.urls', namespace='user')),
]

报错:
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

发现好像是没有指定app_name参数,查看include函数

def include(arg, namespace=None):
    app_name = None
    if isinstance(arg, tuple):
        # Callable returning a namespace hint.
        try:
            urlconf_module, app_name = arg
        except ValueError:
            if namespace:
                raise ImproperlyConfigured(
                    'Cannot override the namespace for a dynamic module that '
                    'provides a namespace.'
                )
            raise ImproperlyConfigured(
                'Passing a %d-tuple to include() is not supported. Pass a '
                '2-tuple containing the list of patterns and app_name, and '
                'provide the namespace argument to include() instead.' % len(arg)
            )
    else:
        # No namespace hint - use manually provided namespace.
        urlconf_module = arg

    if isinstance(urlconf_module, str):
        urlconf_module = import_module(urlconf_module)
    patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
    app_name = getattr(urlconf_module, 'app_name', app_name)
    if namespace and not app_name:
        raise ImproperlyConfigured(
            'Specifying a namespace in include() without providing an app_name '
            'is not supported. Set the app_name attribute in the included '
            'module, or pass a 2-tuple containing the list of patterns and '
            'app_name instead.',
        )
    namespace = namespace or app_name
    # Make sure the patterns can be iterated through (without this, some
    # testcases will break).
    if isinstance(patterns, (list, tuple)):
        for url_pattern in patterns:
            pattern = getattr(url_pattern, 'pattern', None)
            if isinstance(pattern, LocalePrefixPattern):
                raise ImproperlyConfigured(
                    'Using i18n_patterns in an included URLconf is not allowed.'
                )
    return (urlconf_module, app_name, namespace)

正确写法

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('user/', include(('user.urls', 'user'), namespace='user')),
]

二、应用中,捕获url的值

官网例子

from django.urls import path

from . import views

urlpatterns = [
    path('articles/2003/', views.special_case_2003, name='special_case_2003'),
    path('articles/<int:year>/', views.year_archive, name='special_case_2003'),
    path('articles/<int:year>/<int:month>/', views.month_archive, name='month_archive'),
    path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail, name='article_detail'),
]
  • 要从URL捕获值,请使用尖括号
  • 捕获的值可以选择包括转换器类型。例如,使用<int:name>捕获整数参数。如果不指定类型默认为String
  • 没有必要添加 /,因为每个URL都有。例如,应该写articles,而不是/articles

解释

  • /articles/2005/03/将匹配列表中的第三个。 调用函数views.month_archive(request,year = 2005,month = 3)
  • /articles/2003/将匹配列表中的第一个,因为模式是按顺序测试的,第一个是第一个要通过的测试。  这里,Django会调用函数views.special_case_2003(request)
  • /articles/2003与这些模式中的任何一个都不匹配,因为每个模式都要求URL以斜杠结尾
  • /articles/2003/03/building-a-django-site/将匹配第四个。 调用函数views.article_detail(request,year = 2003,month = 3,slug =“building-a-django-site”)

其他转换器

  • str  - 匹配任何非空字符串,不包括路径分隔符'/'。 如果转换器未包含在表达式中,则这是默认值。
  • int  - 匹配零或任何正整数。 返回一个int。
  • slug  - 匹配由ASCII字母或数字组成的任何slug字符串,以及连字符和下划线字符。 例如,建立你的第一个django网站。
  • uuid  - 匹配格式化的UUID。 要防止多个URL映射到同一页面,必须包含短划线,并且字母必须为小写。 例如,075194d3-6885-417e-a8a8-6c931e272f00。 返回一个UUID实例。
  • path  - 匹配任何非空字符串,包括路径分隔符'/'。 这允许您匹配完整的URL路径,而不是像str一样匹配URL路径的一部分。

Django官方文档

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值