-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdjango_guide_db.txt
119 lines (109 loc) · 4.54 KB
/
django_guide_db.txt
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
116
117
118
Add record into DB you must do the following step
1. define table and columm of table in the app - models.py
2. define form that contains all the columm in models - form_add.py
3. define a html file to render for the form - add_subscriber.html
4. define views for the form that creat in step 2 - views.py
5. example:
5.1 vim models.py
from django.db import models
# Create your models here.
class fsUser(models.Model):
Username = models.CharField(max_length=20)
Password = models.CharField(max_length=20)
Toll_allow = models.CharField(max_length=20)
User_context = models.CharField(max_length=20)
5.2 vim form_add.py
from django import forms
from models import fsUser
class fsUserForm(forms.ModelForm):
class Meta:
model = fsUser
fields = ('Username', 'Password', 'Toll_allow', 'User_context')
5.3 vim add_subscriber.html
{% extends "base.html" %}
{% block sidebar %}
<ul>
<li><a href="/subscriber">Cancel</a></li>
</ul>
{% endblock %}
{% block content %}
<form action="/subscriber/add/" method="post">{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type="submit" name="submit" value="Add">
</form>
{% endblock %}
5.4 vim views.py
def add(request):
if request.POST:
form = fsUserForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/subscriber')
else:
form = fsUserForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('add_subscriber.html', args)
Update record on database you must do the following step
1. define a html that contains the id of record - form_update_subscriber.html
2. define a views with id of record that render a html file in step 1 -
views.edit function
3. define a views that handle the http.post to change record - views.update
function
4. example:
4.1 vim form_update_subscriber.html
{% extends "base.html" %}
{% block sidebar %}
<ul>
<li><a href="/subscriber">Cancel</a></li>
</ul>
{% endblock %}
{% block content %}
<form action="/subscriber/update/" method="post">{% csrf_token %}
<ul>
<input id="id_User_id" name="User_id" type="hidden" value="{{ id }}"/>
{{ form.as_ul }}
</ul>
<input type="submit" name="submit" value="Update">
</form>
{% endblock %}
4.2 vim views
def edit(request, user_id):
fsUser_id = fsUser.objects.get(id=user_id)
form = fsUserForm(instance=fsUser_id)
args = {}
args.update(csrf(request))
args['form'] = form
args['id'] = user_id
return render_to_response('form_update_subscriber.html', args)
4.3 vim views
def update(request):
if request.POST:
form = fsUserForm(request.POST)
user_id = request.POST['User_id']
fsUser_id = fsUser.objects.get(id=user_id)
if form.is_valid():
fsUser_id.Username = form['Username'].value()
fsUser_id.Password = form['Password'].value()
fsUser_id.Toll_allow = form['Toll_allow'].value()
fsUser_id.User_context = form['User_context'].value()
fsUser_id.save()
return HttpResponseRedirect('/subscriber')
else:
fsUser_id = fsUser.objects.get(id=user_id)
form = fsUserForm(instance=fsUser_id)
args = {}
args.update(csrf(request))
args['form'] = form
args['id'] = user_id
return render_to_response('form_update_subscriber.html', args)
Delete record in database you must do the following step
1. define a view that handle a delete request - views.delete
2. example:
2.1 vim views
def delete(request, user_id):
fsUser.objects.filter(id=user_id).delete()
return HttpResponseRedirect('/subscriber')