Python全系列 教程
3567个小节阅读:5929.8k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
创建项目
xxxxxxxxxx
django-admin startproject demo
增加子应用
xxxxxxxxxx
python manage.py startapp user_app
添加子应用与ckeditor子应用
xxxxxxxxxx
# settings.py
INSTALLED_APPS = [
'user_app',
'...'
]
设置模板目录
xxxxxxxxxx
# settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
增加逻辑视图
xxxxxxxxxx
# user_app/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
# 根据请求方式,处理不同业务
# GET 返回注册页面
if request.method =='GET':
return render(request,'register.html')
# POST 注册
增加路由
xxxxxxxxxx
# user_app/urls.py
from django.urls import path
from .views import index
urlpatterns = [
path('register/', index,name='register'),
]
设置页面
xxxxxxxxxx
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>注册页面</h2>
<form action="register/" method="post">
{% csrf_token %}
<table>
<tr>
<td>邮箱</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="vcode"></td>
<td><input type="button" value="获取验证码"></td>
</tr>
<tr>
<td><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>
创建模型
xxxxxxxxxx
# user_app/models.py
from django.db import models
# Create your models here.
class User(models.Model):
email = models.CharField(max_length=64,verbose_name='用户名')
pwd = models.CharField(max_length=32,verbose_name='密码')
def __str__(self) -> str:
return self.email
class Meta:
db_table = 't_user'
迁移数据库
xxxxxxxxxx
python manage.py makemigrations user_app
python manage.py migrate