1、项目说明
基于python+Django+Mysql的网上书城系统项目实战
项目需要安装pycharm专业版以及MySQL环境(环境搭建和破解可以看我的B站里的视频有讲解)
首先需要创建数据库数据库名字叫做 db_bookshop
运行sql导入数据或者也可以直接用命令生成(视频有讲解)
需要安装一下下面的模块(如果运行提示有模块未安装,安装一下即可,命令同下,改一下模块名即可)
项目需要安装 Django、及其附带的其他一些模块
Django==3.2.17
Pillow==9.4.0
PyMySQL==0.9.3
Werkzeug==2.2.2
安装命令如下:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple django(替换模块名)
2、项目主要技术
Django框架技术
python编程技术
MySQL数据库技术
前端技术
3、项目结构bookshop:
adminuser 管理员模块(项目的配置文件、主路由)
bookshop 主模块 (项目的配置文件、主路由)
Shop 商城功能模块 (客户浏览,下单等功能实现)
User 用户信息管理模块
static 静态文件文件夹(css,js)
templates 前端html文件
manage.py 程序的入口
venv 虚拟环境文件夹
4、项目主要功能
顾客登录注册
顾客信息管理
顾客浏览图书商品、查看、搜索图书信息
顾客加入购物车、加入订单
管理员添加、修改、删除、图书商品
5、部分源码
user.views.py
from django.shortcuts import render
from .models import UserInfo, Address
from .user import UserMethod
from django.db.models import Q
from django.http import HttpResponseRedirect, JsonResponse
from functools import wraps
# 01验证是否需要的登录(装饰器)
def login_required(f):
@wraps(f)
def decorated_function(request, *args, **kwargs):
thisuser = UserMethod(request)
userinfo = thisuser.getUserInfo()
if userinfo['islogin'] is not True:
return HttpResponseRedirect('/user/login/')
return f(request, *args, **kwargs)
return decorated_function
# 02用户注册
def register(request):
if request.method == "POST":
username = request.POST.get('txt_username')
password = request.POST.get('txt_password')
email = request.POST.get('txt_email')
print(username, password, email)
request.session["user"] = {'islogin': True, 'username': username, 'email': email}
newuser = UserInfo(username=username, email=email, password=password)
newuser.save()
return HttpResponseRedirect('/user/login')
else:
return render(request, 'bshop_user/register.html')
# 03已存在注册的用户数量
def register_exist(request):
username = request.GET.get('uname')
count = UserInfo.objects.filter(username=username).count()
return JsonResponse({'count': count})
# 04登录页面跳转
def login(request):
if request.method == "POST":
username = request.POST.get('txtUsername')
password = request.POST.get('txtPassword')
thisuser = UserInfo.objects.filter(Q(username=username)) # 查找用户
if thisuser.count() == 0: # 用户不存在
context = {'error_name': 1, 'error_pwd': 0, 'username': username, 'password': password}
return render(request, 'bshop_user/login.html', context)
else:
if thisuser.first().verify_password(password):
request.session["user"] = {'islogin': True, 'username': thisuser.first().username,
'email': thisuser.first().email,
'phone': thisuser.first().phone}
return HttpResponseRedirect("/shop/index/")
else: # 密码不对
context = {'error_name': 0, 'error_pwd': 1, 'username': username, 'password': password}
return render(request, 'bshop_user/login.html', context)
else:
this_user = UserMethod(request) # 从session 获取用户
userinfo = this_user.getUserInfo()
# 已登录
if userinfo['islogin']:
return HttpResponseRedirect("/shop/index/")
# 未登录
else:
return render(request, 'bshop_user/login.html')
# 05退出登录
def logout(request):
thisuser = UserMethod(request)
userinfo = thisuser.getUserInfo()
if userinfo['islogin']:
request.session.pop("user") # 清空session
return HttpResponseRedirect("/shop/index/") # 跳转首页
# 06获取用户信息
@login_required
def personalinfo(request):
# session中查找用户
thisuser = UserMethod(request)
userinfo = thisuser.getUserInfo()
thisuser = UserInfo.objects.filter(username=userinfo['username']).first()
context = {'username': thisuser.username, 'email': thisuser.email, 'userinfo': userinfo, 'thisuser': thisuser}
return render(request, "bshop_user/personalinfo.html", context)
# 07修改密码
@login_required
def editpassword(request):
thisuser = UserMethod(request)
user_info = thisuser.getUserInfo()
thisuser = UserInfo.objects.filter(username=user_info['username']).first()
if request.method == "POST":
oldpassword = request.POST.get('oldpassword')
newpassword = request.POST.get('newpassword')
if thisuser.verify_password(oldpassword):
thisuser.password = newpassword
thisuser.save()
return HttpResponseRedirect("/shop/index/")
else: # 密码不对
context = {'error_pwd': 1, 'oldpassword': oldpassword, 'newpassword': newpassword, 'userinfo': user_info}
return render(request, 'bshop_user/editpwd.html', context)
else:
context = {'error_pwd': 0, 'oldpassword': '', 'newpassword': '', 'userinfo': user_info}
return render(request, 'bshop_user/editpwd.html', context)
# 08添加编辑地址
@login_required
def editaddress(request):
thisuser = UserMethod(request)
user_info = thisuser.getUserInfo()
userinfo1 = UserInfo.objects.filter(username=user_info['username'])
userinfo1 = userinfo1.first()
this_address = Address.objects.filter(userinfo_id=userinfo1.uid)
# 没有地址,可添加
if this_address.count() == 0:
addressinfo = {
'userinfo': user_info,
'address': {
'curaddress': '请设置收货地址',
'province': '重庆市',
'city': '重庆市',
'district': '',
'detail': '',
'getname': '',
'getphone': '',
'getcode': ''
}
}
if request.method == 'POST':
province = request.POST.get('province')
city = request.POST.get('city')
district = request.POST.get('district')
adddetail = request.POST.get('adddetail')
getphone = request.POST.get('getphone')
getcode = request.POST.get('getcode')
getname = request.POST.get('getname')
newaddress = Address(province=province, city=city, district=district,
detail=adddetail, get_name=getname, get_phone=getphone, get_code=getcode,
userinfo_id=userinfo1.uid)
newaddress.save()
return HttpResponseRedirect("/user/address/")
# 存在地址,更新地址
else:
this_address = this_address.first()
addressinfo = {
'userinfo': user_info,
'address': {
'curaddress': this_address.getFullAddress(),
'province': this_address.province,
'city': this_address.city,
'district': this_address.district,
'detail': this_address.detail,
'getname': this_address.get_name,
'getphone': this_address.get_phone,
'getcode': this_address.get_code,
}
}
if request.method == 'POST':
province = request.POST.get('province')
city = request.POST.get('city')
district = request.POST.get('district')
adddetail = request.POST.get('adddetail')
getname = request.POST.get('getname')
getphone = request.POST.get('getphone')
getcode = request.POST.get('getcode')
this_address.province = province
this_address.city = city
this_address.district = district
this_address.detail = adddetail
this_address.get_name = getname
this_address.get_phone = getphone
this_address.get_code = getcode
this_address.save()
return HttpResponseRedirect("/user/address/")
return render(request, "bshop_user/address.html", addressinfo)
# 09获取用户保存的地址
@login_required
def getaddress(request):
thisuser = UserMethod(request)
user_info = thisuser.getUserInfo()
userinfo1 = UserInfo.objects.filter(username=user_info['username'])
userinfo1 = userinfo1.first()
this_address = Address.objects.filter(userinfo_id=userinfo1.uid)
# 无地址
if this_address.count() == 0:
return JsonResponse({
'recode': 0,
'remsg': '无地址',
'data': {
'error': '',
'address': {
'province': '重庆市',
'city': '重庆市',
'district': '',
'detail': ''
}
}
})
# 有地址
else:
thisaddress = this_address.first()
return JsonResponse({
'recode': 1,
'remsg': '获取地址成功',
'data': {
'error': '',
'address': {
'province': thisaddress.province,
'city': thisaddress.city,
'district': thisaddress.district,
'detail': thisaddress.detail
}
}
})
6、运行截图
7、项目总结
本项目是一个非常适合学习练手,作为大作业、毕业设计的一个项目,对我们的Django框架技术以及MySQL数据库技术,对开发经验的提升,完善简历提高技能都有有很大的帮助,推荐大家学习研究这个项目,搞懂其中的业务逻辑流程以及各个知识点非常关键。
项目资料截图:
资料获取地址:https://h5.m.taobao.com/awp/core/detail.htm?ft=t&id=703702275939
B站视频讲解地址:https://www.bilibili.com/video/BV1t54y1w7sG/
注:其他问题请参看视频讲解,都有介绍,一定要认真看完哦!
代码编写、视频录制不易,感谢您的支持,祝您学习愉快!
避免走丢,记得关注哦🌹🌹🌹