Python Django路径配置实现过程解析

假设访问的views.py如下

1.使用url配置默认页

from django.conf.urls import url

from django.contrib import admin

from django.urls import path

from HelloWorld import views

urlpatterns = [

path('admin/', admin.site.urls),

url('^$', views.hello),

]

这样可以访问http://127.0.0.1:8090/

2.使用path配置默认页

from django.contrib import admin

from django.urls import path

from HelloWorld import views

urlpatterns = [

path('admin/', admin.site.urls),

# url('^$', views.hello),

path('', views.hello),

]

这样也可以访问http://127.0.0.1:8090/

3.使用path配置hello页

from django.contrib import admin

from django.urls import path

from HelloWorld import views

urlpatterns = [

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 url

from django.contrib import admin

from django.urls import path

from HelloWorld import views

urlpatterns = [

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 admin

from django.urls import path, include

from CRUD import views

urlpatterns = [

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')),

]

以上是 Python Django路径配置实现过程解析 的全部内容, 来源链接: utcz.com/z/341597.html

回到顶部