在路由配置中 里边都是正则来表示
净弄花里胡哨的东西 会两种就好了 就是配置访问的路径和参数吧
当一个用户通过网页发送一个请求给Django网站,Django执行过程如下:
-
首先访问项目下的settings.py文件中 ROOT_URLCONF = \'test1.urls\'
-
执行项目包下的urls.py文件中的urlpatterns列表
-
执行应用包下的urls.py文件中的urlpatterns列表
-
根据匹配的url正则调用相应的视图函数/通用视图
-
如果没有匹配的正则,将会自动调用Django错误处理页面
url函数配置方式
-
方式1
#student/urls.py
#coding=utf-8
from django.conf.urls import url
from . import views
urlpatterns=[
url(r\'^query$\',views.queryAll)
]
#student/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def queryAll(request):
return HttpResponse(\'hello world\')
#访问地址
http://127.0.0.1:8000/student/query
-
方式2:位置传参
#student/urls.py
#coding=utf-8
from django.conf.urls import url
from . import views
urlpatterns=[
url(r\'^query/(\d{2})$\',views.queryAll),
]
#student/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def queryAll(request,sno):
print sno
return HttpResponse(\'hello world\')
#访问地址
http://127.0.0.1:8000/student/query/12
-
方式3:关键字传参
urlpatterns=[
url(r\'^query/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$\', views.queryAll),
]
def queryAll(request,year,day,month):
print year,month,day
return HttpResponse(\'hello world\')
#访问地址
http://127.0.0.1:8000/student/query/2008/10/12/
-
方式4:加载其他映射文件
from django.conf.urls import include, url
urlpatterns = [
url(r\'^community/\', include(\'aggregator.urls\')),
]
-
方式5:传参(参数名必须保持一致)
urlpatterns=[
url(r\'^query/(?P<num1>\d{3})/$\',views.queryAll,{\'hello\':\'123\'}),
]
def queryAll(request,num1,hello):
print num1,hello
return HttpResponse(\'hello world\')
#访问地址
http://127.0.0.1:8000/student/query/888/
#student/urls.py
from django.conf.urls import url
from . import views
urlpatterns=[
url(r\'^query1/([0-9]{4})/$\', views.queryAll, name=\'hello\'),
url(r\'^$\', views.index_view),
]
#student/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def queryAll(request,num1):
print num1
return HttpResponse(\'hello world\')
#通过模板页面逆向访问
def index_view(request):
return render(request,\'index.html\')
#通过Python代码逆向访问
def index_view(request):
# return render(request,\'index.html\')
return HttpResponseRedirect(reverse(\'hello\',args=(2018,)))
#templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="{% url \'hello\' 2008 %}">访问</a>
</body>
</html>
-
方式2
#项目包/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r\'^admin/\', admin.site.urls),
url(r\'^student/\', include(\'student.urls\',namespace=\'stu\',app_name=\'student\')),
]
#应用包/urls.py
#coding=utf-8
from django.conf.urls import url
from . import views
urlpatterns=[
url(r\'^$\', views.Index.as_view()),
url(r\'^query2/\',views.Login.as_view(),name=\'login\')
]
#应用包/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views import View
class Index(View):
def get(self,request):
return render(request,\'index.html\')
class Login(View):
def get(self,request):
return HttpResponse(\'hello\')
#templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="{% url \'stu:login\' %}">访问</a>
</body>
</html>