Python Django路径配置实现过程解析
假设访问的views.py如下
1.使用url配置默认页
from django.conf.urls import urlfrom django.contrib import adminfrom django.urls import pathfrom HelloWorld import viewsurlpatterns = [ path(’admin/’, admin.site.urls), url(’^$’, views.hello),]
这样可以访问http://127.0.0.1:8090/
2.使用path配置默认页
from django.contrib import adminfrom django.urls import pathfrom HelloWorld import viewsurlpatterns = [ path(’admin/’, admin.site.urls), # url(’^$’, views.hello), path(’’, views.hello),]
这样也可以访问http://127.0.0.1:8090/
3.使用path配置hello页
from django.contrib import adminfrom django.urls import pathfrom HelloWorld import viewsurlpatterns = [ path(’admin/’, admin.site.urls), # url(’^$’, views.hello), # path(’’, views.hello), path(’hello/’, views.hello),]
这样可以访问http://127.0.0.1:8090/hello/
4.使用url和正则
from django.conf.urls import urlfrom django.contrib import adminfrom django.urls import pathfrom HelloWorld import viewsurlpatterns = [ path(’admin/’, admin.site.urls), # url(’^$’, views.hello), # path(’’, views.hello), # path(’hello/’, views.hello), url(’^hello’, views.hello),]
可以访问http://127.0.0.1:8090/hello+任意字符
5.如果有多个app,还可以使用include
from django.contrib import adminfrom django.urls import path, includefrom CRUD import viewsurlpatterns = [ path(’admin/’, admin.site.urls), path(’’, views.login_html), path(’login.html/’, views.login_html), path(’index.html/’, views.index_html), path(’debug.html/’, views.debug_html), path(’debug/’, views.debug), path(’login/’, views.login), path(’one/’, include(’Model.urls’)), path(’two/’, include(’Model2.urls’)), path(’three/’, include(’Model3.urls’)), path(’four/’, include(’Model4.urls’)),]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章: