在include方法里面指定namespace却不提供app_name是不允许的。
在包含的模块里设置app_name变量,或者在include方法里面提供app_name参数。
runserver发生错误
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.
解决方法
方法1:在 主文件夹/urls.py中修改
from django.urls import path,include
from app import urls as app_url
urlpatterns = [
path('', include((common_url,'common'), namespace='common')),
]
方法2:在 app文件夹/urls.py中修改
from django.urls import path
from .views import index
app_name='common'
urlpatterns = [
path('',index,name='index'),
]
正向解析与反向解析
urlinfo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="//www.greatytc.com/cmdb/urlinfo">cmdb/userinfo</a> <br>
<a href="//www.greatytc.com/cmdb/urlinfo/tom">cmdb/userinfo/tom</a><br>
<a href="//www.greatytc.com/cmdb/urlinfo/tom/10">cmdb/userinfo/tom/10</a><br>
<a href="//www.greatytc.com/cmdb/urlinfo/tom/tomnickname/10">cmdb/userinfo/tom/tomnickname/10</a><br>
<a href="//www.greatytc.com/cmdb/urlinfo/2019/02/12">//www.greatytc.com/cmdb/urlinfo/2019/02/12</a><br>
<a href="{% url 'cmdb:userinfo1' %}">url 'cmdb:userinfo1'</a>
<a href="{% url 'cmdb:userinfo2' 'tom' 10 %}">url 'cmdb:userinfo2' tom 10 </a><br>
<a href="{% url 'cmdb:userinfo3' name='tom' age=19 nickname='tomnickname' %}">cmdb/userinfo/tom/tomnickname/10</a><br>
<br><br>
通过路由获得用户信息<br>
info:{{ info }} <br>
name:{{ name }}<br>
nickname:{{ nickname }}<br>
age:{{ age }}
year:{{ year }}
month:{{ month }}
day:{{ day }}
</body>
</html>
其中解析部分如下
<-- 正向解析 -->
<a href="//www.greatytc.com/cmdb/urlinfo">cmdb/userinfo</a> <br>
<a href="//www.greatytc.com/cmdb/urlinfo/tom">cmdb/userinfo/tom</a><br>
<a href="//www.greatytc.com/cmdb/urlinfo/tom/10">cmdb/userinfo/tom/10</a><br>
<a href="//www.greatytc.com/cmdb/urlinfo/tom/tomnickname/10">cmdb/userinfo/tom/tomnickname/10</a><br>
<a href="//www.greatytc.com/cmdb/urlinfo/2019/02/12">//www.greatytc.com/cmdb/urlinfo/2019/02/12</a><br>
<-- 反向解析 -->
<a href="{% url 'cmdb:userinfo1' %}">url 'cmdb:userinfo1'</a>
<a href="{% url 'cmdb:userinfo2' 'tom' 10 %}">url 'cmdb:userinfo2' tom 10 </a><br>
<a href="{% url 'cmdb:userinfo3' name='tom' age=19 nickname='tomnickname' %}">cmdb/userinfo/tom/tomnickname/10</a><br>
正向解析url.py
django2 中使用
path来进行配置普通路径使用r
e_path来配置正则表达式路径
(?P<name>partten)来定义有名路径 其中name为名字 patten为正则表达式
#正向解析url
path("urlinfo", views.urlinfo1),
path("urlinfo/<str:name>", views.urlinfo2),
path("urlinfo/<str:name>/<int:age>", views.urlinfo3),
# path("urlinfo/<str:nickname>/<str:name>/<int:age>",views.urlinfo4), #位置匹配 re_path(r"^urlinfo/(?P<name>[A-Za-z]{0,})/(?P<nickname>[A-Za-z]{0,})/(?P<age>\d{0,})/$",views.urlinfo5),#名称匹配
re_path(r'^urlinfo/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})', views.urlinfo6),
反向解析url.py
在主目录下的urls中配置namespace,子app目录下urls中配置name
页面中通过 <% url namesapce:name %>来反向匹配
#反向解析url
path("urlinfo", views.urlinfo1,name="userinfo1"),
path("urlinfo/<str:name>/<int:age>", views.urlinfo3,name="userinfo2"),
re_path(r"^urlinfo/(?P<name>[A-Za-z]{0,})/(?P<nickname>[A-Za-z]{0,})/(?P<age>\d{0,})/$",views.urlinfo5,name="userinfo3"),
在重定向时使用反向解析
先导入模块 from django.urls import reverse
传参数是 位置参数名 args=(tuple) tuple
参数名参数 kwargs ={key:value .....} dict
app目录下views.py
def urlinfo7(request):
#无参数
url=reverse('cmdb:userinfo1')
#顺序参数
url=reverse('cmdb:userinfo2',args=(1,2))
#含参参数
url=reverse('cmdb:userinfo3',kwargs={"name":"tom","age":19,"nickname":"tomnickname"})
return redirect(url)