from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index_page),
path('page/1',views.page_1_views),
path('page/2',views.page_2_views)
]
from django.http import HttpResponse
def index_page(resquest):
h = "这是我的首页
"
return HttpResponse(h)
def page_1_views(resquest):
h = "这是编号1的网页
"
return HttpResponse(h)
def page_2_views(resquest):
h = "这是编号2的网页
"
return HttpResponse(h)
path('page/' ,views.xxx)
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('page/' ,views.page_views)
]
from django.http import HttpResponse
def page_views(resquest,pg):
h = "这是编号%s的网页!
"%(pg)
return HttpResponse(h)
转换器类型 | 作用 | 示例 |
---|---|---|
str | 匹配除了“/”之外的非空字符串 | “v1/users/< str:username >” 匹配 /v1/users/ane |
int | 匹配0或任何正整数,返回一个int | “page/< int:pg >” 匹配 /page/10 |
slug | 匹配任意由ASCII字母或数字以及连字符和下划线组成的短标签 | “detail/< slug:sl >” 匹配 detail/this-is-django |
path | 匹配非空字段,包括路径分隔符“/” | “v1/users/< path:ph >” 匹配 /v1/users/a/b/c |
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('//' ,views.cal_view)
]
views.py
from django.http import HttpResponse
def cal_view(request,n,op,m):
if op not in("add","sub","mul"):
return HttpResponse("Your op is wrong")
result = 0
if op == "add":
result = n + m
elif op == "sub":
result = n - m
elif op == "mul":
result = n * m
return HttpResponse('结果为:%s'%(result))
re_path(r'^(?P\d{1,2})/(?P\w+)/(?P\d{1,2})$' ,views.cal_view)
urls.py
from django.contrib import admin
from django.urls import path, re_path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^birthday/((?P\d{1,2})/(?P\d{1,2})/(?P\d{4}))$' ,views.birthday_view),
re_path(r'^birthday/(?P\d{4})/((?P\d{1,2})/(?P\d{1,2}))$' ,views.birthday_view)
]
views.py
from django.http import HttpResponse
def birthday_view(request,year,month,day):
bd = "生日为:%s年%s月%s日"%(year,month,day)
return HttpResponse(bd)