数据库MySQL
- 首先准备好数据库
新建一个连接test,用户名root,密码:。。。
然后启动MySQL 8.0 Command Line Client
mysql>select host,user,plugin,authentication_string from mysql.user;
查看一下
把密码更改一下,改成mysql_native_password的才能成功连接到数据库。
- 添加一个模式 student_management
新建项目和app
win+r进cmd 进相应目录下新建一个项目stu_management,新建应用stumanagement
文件夹:
- stu_management
- stu_management
* __init__.py
* __init__.pyc
* settings.py #Django的设置,配置文件,比如DEBUG开关,静态文件位置等
* settings.pyc
* urls.py #网址入口,关联到对应的views.py中的一个函数,访问网址就对应一个函数
* wsgi.py
- stumanagement
* migtations
- __init__.py
* templates #自己手动新建的,views.py中的函数渲染templates中的html模板,得到动态内容的网页
- index.html #新建文件,后面会提到
- error.html #新建文件,后面会提到
* __init__.py
* admin.py #后台,可以用很少的代码拥有一个强大的后台
* apps.py
* models.py #与数据库相关操作,存入或读取数据时用到
* tests.py
* views.py #处理用户发出的请求,从urls.py中对应过来,通过渲染templates中的网页可以将显示内容输出到网页,比如登陆后的用户名,用户请求的数据。
+ manage.py
添加app,修改默认数据库
修改 settings.py (为什么字体会变蓝色??怎么还有链接??)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'stumanagement', #new
]
【咳咳咳。。。突然想到遇到的一个格式错误问题哦。。用notepad++的话打开 视图–>显示符号–>显示空格与制表符 吧。。。如图找了一个世纪的错误。。。。肉眼对齐 sowhat???】
好 顺便修改下面的默认数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'student_management',
'USER': 'root',
'PASSWORD': 'newpassword',
'HOST': 'localhost',
'PORT':'3306',
}
}
请保存修改的文件
修改 models.py 设计数据库
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class student(models.Model):
stunum = models.IntegerField()
stuname = models.CharField(max_length=20)
stusex = models.CharField(max_length=20)
stuclass = models.CharField(max_length=20)
创建数据库模型
E:\django\stu_management>python manage.py makemigrations
E:\django\stu_management>python manage.py migrate
然后查看我们的数据库
设计页面
打开stumanagement ,新建文件夹templates,打开templates,新建html
一个管理界面index.html,一个错误跳转界面error.html
<!DOCTYPE html>
<html>
<body>
<div id= "container" style="width:1265px">
<div id="header" style="background-color:#FFA500;">
<br>
<h1 style="margin-bottom:0;">Student Management</h1> <br> </div>
<div id="content0" style="background-color:#FFD700;height:500px;width:100px;float:left;">
<b> Menu</b>
</div>
<div id="content0" style="background-color:#EEEEEE;height:500px;width:100px;float:left;">
</div>
<div id="content1" style="background-color:#EEEEEE;height:500px;width:400px;float:left;">
<br>
<p>请输入信息</p>
<form id="studentFrom" action="/add/">
stunum:<input type="text" name="stunum"><br><br>
stuname: <input type="text" name="stuname"> <br> <br>
sex: <input type="text" name="stusex"> <br> <br>
class:<input type="text" name="stuclass"> <br> <br>
<input type="submit" value="添加"/>
<input type="button" value="删除" onclick="process('del');">
<input type="button" value="修改" onclick="process('chan');">
<input type="button" value="查询" onclick="process('ser');">
</form>
<script type="text/javascript">
function process(v){
if(v=="del"){
document.forms.studentFrom.action="/delete/";
document.forms.studentFrom.submit();}
else{
if(v=="chan")
{document.forms.studentFrom.action="/chan/";
document.forms.studentFrom.submit();}
else
if(v=="ser"){
document.forms.studentFrom.action="/res/";
document.forms.studentFrom.submit();}
}
}
</script>
</div>
<div id="content" style="background-color:#EEEEEE;height:500px;width:465px;float:left;">
<br>
<fieldset>
<legend>查询结果</legend>
<br>
<table border='1'>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>班级</th>
</tr>
{% for item in li %}
<tr>
<td>{{item.stunum}}</td>
<td>{{item.stuname}}</td>
<td>{{item.stusex}}</td>
<td>{{item.stuclass}}</td>
</tr>
{% endfor %}
</table>
</div>
<div id="content0" style="background-color:#EEEEEE;height:500px;width:200px;float:left;">
</div>
<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
Copyright © lixiaoyu.com</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<br>
添加失败!
<a href="http://127.0.0.1:8000/" >返回界面</a>
</body>
</html>
修改views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from stumanagement.models import student
from django.shortcuts import HttpResponse
def index(request):
return render(request, 'index.html')
def error(request):
return render(request, 'error.html')
def add(request):
stu_num = request.GET['stunum']
stu_name = request.GET['stuname']
stu_sex = request.GET['stusex']
stu_class = request.GET['stuclass']
flag=student.objects.get_or_create(stunum=stu_num,stuname=stu_name,stusex=stu_sex,stuclass=stu_class)
if flag[1]==True:
return render(request, 'index.html')
else:
return render(request, 'error.html')
def delete(request):
stu_num = request.GET['stunum']
stu_name = request.GET['stuname']
stu_sex = request.GET['stusex']
stu_class = request.GET['stuclass']
p=student.objects.filter(stunum=stu_num).delete()
if p[1]==True:
return render(request, 'index.html')
else:
return render(request, 'error.html')
def chan(request):
stu_num = request.GET['stunum']
stu_name = request.GET['stuname']
stu_sex = request.GET['stusex']
stu_class = request.GET['stuclass']
student.objects.filter(stunum=stu_num).update(stuclass=stu_class)
return HttpResponse('chan ok')
def res(request):
stu_num = request.GET['stunum']
student_list_obj=student.objects.filter(stunum=stu_num)
return render(request,'index.html',{'li':student_list_obj})
修改urls.py
from django.conf.urls import url
from django.contrib import admin
from stumanagement import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='home'),
url(r'^$',views.error, name='error'),
url(r'^add/$', views.add, name='add'),
url(r'^delete/$', views.delete, name='delete'),
url(r'^chan/$', views.chan, name='chan'),
url(r'^res/$', views.res, name='res'),
]
运行
打开http://127.0.0.1:8000/
换成中文美观一点