《Django 项目之网络云笔记》
目录
网络云笔记项目
- 功能:
- 注册
- 登陆
- 退出登陆
- 查看笔记列表
- 创建新笔记
- 修改笔记
- 删除笔记
python3 manage.py startproject wiki
cd wiki
python3 manage.py startapp user
python3 manage.py startapp note
cd user && mkdir -p templates/user
vim wiki/setting.py
INSTALLED_APPS = [
....
'user', #添加应用:
'note', #添加note应用:
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'wiki',
"USER":'root',
'PASSWORD':'123qqq...A',
'HOST':'192.168.22.130',
"PORT":"3306"
}
}
...
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
SESSION_COOKIES_AGE = 3600 #cookies 3600秒过期
vim user/tmplates/user/register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>
<form action="/usr/reg" method="post">
<p>
用户名: <input type="text" name="username">
</p>
<p>
密码: <input type="text" name="password_1">
</p>
<p>
再次输入密码: <input type="text" name="password_2">
</p>
<p>
<input type="submit" value="注册">
</p>
</form>
</body>
</html>
vim user/templates/user/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="/user/login" method="post">
<p>
用户名: <input type="text" name="username">
</p>
<p>
密码: <input type="text" name="password">
</p>
<p>
记住用户名: <input type="checkbox" name="remember">
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</body>
</html>
vim note/templates/note/add_note.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加笔记</title>
</head>
<body>
<form action="/note/add" method="post">
<p>
标题: <input type="text" name="title">
<input type="submit" value="保存">
</p>
<p>
<textarea name="content" cols="50" rows="10"></textarea>
</p>
</form>
</body>
</html>
vim wiki/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('user/',include('user.urls')),
]
vim user/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('reg',views.reg_view),
path('login',views.login_view),
path('logout',views.logout_view),
]
vim note/urls.py
urlpatterns=[
path('add',views.add_view)
]
vim user/models.py
from django.db import models
# Create your models here.
class User(models.Model):
username=models.CharField("用户名",max_length=30,unique=True)
password=models.CharField("密码",max_length=32)
created_time=models.DateTimeField('创建时间',auto_now_add=True)
updated_time=models.DateTimeField('更新时间',auto_now=True)
def __str__(self):
return "用户" + self.username
vim note/models.py
from django.db import models
# Create your models here.
from user.models import User
class Note(models.Model):
title=models.CharField('标题',max_length=100)
content=models.TextField('内容')
created_time=models.DateTimeField('创建时间',auto_now_add=True)
updated_time=models.DateTimeField('更新时间',auto_now=True)
user=models.ForeignKey(User,on_delete=models.CASCADE)
vim user/views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import User
import hashlib
# Create your views here.
def reg_view(request):
if request.method =='GET':
return render(request,'user/register.html')
elif request.method =='POST':
username = request.POST['username']
password_1 = request.POST['password_1']
password_2 = request.POST.get('password_2')
if not username or not password_1:
return HttpResponse('数字不能为空')
if password_1 != password_2:
return HttpResponse('两次密码要一致')
old_user = User.objects.filter(username=username)
if old_user:
return HttpResponse('该用户已经被注册!')
md5 = hashlib.md5()
md5.update(password_1.encode())
password_h = md5.hexdigest()
try:
user = User.objects.create(username=username,password=password_h)
except Exception as e:
print('create error is %s' % e )
return HttpResponse('该用户名被注册!')
return HttpResponse("---注册成功---")
def login_view(request):
if request.method =='GET':
if 'username'in request.session and 'uid' in request.session:
return HttpResponse('---您已经登录---')
username = request.COOKIES.get('username')
uid = request.COOKIES.get('uid')
if username and uid:
request.session['username'] = username
request.session['uid'] = uid
return HttpResponse('---您已经登录---')
return render(request,'user/login.html')
elif request.method =='POST':
username = request.POST['username']
password = request.POST['password']
try:
old_user =User.objects.get(username=username)
except Exception as e:
print('login get error is %s' %e)
return HttpResponse('用户名或密码错误')
md5 = hashlib.md5()
md5.update(password.encode())
password_h = md5.hexdigest()
if password_h != old_user.password:
return HttpResponse('用户名或密码错误')
resp = HttpResponse('登录成功')
request.session['uid'] = old_user.id
request.session['username'] = old_user.username
if 'remember' in request.POST:
resp.set_cookie('uid',old_user.id,3600*24*7)
resp.set_cookie('username',old_user.username,3600*24*7)
return resp
def logout_view(request):
if 'username' in request.session:
del request.session['username']
if 'uid' in request.session:
del request.session['uid']
resp = HttpResponse('已退出')
if 'username' in request.COOKIES:
resp.delete_cookie('username')
if 'uid' in request.COOKIES:
resp.delete_cookie('uid')
return resp
vim note/views.py
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
from .models import Note
# Create your views here.
def login_check(fn):
def wrap(request,*args,**kwargs):
if 'username' not in request.session or 'uid' not in request.session:
c_username = request.COOKIES.get('username')
c_uid = request.COOKIES.get('uid')
if not c_username or not c_uid:
return HttpResponseRedirect('/user/login')
else:
request.session['username']=c_username
request.session['uid'] = c_uid
return fn(request,*args,**kwargs)
return wrap
@login_check
def add_view(request):
if request.method =='GET':
return render(request,'note/add_note.html')
elif request.method =='POST':
title = request.POST.get('title')
content= request.POST['content']
uid = request.session['uid']
Note.objects.create(title=title,content=content,user_id = uid)
return HttpResponse('添加笔记成功')
数据库设计
- 模型类
- 用户模型类
class User(models.Model):
username = models.CharField("用户名", max_length=30, unique=True)
password = models.CharField("密码", max_length=32)
created_time = models.DateTimeField('创建时间', auto_now_add=True)
updated_time = models.DateTimeField('更新时间', auto_now=True)
def __str__(self):
return "用户" + self.username
2. 笔记模型类
from user.models import User
class Note(models.Model):
title = models.CharField('标题', max_length=100)
content = models.TextField('内容')
created_time = models.DateTimeField('创建时间', auto_now_add=True)
updated_time = models.DateTimeField('更新时间', auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
设计规范
-
登陆设计规范(在user应用中写代码)
路由正则 视图函数 模板位置 说明 /user/login login_view templates/user/login.html 登陆 /user/reg reg_view templates/user/register.html 注册 /user/logout logout_view 无 退出 - 参考界面:
- 登陆界面
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HuyhOltW-1618039985950)(cloud_note_images/login.png)]
- 注册界面
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MtmL2bSd-1618039985955)(cloud_note_images/reg.png)]
- 登陆界面
- 参考界面:
-
主页设计规范(在index应用中写代码)
路由正则 视图函数 模板位置 说明 /index/ index_view templates/index/index.html 主页 - 参考界面
- 登陆前
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VM1NoqFf-1618039985957)(cloud_note_images/index1.png)]
- 登陆后
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cXgG8Y2T-1618039985960)(cloud_note_images/index2.png)]
- 登陆前
- 参考界面
-
云笔记设计规范
路由正则 视图函数 模板位置 说明 /note/ list_view templates/note/list_note.html 显示笔记列表功能 /note/add add_view templates/note/add_note.html 添加云笔记 /note/mod/(\d+) mod_view templates/note/mod_note.html 修改之前云笔记 /note/del/(\d+) del_view 无(返回列表页) 删除云笔记 - 参考界面
-
添加新笔记界面
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DCCju5ny-1618039985962)(cloud_note_images/new_note.png)]
-
显示笔记列表
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QQzSOWLz-1618039985964)(cloud_note_images/list_note.png)]
-
修改云笔记
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hV7ASjUM-1618039985966)(cloud_note_images/mod_note.png)]
-
- 参考界面