-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.py
More file actions
97 lines (75 loc) · 2.82 KB
/
view.py
File metadata and controls
97 lines (75 loc) · 2.82 KB
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
# -*- coding: utf-8 -*-
#from django.http import HttpResponse
from datetime import datetime
from django.shortcuts import render
from .models import Test,article
#import pdf lib
from reportlab.pdfgen import canvas
from django.http import HttpResponse
from django.shortcuts import redirect
from django.http import HttpResponseRedirect
from .forms import NameForm
from .models import ArticleForm
def index(request):
# 重定向到另一个url
return redirect('/hello/list/')
# return HttpResponse('首页!')
def hello(request):
context = {}
context['hello'] = 'Hello,World !'
context['name'] = 'lmm'
context['athlete_list'] = ['one','two','three']
# sql_data = Test.objects.filter(name='runoob')
all_sql_data = Test.objects.all()
context['sql_data'] = all_sql_data
#按照create_data降序排序,'-'表示降序,不加表示正序
# context['article_list'] = article.objects.all().order_by('-create_date')
#获取5条记录
context['article_list'] = article.objects.all()[:5]
# album_data = Test.objects.filter(title = '4455')
# context['album_data'] = album_data
context['form'] = ArticleForm()
return render(request,'hello.html',context)
# return HttpResponse('Hello World how are you!')
# 原始的form表单提交数据的方式
# def add_article_old(request):
# if request.method == 'POST':
# title = request.POST.get('title')
# text = request.POST.get('text')
# art = article(title = title,text = text)
# art.save()
# return HttpResponseRedirect('/hello/list/')
def add_article_new(request):
if request.method == 'POST':
form = ArticleForm(request.POST)
if form.is_valid():
post = form.save(commit = False)
post.create_date = datetime.now()
post.publish_date = datetime.now()
post.save()
return HttpResponseRedirect('/hello/list/')
def detail(request,id):
context = {}
detail = article.objects.filter(id = id)
context['detail'] = detail
return render(request,'detail.html',context)
def out_pdf(request):
response = HttpResponse(content_type = 'application/pdf')
response['Content-Disposition'] = 'attachment;filename = "test.pdf"'
p = canvas.Canvas(response)
p.drawString(100, 100, "Hello world.")
p.showPage()
p.save()
return response
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
# name = request.POST['your_name']
T = Test(name = request.POST['your_name'])
T.save()
return redirect('/hello/list/')
# return HttpResponseRedirect('/hello/list/')
else:
form = NameForm()
return render(request,'form.html',{'form':form})