python---django中url路由分发

时间:2022-12-13 20:11:09

在urls.py文件中包含使用方法:

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    # Examples:
    # url(r'^blog/', include('blog.urls')),  #以blog开头,则去APP:blog中urls.py文件下去找路由访问路径
    # url(r'^admin/', include(admin.site.urls)),
]

 使用方法:

目录:

HelloWorld
    HelloWorld
        settings.py
        urls.py
        wsgi.py
    ...
    blog
        views.py
        urls.py

HelloWorld:全局文件urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from blog import views

urlpatterns = [
    url(r'^blog/', include('blog.urls')),#将路由分发到APP中的urls文件中去
]

APP:blog下urls.py:

from django.conf.urls import url
from blog import views

urlpatterns = [
    url(r'news/story$',views.introduce),#与全局文件中的路由分发紧跟着,前面不需要其他操作,其他地方可以使用正则  调用introduce方法
]

views.py文件:

def introduce(req):
    return HttpResponse("<h1>ok</h1>")

访问路径:

http://127.0.0.1:8080/blog/news/story