url分发
第一步 主文件夹urls
"""mysite01 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url,include from django.contrib import admin from blog import views urlpatterns = [ url(r'admin/', admin.site.urls),三角是上标,pycharm里没有 url(r'show_time/', views.show_time), url(r'blog/', include('blog.urls')),多了一个路由http;//..../blog/register
这样访问了 ]
2 blog应用里新建一个urls 分发
from django.conf.urls import url, include from blog import views urlpatterns = [ url(r'article/(\d{4})$', views.article_year), # $代表以某4位数结尾,如2001,只以他结尾 # 无命名分组形式访问 # \d一个占位符,占4个如2004, # r代表正则 url(r'article/(?P<year>\d{4})/(?P<month>\d{2})', views.article_year_month), # 命名分组,就是加上?P<>这种固定格式,且尖括号里内容为函数参数名 url(r'register', views.register, name="reg"), # 别名reg,以后用它就行 ]