一、项目预览:(关于喜羊羊与灰太狼简介项目)
二、功能介绍
1、主要用户可以进行注册账号,登录信息进入首页当中,修改密码,修改个人信息及个人头像。(数据库信息)。
2、观看喜羊羊与灰太狼的简单的介绍信息。
三、项目前期配置
1、创建一个django项目。
2、在 yangcun项目中创建一个app (在工具栏中选中Run manage.py Task...)在框中输入 startapp (app名) 创建app。
3、注册app(在settings.py配置文件中找到INSTALLED_APPS在里面注册app),以及配置settings文件。
4、创建文件夹。
在users app中创建一个static文件夹,以及templates文件夹、把views.py文件删除,创建views文件夹、创建一个urls.py文件。在yangcun文件夹中创建一个media文件夹。
5、打开yangcun文件夹中的urls.py文件,在里面输入如下代码:
yangcun>urls.py:
from django.urls import path,include,re_path
from django.views.static import serve
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('user/', include("users.urls")),
re_path('^media/(?P<path>.*)$', serve, {'document_root' : settings.MEDIA_ROOT}),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
四、后端完整代码
在users>models.py中:
from django.db import models
# Create your models here.
# 用户表
class User(models.Model):
name = models.CharField(verbose_name="用户名",max_length=64)
account = models.CharField(verbose_name="账号",max_length=64)
password = models.CharField(verbose_name="密码",max_length=64)
img = models.ImageField(verbose_name="头像",max_length=255,upload_to="imgs",null=True)
age = models.IntegerField(verbose_name="年龄",null=True)
sex = models.CharField(verbose_name="性别",max_length=11,null=True) # True 是 男生 False 是女生
phone = models.CharField(verbose_name="联系方式",max_length=11,null=True)
introduction = models.TextField(verbose_name="个人简介",null=True)
time = models.DateTimeField(verbose_name="注册时间",auto_now=True)
然后执行:python3 manage.py makemigrations 和 Python3 manage.py migrate
在users>urls.py中:
"""yangcun URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from users.views import login,index
urlpatterns = [
# 登录
path('login/',login.login,name="login"), # 登录
path('logout/',login.logout,name="logout"), # 注销
path('register/',login.register,name="register"), # 注册
path('password/',login.password,name="password"), # 忘记密码
# 首页
path('index/',index.index,name="index"), # 首页
path('center/',index.center,name="center"), # 个人中心
path('works/',index.works,name="works"), # 作品简介
path('role/',index.role,name="role"), # 角色简介
]
在users>views>index.py中: (vindex.py需要自己创建)
from django.shortcuts import render,HttpResponse,redirect
from users import models
from users.models import User
# Create your views here.
# 首页
def index(request):
print(request.session.get("account"))
print(request.session.get("name"))
return render(request,"index.html")
# 个人中心
def center(request):
if request.method == "GET":
account = request.session.get("account") # 账号
print(account)
user_info = models.User.objects.filter(account=account).first()
print(user_info.sex)
print(user_info.img)
print(request.session.get("img"))
return render(request,"center.html",{"user_info":user_info})
account = request.POST["account"] # 账号
name = request.POST["name"] # 用户名
img_file = request.FILES.get("img") # 头像
age = request.POST["age"] # 年龄
sex = request.POST["sex"] # 性别
phone = request.POST["phone"] # 联系方式
introduction = request.POST["introduction"] # 个人简介
print(account)
print(img_file)
user_info = models.User.objects.exclude(account=account)
for i in user_info:
if i.phone == phone:
err = "联系方式错误或重复!"
return render(request,"center.html",{"err":err})
# 修改
info = User.objects.get(account=account)
info.name = name
if img_file == None:
pass
else:
info.img = img_file
info.age = age
if sex == "男":
info.sex = "男"
elif sex == "女":
info.sex = "女"
else:
err = "性别输入错误,请重新输入!"
return render(request,"center.html",{"err":err})
info.phone = phone
info.introduction = introduction
info.save()
request.session['account'] = account # 账号
request.session['name'] = name # 用户名
user_info = models.User.objects.filter(account=account).first()
request.session['img'] = str(user_info.img) # 头像路径
request.session['qx'] = 1 # 权限
return redirect("/user/center/")
# 作品简介
def works(request):
return render(request,"works.html")
# 角色简介
def role(request):
return render(request,"role.html")
在users>views>login.py中: (login.py需要自己创建)
from django.shortcuts import render,HttpResponse,redirect
from users import models
from users.models import User
# Create your views here.
# 注销
def logout(request):
request.session.clear()
return redirect("/user/login/")
# 登录页面
def login(request):
if request.method == "GET":
return render(request,'login.html')
account = request.POST["account"]
password = request.POST["password"]
user_info = models.User.objects.all()
for i in user_info:
if account == i.account and password == i.password:
request.session['account'] = i.account # 账号
request.session['name'] = i.name # 用户名
request.session['img'] = str(i.img) # 头像
request.session['qx'] = 1 # 权限
return redirect("/user/index/")
err = "账号或者密码错误请重新输入!"
return render(request,'login.html',{"err":err})
# 注册页面
def register(request):
if request.method == "GET":
return render(request,'register.html')
name = request.POST['name'] # 用户名
account = request.POST['account'] # 账号
password1 = request.POST['password1'] # 密码1
password2 = request.POST['password2'] # 密码2
user_info = models.User.objects.all() # 用户信息
err = "" # 错误信息
flag = 1
for i in user_info:
# 用户登录
if account == i.account:
err = "账号重复请重输!"
flag = 0
elif password1 != password2:
err = "密码不一致请重输!"
flag = 0
if flag == 1:
info = User()
info.name = name
info.account = account
info.password = password1
info.save()
print("保存成功!")
return redirect("/user/login/")
else:
return render(request,'register.html',{"err":err})
# 忘记密码
def password(request):
if request.method == "GET":
return render(request,'password.html')
account = request.POST["account"] # 账号
password = request.POST["password1"] # 原密码
new_password = request.POST["password2"]# 新密码
user_info = models.User.objects.all() # 用户信息
for i in user_info:
if account == i.account and password == i.password:
info = User.objects.get(account=account)
info.password = new_password
info.save()
return redirect("/user/login/")
err = "账号和密码错误请重输!"
return render(request,'password.html',{"err":err})