练习随笔 - 电商平台

Posted derrickwilliams

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习随笔 - 电商平台相关的知识,希望对你有一定的参考价值。

models.py 

from django.db import models

class Shop(models.Model):
name = models.CharField(max_length=20,verbose_name=‘店铺名称‘)
username = models.CharField(max_length=20,verbose_name=‘登录账号‘)
password = models.CharField(max_length=20,verbose_name=‘登录密码‘)

class Goods(models.Model):
name = models.CharField(max_length=20,verbose_name=‘商品名称‘)
price = models.DecimalField(max_digits=10,decimal_places=5,verbose_name=‘价格‘)
img = models.ImageField(upload_to=‘upload‘,max_length=255,verbose_name=‘商品图片‘)
shop = models.ForeignKey(Shop, on_delete=models.SET_NULL, null=True, blank=True)

# Create your models here.

admin.py

from django.contrib import admin
from . import models

admin.site.register(models.Shop)
admin.site.register(models.Goods)
# Register your models here.

views.py

from django.shortcuts import render,HttpResponse,redirect
from . import models
import os
from test2 import settings
def register(request): #注册
if request.method == ‘GET‘:
return render(request,‘register.html‘)
if request.method == ‘POST‘:
name = request.POST.get(‘name‘) #输入名称
username = request.POST.get(‘username‘) #输入登录名
password = request.POST.get(‘password‘) #输入登录密码
re_pwd = request.POST.get(‘re_pwd‘) #重复提交密码
if username and password and re_pwd: #判断是否获取到三个值
if password == re_pwd: #如果两次输入密码一致
user_obj = models.Shop.objects.filter(username=username).first() #查询用户名是否存在
if user_obj: #如果已经存在
return HttpResponse(‘用户已存在‘)
else: #如果都没有存在就将输入的存入数据库
models.Shop.objects.create(name=name,username=username,password=password).save()
return redirect(‘/login/‘) #返回
else: #如果两次密码输入不一致
return HttpResponse(‘两次密码不一致‘)
else: #如果有空值
return HttpResponse(‘不能有空!‘)


def login(request):
#登录页面
if request.method == ‘GET‘:
return render(request, ‘login.html‘)
if request.method == ‘POST‘:
username = request.POST.get(‘username‘) #输入账号
password = request.POST.get(‘password‘) #输入密码
user_obj = models.Shop.objects.filter(username=username,password=password).first()#数据库匹配
if user_obj: #判断是否有对应账户和密码是否匹配
request.session[‘user_id‘]=user_obj.id #存session
return redirect(‘/show/‘) #返回
else: #如果账户和密码不匹配
return HttpResponse(‘用户名或密码错误‘)

def index(request):

shop = models.Shop.objects.all() #数据库查询
good = models.Goods.objects.all()
return render(request, ‘index.html‘, locals()) #把变量传到html中


def show(request):
user_id = request.session.get(‘user_id‘) #取session
shop = models.Shop.objects.all().filter(id=user_id) #通过session查询登录的
good = models.Goods.objects.all().filter(shop_id=user_id).order_by(‘-price‘)
context = {
‘shop‘ : shop, #teacher传到Html
‘good‘ : good
}
return render(request, ‘show.html‘, context)

def update(request):
user_id = request.session.get(‘user_id‘) #取session
shop = models.Shop.objects.all().filter(id=user_id) #通过session查询登录的
good = models.Goods.objects.all().filter(shop_id=user_id).order_by(‘-price‘)
context = {
‘shop‘ : shop, #teacher传到Html
‘good‘ : good
}
return render(request, ‘update.html‘, context)


def add(request):
user_id = request.session.get(‘user_id‘) #取session
if request.method == ‘GET‘:
return render(request, ‘add.html‘)
if request.method == ‘POST‘:
name = request.POST.get(‘name‘)
price = request.POST.get(‘price‘)
img = request.FILES.get(‘img‘)
f = open(os.path.join(settings.UPLOAD_ROOT, ‘‘, img.name), ‘wb‘)

# 写文件 遍历图片文件流
for chunk in img.chunks():
f.write(chunk)
# 关闭文件流
f.close()
if name and price and img:
models.Goods.objects.create(name=name,price=price,img=img,shop_id=user_id)
else:
return HttpResponse(‘不能有空值‘)
return redirect(‘/update/‘)


def delete(request,id): # 删除
models.Goods.objects.filter(id=id).delete() # 通过session查询学生的id然后将主键关联删除
return redirect(‘/update/‘)
# Create your views here.
HTML页面

add页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>添加商品页</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
请输入商品名称:<input type="text" name="name"><br>
请输入商品价格:<input type="text" name="price"><br>
请选择商品图片:<input type="file" name="img" ><br>

<button type="submit">上传</button>
</form>
</body>
</html>

index页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页展示</title>
</head>
<body>

{% for i in shop %}
{# 循环#}

{{ i.name }}的店
{# 取姓名#}
<table border="1">

<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>商品图片</td>
</tr>
{% for x in good %}
{% if x.shop_id == i.id %}
<tr>
<td>{{ x.name }}</td>
<td>{{ x.price }}</td>
<td><img src="{{ x.img }}" ></td>

</tr>
{% endif %}
{% endfor %}
</table>
{% endfor %}

</body>
</html>

login页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h1>登陆界面</h1>
{#最简单的html代码#}
{#这里要提醒下,提交到后台的get,post方式都可以,但是作为账户数据的话,用post#}
<form action="/login/" method="post">
{# form表单#}
{% csrf_token %}
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>

register页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>register</title>
</head>
<body>
<h1>注册界面</h1>
<form action="" method="post">
{% csrf_token %}
店铺名:<input type="text" name="name"><br>
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="re_pwd"><br>
<input type="submit" value="注册">
</form>
</body>
</html>

show页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页展示</title>
</head>
<body>

{% for i in shop %}

<h1>{{ i.name }}的店铺:<button><a href="/update/">管理店铺商品</a></button></h1>
<h3>{{ i.name }}的店铺信息:</h3>
{% endfor %}

<table border="1">

<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>商品图片</td>
</tr>
{% for x in good %}
<tr>
<td>{{ x.name }}</td>
<td>{{ x.price }}</td>
<td><img src="/{{ x.img }}" ></td>
</tr>
{% endfor %}
</table>



</body>
</html>

update页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>


{% for i in shop %}

<h1>{{ i.name }}的商品:</h1>
{% endfor %}
<a href="/add/">添加商品</a>
<table border="1">

<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>商品图片</td>
<td>删除</td>
</tr>
{% for x in good %}
<tr>
<td>{{ x.name }}</td>
<td>{{ x.price }}</td>
<td><img src="/{{ x.img }}" ></td>
<td><a href="/del/{{ x.id }}/">删除</a></td>
</tr>
{% endfor %}
</table>


</body>
</html>

以上是关于练习随笔 - 电商平台的主要内容,如果未能解决你的问题,请参考以下文章

项目练习目录

CKG10-高性能高可用Yii2.0电商平台 仿京东商城 高级组件 MySQL LVS

Python数据分析第七周作业随笔记录

MYSQL练习随笔

随笔练习

接口练习随笔