Django_ 学员管理后台系统开发 (二)

  |   0 评论   |   0 浏览

  继上次的内容,我们继续 🚄

  1.前端页面

  view.py

 1
 2from django.shortcuts import render
 3
 4# Create your views here.
 5
 6def index(request):
 7
 8words = 'Hi,guys'
 9
10return render(request, 'index.html', context={'words':words})
11

  定义了函数 index,接受 request 参数(这是对用户发过来的 http 请求的封装)

  render 函数顾名思义,即将内容渲染到模版

  index.html 就是 templates 模版,Django 会在每个在 settings 里面注册的应用中寻找当前应用的模版,顺序是自上而下,所以这边我们自己手动建立一个 templates 文件夹,并在里面创建 index.html,内容如下:

  index.html

 1
 2<html>
 3
 4<head>
 5
 6<title>学员管理平台-by mufengs</title>
 7
 8</head>
 9
10<body>
11
122019 {{ words }}
13
14</body>
15
16</html>
17

  这边的{{ words }}就是取 view.py 那边 words 的值

  urls.py

  配置访问路由规则

 1
 2from django.conf.urls import url
 3
 4from django.contrib import admin
 5
 6from django.urls import path
 7
 8from student_sys.views import index
 9
10urlpatterns = [
11
12url(r'^$', index, name='index'),
13
14path('admin/', admin.site.urls),
15
16]
17

  访问:http://127.0.0.1:8000/

  就会出现下面的输出内容了
image.png

  2.操作数据库

  通过操作数据库,将内容渲染到页面上

  修改 view.py

 1
 2from django.shortcuts import render
 3
 4from .models import Student
 5
 6# Create your views here.
 7
 8def index(request):
 9
10students = Student.objects.all()
11
12return render(request, 'index.html', context={'students':students})
13

  从数据库中取出学员信息,接下来修改我们的模版

 1
 2<html>
 3
 4<head>
 5
 6<title>学员管理平台-by mufengs</title>
 7
 8</head>
 9
10<body>
11
12<ul>
13
14{% for student in students %}
15
16<li>{{ student.name }} - {{ student.get_status_display }}</li>
17
18{% endfor %}
19
20</ul>
21
22</body>
23
24</html>
25

  这边发现上次文章中的一个问题,注册 app 的时候写错了,所以这边一直报错过不去

1
2RuntimeError: Model class student_sys.models.Student doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
3

  因为表是空的,所以页面打开是空白的

  接下来我们来实现提交功能,创建一个 form.py 和 view.py 同级

 1
 2from django import forms
 3
 4from .models import Student
 5
 6# 方法一
 7
 8# class StudentForm(forms.Form):
 9
10# name = forms.CharField(label='name', max_length=128)
11
12# sex = forms.ChoiceField(label='sex', choices=Student.SEX_ITEMS)
13
14# profession = forms.CharField(label='profession', max_length=128)
15
16# email = forms.EmailField(label='email', max_length=128)
17
18# qq = forms.CharField(label='qq', max_length=128)
19
20# phone = forms.CharField(label='phone', max_length=128)
21
22#
23
24# 方法二
25
26class StudentForm(forms.ModelForm):
27
28class Meta:
29
30model = Student
31
32fields = (
33
34'name', 'sex', 'profession',
35
36'email', 'phone', 'qq'
37
38)
39

  这边用了两种方法来实现,很明显第二种方法更方便

  加入表单验证

  最后的视图部分代码如下

  views.py

 1
 2from django.http import HttpResponseRedirect
 3
 4from django.shortcuts import render
 5
 6from django.urls import reverse
 7
 8from student_sys.form import StudentForm
 9
10from .models import Student
11
12# Create your views here.
13
14def index(request):
15
16students = Student.get_all()
17
18if request.method == 'POST':
19
20form = StudentForm(request.POST)
21
22if form.is_valid():
23
24# cleaned_data = form.cleaned_data
25
26# student = Student()
27
28# student.name = cleaned_data['name']
29
30# student.sex = cleaned_data['sex']
31
32# student.email = cleaned_data['email']
33
34# student.profession = cleaned_data['profession']
35
36# student.qq = cleaned_data['qq']
37
38# student.phone = cleaned_data['phone']
39
40# student.save()
41
42form.save()
43
44return HttpResponseRedirect(reverse('index'))
45
46else:
47
48form = StudentForm()
49
50context = {
51
52'students': students,
53
54'form': form
55
56}
57
58return render(request, 'index.html', context=context)
59

  模版文件如下

 1
 2<html>
 3
 4<head>
 5
 6<title>学员管理平台-by mufengs</title>
 7
 8</head>
 9
10<body>
11
12<h3><a href="/admin/">Admin</a></h3>
13
14<ul>
15
16{% for student in students %}
17
18<li>{{ student.name }} - {{ student.get_status_display }}</li>
19
20{% endfor %}
21
22</ul>
23
24<form action="/" method="post">
25
26{% csrf_token %}
27
28{{ form }}
29
30<input type="submit" value="Submit" />
31
32</form>
33
34</body>
35
36</html>
37

  又到了最后的效果时间了,曾经有人和我说过,做开发最开心的时候,做出一个功能,是有一种别人理解不了的成就感

  当然我不是一个开发,我也没有所谓的成就感,只是兴趣使然,做自己喜欢的事情

  后台

  image.png
前台

  image.png

  今天就搞这些吧,还需要多理解多消化,盲目的跟着书本敲代码,也没多大用处

  @lizhongyue248 你那个问题就一个标签的事情,没有那么复杂啊,聊天发不了标签 @88250 ,所以这边顺便给你看下我的

 1<amp-auto-ads type="adsense"
 2              data-ad-client="xxx">
 3</amp-auto-ads>
 4<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
 5<ins class="adsbygoogle"
 6     style="display:block; text-align:center;"
 7     data-ad-layout="in-article"
 8     data-ad-format="fluid"
 9     data-ad-client="xxx"
10     data-ad-slot="xxx"></ins>
11<script>
12     (adsbygoogle = window.adsbygoogle || []).push({});
13</script>
---------------------------------------------------------------
>> 博客地址:https://blog.mufengs.com
>> 邮箱地址:[email protected]
>> 微信帐号:Do8080
>> Github : https://github.com/mufengcoding
---------------------------------------------------------------