软件:Pycharm2020
Python:python3.7.5
Django:django3.0.8
数据库:mysql5.7.30
github链接 https://github.com/yt-xy/Django-student
输出 Hello World!
views.py
from django.shortcuts import render
def index(request):words = 'World!'return render(request, 'index.html', context={'words': words})
templates/index.html
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><title>学员管理系统title>
head>
<body>Hello {{ words }}
body>
html>
urls.py
from django.conf.urls import url
from django.contrib import admin
from stu.views import index
urlpatterns &#61; [url(r&#39;^admin/&#39;, admin.site.urls),url(r&#39;^$&#39;, index, name&#61;&#39;index&#39;),
]
再次启动项目&#xff0c;进入 http://127.0.0.1:8000 即可看到 Hello World!
输出数据
views.py
from django.shortcuts import render
from .models import Student
def index(request):students &#61; Student.objects.all()return render(request, &#39;index.html&#39;, context&#61;{&#39;students&#39;: students})
index.html
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><title>学员管理系统title>
head>
<body><ul>{% for student in students %}<li>{{ student.name }} - {{ student.get_status_display }}li>{% endfor %}ul>
body>
html>
提交数据
forms.py
from django import forms
from .models import Student
class StudentForm(forms.Form):name &#61; forms.CharField(label&#61;&#39;姓名&#39;, max_length&#61;128)sex &#61; forms.ChoiceField(label&#61;&#39;性别&#39;, choices&#61;Student.SEX_ITEMS)profession &#61; forms.CharField(label&#61;&#39;职业&#39;, max_length&#61;128)email &#61; forms.EmailField(label&#61;&#39;邮箱&#39;, max_length&#61;128)qq &#61; forms.CharField(label&#61;&#39;QQ&#39;, max_length&#61;128)phone &#61; forms.CharField(label&#61;&#39;手机&#39;, max_length&#61;128)
可修改为&#xff1a;
class StudentForm(forms.ModelForm):def clean_qq(self):cleaned_data &#61; self.cleaned_data[&#39;qq&#39;]if not cleaned_data.isdigit():raise forms.ValidationError(&#39;必须是数字&#xff01;&#39;)return int(cleaned_data)class Meta:model &#61; Studentfields &#61; (&#39;name&#39;, &#39;sex&#39;, &#39;profession&#39;,&#39;email&#39;, &#39;qq&#39;, &#39;phone&#39;)
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .forms import StudentForm
from .models import Student
def index(request):students &#61; Student.objects.all()if request.method &#61;&#61; &#39;POST&#39;:form &#61; StudentForm(request.POST)if form.is_valid():cleaned_data &#61; form.cleaned_datastudent &#61; Student()student.name &#61; cleaned_data[&#39;name&#39;]student.sex &#61; cleaned_data[&#39;sex&#39;]student.email &#61; cleaned_data[&#39;email&#39;]student.profession &#61; cleaned_data[&#39;profession&#39;]student.qq &#61; cleaned_data[&#39;qq&#39;]student.phone &#61; cleaned_data[&#39;phone&#39;]student.save()return HttpResponseRedirect(reverse(&#39;index&#39;))else:form &#61; StudentForm()context &#61; {&#39;students&#39;: students,&#39;form&#39;: form,}return render(request, &#39;index.html&#39;, context&#61;context)
可修改为&#xff1a;
def index(request):students &#61; Student.objects.all()if request.method &#61;&#61; &#39;POST&#39;:form &#61; StudentForm(request.POST)if form.is_valid():form.save()return HttpResponseRedirect(reverse(&#39;index&#39;))else:form &#61; StudentForm()context &#61; {&#39;students&#39;: students,&#39;form&#39;: form,}return render(request, &#39;index.html&#39;, context&#61;context)
现在就可以添加数据并在前端页面显示了>.<