Django-视图

view

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# urls.py
from django.urls import path, include, register_converter #自定义转换器
from . import views
from feed import views as feed_views

urlpatterns = [
path('article/<int:year>/', views.year_archive, {'':''}, name=''), #< >,<slug:>,<uuid:>, <path:>
re_path(r'^blog/(page-(\d+)/)?$', blog_articles),
path('feed', include('feed.urls')),
]

# views.py
from django.http import HttpResponse, HttpResponseNotFound, Http404, HttpResponseBadRequest
from django.shortcuts import render, redirect, get_object_or_404, get_list_or_404
from django.views.decorators.http import require_http_methods #请求, 条件视图
from django.views.decorators.gzip import gzip_page
from django.views.decorators.vary import vary_on_cookie, vary_on_headers
from django.views.decorators.cache import cache_control, never_cache

@require_http_methods(['GET', 'POST'])
def my_view(request):
return render(request, 'temple.html', context, content_type, status, using) #模板引擎
return redirect('urls/view.name', permanent=True)

# HttpRequest
request.scheme #http,https
.body
.path
.path_info
.method
.encoding
.content_type
.GET #字典对象
.POST
.COOKIES
.FILES
.META #字典
.resolver_match

.current_app #应用程序设置的属性
.urlconf

.session #中间件设置的属性
.site #当前网站的实例
.user #登录的实例 if request.user.is_authenticated

.get_host() #方法
.get_port()
.get_full_path()
.build_absolute_uri()
.get_signed_cookie(key, salt='', max_age=60) #返回cookie值
.is_secure() #判断https
.is_ajax()
.read()
.__iter__()

# QueryDict
.get()
.update() #附加到,而不是替换
.items()
.values()
.copy()/deepcopy()
.getlist
.pop()

# HttpResponse
.content
.charset
.status_code
.reason_phrase #http原因短语
.streaming #总是false
.close

.has_header(head) #True
.setdefault(header, value)
.set_cookie(key, value, max_age=60, expires=None, path='/', httponly=True) #阻止JavaScript访问cookie
.set_signed_cookie(key, value, salt='') #可以加盐
.delete_cookie

# JsonResponse
from django.http import JsonResponse
response = JsonResponse({'': ''})
response = JsonResponse([], safe=False)
response.content

# FileResponse
from django.http import FileResponse
response = FileResponse(open('myfile.png', 'rb'))

# TemplateResponse
# HttpResponse是静态结构,TemplateResponse保留模板和上下文
from django.template.response import TemplateResponse
t = TemplateResponse(request, 'original.html', {})
t.render()
response.add_post_render_callback(my_render_callback) #回调

# 类视图,为了重用视图结构,减少代码量
from django.views.generic import TemplateView, ListView, DetailView
#在urls.py中,要用.as_view()
class PublisherList(ListView): #用于返回列表
model = Publisher/queryset = Publisher.objects.filter()
context_object_name = '' #上下文变量,也就是html中的变量
template_name = ''

class PublisterDetail(DetailView): #额外信息
model = Publisher

def get_context_data(self, **kw): #添加额外的上下文
context = super().get_context_data(**kw)
context['book_list'] = Book.object.all()
return context

def get_object(self): #执行额外的工作
object = super().get_object()
return object
----------本文完,感谢您的阅读----------