from django.conf.urls import url
from django.contrib import admin
from appname.views import viewfunctionName
urlpatterns=[
url(r'^admin',admin.site.urls),
url(r'^$','welcome',name="welcome")
]
*a
匹配""
、a
、aa
、aaa
、aaaa
等。例如:
^$
,welcome):匹配http://127.0.0.1:8000/
^index
,index):匹配http://127.0.0.1:8000/index1
、http://127.0.0.1:8000/index2
、http://127.0.0.1:8000/index3
以及http://127.0.0.1:8000/index
^index$
,welcome):严格匹配http://127.0.0.1:8000/index
我们经常需要在视图层和模版层中调用views函数以进行跳转。所以我们需要通过如下方式来达到这个目的:
{% url 'urlname' %}
)这种方式来调用。reverse('urlname')
这种方法映射到其他对应的views函数。案例1,在模版层调用views函数:
在urls.py中:
url(r'^$',welcome,name="welcomeurl")
url(r'^index',index,name="indexurl")
在welcome.html中若要调用index这个views函数
进入主页
案例2,在视图层调用其他views函数:
在urls.py中:
url(r'^list',list,name="listurl")
在views.py的其他函数中:
return HttpResponseRedirect(reverse('listurl'))
基于命名传递给views函数/类。
语法规则:?P
,即参数名+匹配模式(正则表达式)。
案例:
在urls.py中:
url(r'^news/(?P
在views.py中的InterDataView类中:
def get(self,request,news_id):
news=Newsdb.objects.get(id=int(news_id))
#根据新闻id获取新闻内容
在大型项目中,会存在有多个app和第三方插件的情况。这时候应该分别为不同的app设置单独的urls.py文件并在主urls.py中引用,这样更加方便管理。
#urls.py
urlpatterns = [
url(r`^admin/`,include(admin.site.urls)),
url(r'^app1/',include('app1.urls')),
url(r'^app2/',include('app2.urls')),
]
#settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2'
)
#settings.py
HERE = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join( HERE ,'media').replace('\\','/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(HERE,'static').replace('\\','/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# add other path no app static
os.path.join(HERE,'commonstatic/').replace('\\','/'),
)
#settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(os.path.join(BASE_DIR, 'commontemplates')),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
url(r'audio/(?P\w+)/$',vies.captcha_audio,name='captcha-audio')
return render(request,"welcome.html",{'nowtime':nowtime})
return HttpResponse('你当前通过get方法访问该页面
')
return HttpResponseRedirect(reverse("friendurl"))
,这里还用到了反向解析url映射。def friend(request):
all_friend = friendDB.objects.all()
if request.method == "POST":
if request.POST['checkfriendname']:
checkfriendname = request.POST.get('checkfriendname')
checkfriend = friendDB.objects.filter(friendName=checkfriendname)
return render(request, 'friend.html', {"thisfriend": checkfriend,"all_friend": all_friend})
else:
deltefriend = request.POST.get('deltefriend')
friendDB.objects.filter(friendName=deltefriend).delete()
return render(request, 'friend.html', {"all_friend": all_friend})
else:
return render(request, 'friend.html', {"all_friend": all_friend})
但实际上,若处理的页面中有多个可以产生POST请求的按钮,应该采用下面这种形式来处理。
View类继承自django自带的基类base.View,自带get和post方法,可以直接重构,也可以封装更多的方法。
class learnHttp(View):
def get(self,request):
# requestInf = {
# "path": request.path,
# "method": request.method,
# "GET": request.GET,
# "POST": request.POST,
# # "COOKIES": request.cookies,
# # "FILES": request.files,
# "META": request.META,
# "user": request.user,
# "session": request.session,}
# # "raw_post_data": request.raw_post_data}
return render(request,'learnDjango.html')
def post(self,request):
return render(request, 'learnDjango.html')