django 测试服2之电商

setting:

图片上传前要记得创建upload文件夹跟templates同级

STATIC_URL = '/static/'

# DIR目录列表的意思
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]

UPLOAD_ROOT = os.path.join(BASE_DIR,'upload')

 

urls:

from django.contrib import admin
from django.urls import path,include,re_path
from django.views.static import serve
from again.settings import UPLOAD_ROOT

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('web.urls')),
re_path('^upload/(?P<path>.*)$',serve,{'document_root':UPLOAD_ROOT})
]

 

 

views:

from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from again import settings
import os
from web.models import *
# Create your views here.


#图片流 添加图片的东西
def uploadfile(img):
f = open(os.path.join(settings.UPLOAD_ROOT, '', img.name), 'wb')
for chunk in img.chunks():
f.write(chunk)
# 关闭文件流
f.close()

def index(request):
# 从session中获取存入的当前店主id和店主名
id_ = request.session.get('id')
name_ = request.session.get('name')
print(id_,name_)
# 查询所有的店主信息和所有商品信息
cate = Shop.objects.all()
good = Goods.objects.order_by('-price').all()
return render(request,'index.html',locals())

class Login(View):
def get(self,request):
username = request.GET.get('username')
password = request.GET.get('password')
ok = request.GET.get('ok')
print(username,password,ok)
if ok:
if not all([username, password]):
mes = '信息不全'
else:
user = Shop.objects.filter(username=username,password=password).first()
print(user)
if user:
request.session['id'] =user.id
request.session['name'] =user.name
return redirect('/index/')
else:
mes = '用户名错误'
else:
mes=""
return render(request,'login.html')
return render(request,'login.html',{'mes':mes})

 

 

 

class Add_goods(View):
def get(self,request):
name_ = request.session.get('name')
return render(request,'add.html',locals())
def post(self,request):
id = request.session.get('id')
data = request.POST
name_ = data.get('name')
price_ = data.get('price')
img_ = request.FILES.get('img')
uploadfile(img_)
if all([name_,price_,img_]):
name = Goods.objects.filter(name=name_)
if name:
mes = '已经存在该商品'
else:
new = Goods(name=name_,
price=price_,
img='/upload/'+img_.name,
shop_id=id)
new.save()
return redirect('/index/')

class Edit(View):
def get(self,request):
id_ = request.session.get('id')
goods = Goods.objects.filter(shop_id=id_).all()
return render(request, 'edit.html', {'goods':goods})

def delete(request):
id_ = request.GET.get('id')
Goods.objects.filter(id=id_).delete()
return redirect('/change/')

class Change(View):
def get(self,request):
id = request.GET.get('id')
good = Goods.objects.filter(id=id).all()
return render(request,'change.html',locals())
def post(self,request):
data = request.POST
id_ = request.GET.get('id')
name_ = data.get('name')
price_ = data.get('price')
img_ = request.FILES.get('img')
uploadfile(img_)
if all([name_, price_, img_]):
new = Goods.objects.filter(id=id_).first()
new.name = name_
new.price = price_
new.img = '/upload/'+img_.name
new.save()
return redirect('/edit/')

 

html:

add

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>商品添加页</h3>
<h3>欢迎您来到{{ name_ }}</h3> <br>
<form action="" method="post" enctype="multipart/form-data">
请输入商品名: <input type="text" name="name"> <br>
请选择商品图片: <input type="file" name="img"> <br>
请输入商品价格: <input type="text" name="price"> <br>
<button type="submit">上传</button>
</form>
<a href="/index/">返回主页</a>
</body>
</html>

 

change

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>商品修改页</h3>
<h3>欢迎您来到{{ name_ }} 的修改商品页面</h3> <br>
<form action="" method="post" enctype="multipart/form-data">
{% for i in good %}
请输入商品名: <input type="text" name="name" value="{{ i.name }}"> <br>
请选择商品图片: <input type="file" name="img" value="{{ i.img_name }}"> <br>
请输入商品价格: <input type="text" name="price" value="{{ i.price }}"> <br>
<button type="submit">确定</button>
{% endfor %}

<a href="/index/">返回主页</a>
</form>
</body>
</html>

 

edit:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>商品名</td>
<td>商品价格</td>
<td>商品图片</td>
<td>编辑</td>
</tr>
{% for g in goods %}
<tr>
<td>{{ g.name }}</td>
<td>{{ g.price }}</td>
<td><img src="{{ g.img }}" height="70px" width="80px"> </td>
<td>
<a href="/delete/?id={{ g.id }}">删除</a> <br>
<a href="/change/?id={{ g.id }}">修改</a>
</td>
</tr>
{% endfor %}

</table>
<a href="/index/">返回主页</a>
</body>
</html>

 

 

index:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4>欢迎您来到{{ name_ }}</h4>

 

{% for i in cate %}
<ul>
<p> <h3>{{ i.name }}:
{% if id_ == i.id %}
<a href="/edit/">管理店铺商品</a></h3> </p> <br>
{% endif %}
{% if id_ == i.id%}
<a href="/add/">添加商品</a>
{% endif %}

</ul>
<ul>
<table border="1">
<tr>
<td>商品名</td>
<td>商品价格</td>
<td>商品图片</td>
</tr>

{% for g in good %}
<tr>
{% if g.shop_id == i.id %}
<td>{{ g.name }}</td>
<td>{{ g.price }}</td>
<td><img src="{{ g.img }}" height="120px" width="120px"> </td>
{% endif %}
</tr>
{% endfor %}

</table>
</ul>
{% endfor %}

<a href="/login/">返回店主登录页</a>

</body>
</html>

 

login:略

 

转载于:https://www.cnblogs.com/lhrd/p/10910793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值