1、创建一个school的简单django项目以及相关配置
2、完成school项目的models模板与视图views的编写
1、在cmd管理员界面中进入到存放项目的文件夹创建项目school
2、在Navicat中创建数据库school
3、在pycharm中的setting中编写代码连接数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'school',
'USER': 'root',
'PASSWORD': '1234',
'HOST': '127.0.0.1',
}
}
4、然后在pycharm的terminal中创建应用app1
5、在项目app1的视图views中编写index方法,编写路由,然后用浏览器访问到视图index
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('hello world!!!')
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name='index')
]
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('app1/',include('app1.urls')),
path('admin/', admin.site.urls),]
可以看见在视图views中编写的index返回的内容
6、在应用app1中的app1/models中编写类,向数据库mysql中提交生成表
from django.db import models
class Teacher(models.Model):
tname = models.CharField(max_length=20)
tage = models.IntegerField()
tgender = models.CharField(max_length=2)
def __str__(self):
return self.tname
class Student(models.Model):
sname = models.CharField(max_length=20)
sage = models.IntegerField()
sgender = models.CharField(max_length=2)
s_t = models.ForeignKey(Teacher, on_delete=models.CASCADE)
def __str__(self):
return self.tname
1、创建templates
2、编写index首页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" bgcolor="#8fbc8f">
<tr><td>学生姓名</td><td>学生年龄</td><td>学生性别</td><td>操作</td></tr>
{
%for i in student%}
<tr><td>{
{
i.sname}}</td><td>{
{
i.sage}}</td><td>{
{
i.sgender}}</td><td><a href="#">修改</a>|<a href="#">删除</a></td></tr>
{
% endfor %}
</table>
<a href="{% url 'add'%}">添加</a>
</body>
</html>
2、编写add操作
在index页面的下面有个add操作,在templates中添加add.html文件,在子路由app1/urls.py中添加add路由,在视图views添加add方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加学生</title>
<style type="text/css">
* {
font-size: 12px;
margin: 0;
padding: 0;
}
input {
width: 320px;
height: 24px;
border: 1px solid #999;
border-radius: 4px;
}
.formErr {
border: 1px solid #ff0000;
}
.form .label {
display: block;
float: left;
width: 128px;
height: 40px;
line-height: 40px;
text-align: end;
}
.form .txt {
display: block;
float: left;
width: 340px;
height: 40px;
line-height: 40px;
padding-left: 16px;
}
.bt {
width: 56px;
height: 24px;
background-color: green;
border: 0;
border-radius: 4px;
color: white;
}
.form .errTips {
width: 226px;
background-color: lightgreen;
color: darkred;
border-radius: 4px;
margin-left: 144px;
margin-top: 6px;
margin-bottom: 4px;
padding: 16px 48px;
}
</style>
</head>
<body>
<div class="form">
<form action="" method="post">
{
% csrf_token %}
<div>
<span class="label">学生姓名</span>
<span class="txt"><input type="text" placeholder="6-20个字符" name='sname'/></span>
</div>
<div style="clear: both"></div>
<div>
<span class="label">学生性别</span>
<span class="txt">
<select name="sgender">
<option value="男">男</option>
<option value="女">女</option></select>
</span>
</div>
<div style="clear: both"></div>
<div>
<span class="label">教师选择</span>
<span class="txt">
<select name="tid">
{
% for t in teacher %}
<option value="{
{t.id}}">{
{
t.tname }}</option>
{
% endfor %}
</select>
</span>
</div>
<div style="clear: both"></div>
<div>
<span class="label">学生年龄</span>
<span class="txt"><input type="text" placeholder="请输入年龄" name="sage"/></span>
</div>
<div style="clear: both"></div>
{
% if a %}
<div class="errTips">
<ul>
<li>{
{
a }}</li>
</ul>
</div>
{
% endif %}
<div style="clear: both"></div>
<div>
<span class="label"></span>
<span class="txt"><input type="submit" class="bt" value="提交">||<a href="{% url 'index' %}" ><input type="button" class="bt" value="返回"></a></span>
</div>
<div style="clear: both"></div>
</form>
</div>
</body>
</html>
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name='index'),
path('add', add, name='add'),
]
def add(request):
a = ''
teacher = Teacher.objects.all()
if request.POST:
a = Student.objects.create( sname=request.POST['sname'], sgender=request.POST['sgender'], sage = request.POST['sage'], s_t_id=request.POST['tid'])
if a:
a='添加成功'
return render(request,'add.html',{
'teacher':teacher,'a':a})