标签:
入门笔记翻译整理自:https://docs.djangoproject.com/en/1.8/topics/
*该笔记将对各个模块进行单独介绍
* HTTP Request Handlers
URL在名为URLconf的Python模块中创建,该模块提供了URL模式(正则表达式)和Python函数(视图)之间的映射。
URLconf举例如下:
1 from django.conf.urls import url 2 3 from . import views 4 5 urlpatterns = [ 6 url(r‘^articles/2003/$‘, views.special_case_2003), 7 url(r‘^articles/([0-9]{4})/$‘, views.year_archive), 8 url(r‘^articles/([0-9]{4})/([0-9]{2})/$‘, views.month_archive), 9 url(r‘^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$‘, views.article_detail), 10 ]
可以为对模式进行命名,以方便之后作为命名参数使用:
1 from django.conf.urls import url 2 3 from . import views 4 5 urlpatterns = [ 6 url(r‘^articles/2003/$‘, views.special_case_2003), 7 url(r‘^articles/(?P<year>[0-9]{4})/$‘, views.year_archive), 8 url(r‘^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$‘, views.month_archive), 9 url(r‘^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$‘, views.article_detail), 10 ]
可以使用include来包含其他URLconfs:
1 from django.conf.urls import include, url 2 3 urlpatterns = [ 4 # ... snip ... 5 url(r‘^community/‘, include(‘django_website.aggregator.urls‘)), 6 url(r‘^contact/‘, include(‘django_website.contact.urls‘)), 7 # ... snip ... 8 ]
可以使用include进行代码简化:
1 from django.conf.urls import include, url 2 from . import views 3 4 urlpatterns = [ 5 url(r‘^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/‘, include([ 6 url(r‘^history/$‘, views.history), 7 url(r‘^edit/$‘, views.edit), 8 url(r‘^discuss/$‘, views.discuss), 9 url(r‘^permissions/$‘, views.permissions), 10 ])), 11 ]
URLconf的参数具有继承关系:
1 # In settings/urls/main.py 2 from django.conf.urls import include, url 3 4 urlpatterns = [ 5 url(r‘^(?P<username>\w+)/blog/‘, include(‘foo.urls.blog‘)), 6 ] 7 8 # In foo/urls/blog.py 9 from django.conf.urls import url 10 from . import views 11 12 urlpatterns = [ 13 url(r‘^$‘, views.blog.index), 14 url(r‘^archive/$‘, views.blog.archive), 15 ]
可以在URLconf中设置嵌套参数,注意以下两者的不同:
1 from django.conf.urls import url 2 3 urlpatterns = [ 4 url(r‘blog/(page-(\d+)/)?$‘, blog_articles), # bad 5 url(r‘comments/(?:page-(?P<page_number>\d+)/)?$‘, comments), # good 6 ]
可以对URL传入参数:
1 from django.conf.urls import url 2 from . import views 3 4 urlpatterns = [ 5 url(r‘^blog/(?P<year>[0-9]{4})/$‘, views.year_archive, {‘foo‘: ‘bar‘}), 6 ]
也可以对URL进行空间命名(namespaced URLs),这里略过。
———————————————————————————————————————————————————————————————————————————————————————————
通过例子说明用法:
1 from django.http import HttpResponse 2 import datetime 3 4 def current_datetime(request): 5 now = datetime.datetime.now() 6 html = "<html><body>It is now %s.</body></html>" % now 7 return HttpResponse(html)
1 from django.http import HttpResponse, HttpResponseNotFound 2 3 def my_view(request): 4 # ... 5 if foo: 6 return HttpResponseNotFound(‘<h1>Page not found</h1>‘) 7 else: 8 return HttpResponse(‘<h1>Page was found</h1>‘)
可以很容易自定义错误视图,这里略过。
———————————————————————————————————————————————————————————————————————————————————————————
装饰器可以限制访问视图的方式:
1 from django.views.decorators.http import require_http_methods 2 3 @require_http_methods(["GET", "POST"]) 4 def my_view(request): 5 # I can assume now that only GET or POST requests make it this far 6 # ... 7 pass
除了@require_http_methods装饰器之外,还有其他较为复杂的装饰器,这里略过。
———————————————————————————————————————————————————————————————————————————————————————————
基本的上传方法:
1 from django.http import HttpResponseRedirect 2 from django.shortcuts import render_to_response 3 from .forms import UploadFileForm 4 5 # Imaginary function to handle an uploaded file. 6 from somewhere import handle_uploaded_file 7 8 def upload_file(request): 9 if request.method == ‘POST‘: 10 form = UploadFileForm(request.POST, request.FILES) 11 if form.is_valid(): 12 handle_uploaded_file(request.FILES[‘file‘]) 13 return HttpResponseRedirect(‘/success/url/‘) 14 else: 15 form = UploadFileForm() 16 return render_to_response(‘upload.html‘, {‘form‘: form})
处理模型中的上传文件(uploaded files):
1 from django.http import HttpResponseRedirect 2 from django.shortcuts import render 3 from .forms import ModelFormWithFileField 4 5 def upload_file(request): 6 if request.method == ‘POST‘: 7 form = ModelFormWithFileField(request.POST, request.FILES) 8 if form.is_valid(): 9 # file is saved 10 form.save() 11 return HttpResponseRedirect(‘/success/url/‘) 12 else: 13 form = ModelFormWithFileField() 14 return render(request, ‘upload.html‘, {‘form‘: form})
render()函数将给定模板和给定环境字典结合,返回被渲染文本的HttpResponse对象。
render_to_response()函数和render()函数功能类似。
redirect()函数向合适的URL返回HttpResponseRedirect对象。
get_object_or_404()函数在给定模型管理器上调用get()函数,但是它抛出Http404错误而不是模型DoesNotExist异常。
get_list_or_404()函数将给定模型管理器上filter()的结果返回给列表,如果结果为空,则抛出Http404错误。
略。
在设置中,向MIDDLWARE_CLASSES元组中添加中间件,中间件顺序很重要。
在请求阶段(request phase),Django会在调用视图之前依次应用中间件。
在响应阶段(response phase),Django会在调用视图之后逆序依次应用中间件。如下图:
这两个过程总共涉及5个方法:process_request, process_view; process_template_response, process_response, process_exception。
会话分为file based sessions和cache based sessions。此处略过。
-- the end --
标签:
原文地址:http://www.cnblogs.com/py-drama/p/4615732.html