Django 简单Web搭建(windows下)
Django安装
命令:pip install Django
1. django-admin startproject mysite (windows 命令提示符窗口运行)
2. cd mysite (windows 命令提示符窗口运行)
3. django-admin startapp student (windows 命令提示符窗口运行)
4. mysite\student\models.py下增加
class Userinfo(models.Model):
userid = models.AutoField(primary_key=True)
username = models.CharField(max_length=20)
password = models.CharField(max_length=20)
createCounttime=models.DateTimeField(auto_now_add=True)
class Studentinfor(models.Model):
stuid = models.ForeignKey('Userinfo',to_field='userid',on_delete='CASCADE')
name=models.CharField(max_length=20)
age=models.IntegerField(max_length=3)
gendle=models.BooleanField()
classes=models.CharField(max_length=50)
subject=models.CharField(max_length=50)
result=models.FloatField()
5. 在mysite\mysite\settings.py增加 ‘student’
INSTALLED_APPS = [
'student'
]
6.python manage.py makemigrations (windows 命令提示符窗口运行)
出现No changes detected,有两种情况
第一种:没完成第5步和第4步
第二种:没删除mysite\student\migrations\*_initial.py
7.python manage.py migrate (windows 命令提示符窗口运行)
如果无法迁移成功,则是mysite文件下的*.sqlit3没删除
8.在mysite\student\ 下创建Templates文件
9.在mysite\student\Templates\下创建userinfo.html并在其中增加如下html语句
<html>
<head>
<meta charset="utf-8" />
<title>add_user_info</title>
</head>
<body>
<p align="center">
<form action="/add_data/" method="post">
{% csrf_token %}
<p>username:<input type="text" name="username"></p>
<p>password:<input type="password" name="passwd"></p>
<p><input type="submit" value="adduser"></p>
</form>
</p>
{{ added }}
</body>
</html>
10.在mysite\student\Templates\下创建showall.html 并在其中增加如下html语句
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>datalist</title>
</head>
<body>
<table style="color: green" border="2">
<td>id</td>
<td>username</td>
<td>createtime</td>
{% for m in datas %}
<tr>
<td>{{ m.userid }}</td>
<td>{{ m.username }}</td>
<td>{{ m.createCounttime }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
11.在mysite\mysite\settings.py的’DIRS’中增加[os.path.join(BASE_DIR,'student/Templates')]
12.在mysite\student\views.py中增加如下代码
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.template.context import RequestContext
#包装csrf请求,避免django认为其实跨站攻击脚本
from django.views.decorators.csrf import csrf_exempt
from django.template.context_processors import csrf
from .models import Studentinfor
from .models import Userinfo
def myhtml(request):
ctx={}
return render(request, "userinfo.html",ctx)
def add_data(request):
username=request.POST['username']
pin = request.POST['passwd']
userinfo = Userinfo()
userinfo.username=username
userinfo.password=pin
userinfo.save()
ctx={}
ctx.update(csrf(request))
ctx['added']='添加数据成功!'
return render(request, "userinfo.html",ctx)
def query(request):
b=Userinfo.objects.all()
return render(request,'showall.html',{'datas':b})
13.在mysite\mysite\urls.py中修改如下代码
from django.urls import re_path
from student import views
urlpatterns = [
re_path(r'^myhtml/$', views.myhtml,name='myhtml'),
re_path(r'^add_data/$', views.add_data,name='adddata'),
re_path(r'^query/$', views.query,name='queryall'),
]
14.python manage.py runserver 0.0.0.0:8000 (windows 命令提示符窗口运行)
15.浏览器中输入 127.0.0.1:8000\myhtml (用于增加user) 或 127.0.0.1:8000\query (查询增加的用户)