URL配置:
今天配置一下URL,打开urls.py配置如下:
from django.conf.urls import patterns, include, url
from djapp import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns(
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$',views.index),
url(r'^regist/$',views.regist),
url(r'^login/$',views.login),
url(r'^logout/$',views.logout),
url(r'^cancel/$',views.cancel),
url(r'^myorder/$',views.myorder),
url(r'^viewroom/$',views.viewroom),
url(r'^detail/$',views.detail),
url(r'^order/$',views.order),
)
这么多URL看起来有点眼花,但是这些格式基本都是一样的。。。里面有用到正则表达式。
from django.contrib import admin #django自带的认证模块,可以说是django的一个优势
admin.autodiscover()
这里的URL有两种写法:
1、url(r'^admin/doc/', include('django.contrib.admindocs.urls')), #用include直接写
r:告诉Python“/”是原始字符,不需要处理,“^”是正则表达式,表示以admin/doc开始的字符串
2、from djapp import views #从djapp导入views,URL可以直接调用views里的函数
url(r'^$',views.index), #主页是调用views中index函数,下面几个URL都和它相同。“$”表示匹配以$前边字符串结尾。
URL扩展:
#表示向后台传递两个整形参数,(d+)正则表达式表示任意的整数
url(r'^arg_test/(d+)/(d+)/',views.test)
#后台接收时会把两个数分别存在num1和num2中
def test(req,num1,num2):
#固定形参
url(r'^arg_test/(?p<num>)d+/',views.test)
#后台参数必须是num,不然会报错
def test(req,num):
#默认参数
url(r'^arg_test/(?p<num>)d+/',views.test,{'num':222})
当输入url时,没有给参数,num就默认为222