-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
98 lines (85 loc) · 2.59 KB
/
views.py
File metadata and controls
98 lines (85 loc) · 2.59 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
98
from django.http.response import HttpResponse
from django.shortcuts import render, HttpResponse, redirect
import random
from django.views.decorators.csrf import csrf_exempt
nextid = 4
topics = [
{'id':1, 'title':'routing', 'body':'Routing is ..'},
{'id':2, 'title':'view', 'body':'view is ..'},
{'id':3, 'title':'Model', 'body':'Model is ..'}
]
def HTMLTemplate(articleTag, id=None):
global topics
contextUI = ''
if id != None :
contextUI = F'''
<li>
<form action="/delete" method="post">
<input type="hidden" name="id" value={id}>
<input type="submit" value="delete">
</form>
</li>
'''
ol = ''
for topic in topics:
ol += f'<li><a href="/read/{topic["id"]}">{topic["title"]}</a></li>'
return f'''
<html>
<body>
<h1><a href="/">Django</a></h1>
<ul>
{ol}
</ul>
{articleTag}
<ul>
<li><a href="/create">create</a></li>
{contextUI}
</ul>
</body>
</html>
'''
def index(req):
article = '''
<h2>Welcome</h2>
Hello, Django
'''
return HttpResponse(HTMLTemplate(article))
def read(req, id):
global topics
article = ''
for topic in topics:
print(type(topic['id']), type(id))
if topic['id'] == int(id):
article = f'<h2>{topic["title"]}</h2>{topic["body"]}'
return HttpResponse(HTMLTemplate(article, id))
@csrf_exempt
def create(req):
global nextid
if req.method == 'GET' :
article = '''
<form action="/create" method="post">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea name="body" placeholder="body"></textarea></p>
<p><input type="submit"></p>
</form>
'''
return HttpResponse(HTMLTemplate(article,))
elif req.method == 'POST' :
title = req.POST['title']
body = req.POST['body']
newTopic = {"id":nextid, "title":title, "body":body}
topics.append(newTopic)
url='/read/' + str(nextid)
nextid = nextid + 1
return redirect(url)
@csrf_exempt
def delete(req):
global topics
if req.method == 'POST' :
id = req.POST['id']
newTopics = []
for topic in topics:
if topic['id'] != int(id):
newTopics.append(topic)
topics = newTopics
return redirect('/')