码迷,mamicode.com
首页 > 其他好文 > 详细

django 王中王5之化妆品

时间:2019-05-23 21:01:20      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:cat   foreign   dump   ==   oct   cad   img   data   尺寸   

setting:


STATIC_URL = ‘/static/‘
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
from web import views

urlpatterns = [
path(‘add_goods/‘, views.Add_goods.as_view()),
path(‘add_cate/‘, views.Add_cate.as_view()),
path(‘index/‘, views.index),
path(‘find/‘, views.find),
]

 

 

models:

from django.db import models

# Create your models here.

class Cate(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30,verbose_name=‘分类名‘)
class Meta():
db_table = ‘cate‘
def __str__(self):
return self.name


class Goods(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30, verbose_name=‘商品名‘)
img = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5,decimal_places=2)
cate = models.ForeignKey(Cate,on_delete=‘CASCADE‘)

class Meta():
db_table = ‘goods‘
def __str__(self):
return self.name

 

 

 

 

views:

from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from web import models
import os
from hell_five import settings
import json

# 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()

class Add_cate(View):
def get(self,request):
return render(request,‘add_cate.html‘)
def post(self,request):
name = request.POST.get(‘cate‘)
print(name)
print(1111)
if name:
new = models.Cate(name=name)
new.save()
return redirect(‘/index/‘)
else:
return HttpResponse(‘请正确输入‘)

class Add_goods(View):
def get(self,request):
cate = models.Cate.objects.all()
return render(request,‘add_goods.html‘,locals())
def post(self,request):
c_id = request.POST.get(‘cat‘)
good = request.POST.get(‘good‘)
price = request.POST.get(‘price‘)
img = request.FILES.get(‘img‘)
uploadfile(img)
cat = request.POST.get(‘cat‘)
if all([good,price,img,cat,c_id]):
new = models.Goods(name=good,price=price,
img=‘/upload/‘+img.name,
cate_id=c_id)
new.save()
return redirect(‘/index/‘)
else:
return HttpResponse(‘信息不完整‘)

def index(request):
mes ={}
if request.method ==‘GET‘:
cate = models.Cate.objects.all()
goods = models.Goods.objects.all()
if request.method == ‘POST‘:
id = request.POST.get(‘id‘)
good = models.Goods.objects.filter(cate_id=id).all()
goodlist = []
for i in good:
dict={}
dict[‘id‘]=i.id
dict[‘name‘]=i.name
dict[‘img‘]=i.img
dict[‘price‘]=float(i.price)
dict[‘cate‘]=i.cate_id
goodlist.append(dict)
mes[‘goods‘] = goodlist
mes[‘code‘] = 200
return HttpResponse(json.dumps(mes))

return render(request,‘index.html‘,locals())

def find(request):
if request.method == ‘GET‘:
find = request.GET.get(‘find‘)
goods = models.Goods.objects.filter(name__contains=find).all()
return render(request, ‘find.html‘,locals())

 

 

 

 

html:

add_cate:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
分类名: <input type="text" name="cate">
<button type="submit">添加</button>
</form>
<a href="/index/">回到主页</a>
</body>
</html>

 

 

 

add_goods:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/add_goods/" method="post" enctype="multipart/form-data">
商品名: <input type="text" name="good"> <br>
商品价格: <input type="text" name="price"> <br>
商品图片: <input type="file" name="img"> <br>

 

所属分类: <select name="cat">
{% for i in cate %}
<option value="{{ i.id }}">{{ i.name }}</option>
{% endfor %}

</select> <br>
<button type="submit">添加</button>
</form>
<a href="/index/">回到主页</a>
</body>
</html>

 

 

 

 

asd:

<td><img src="
‘+mes[i]["img"]+‘"></td>

 


<td><img src="mes[i][img]" width="50px" height="50px"></td>


‘<td><img src="‘ + mes[i][img] + ‘"width="50px" height="50px">‘

 

 

asdasdasdasdasdasdasdasd:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="width:100%;border-style:solid;border-width:1pt;border-color:black;">
<div style="width:55%;text-align:center;border-style:solid;border-width:1pt;border-color:black;display:inline;">商品信息</div>
<div style="width:15%;text-align:center;border-style:solid;border-width:1pt;border-color:black;display:inline;">单价</div>
<div style="width:15%;text-align:center;border-style:solid;border-width:1pt;border-color:black;display:inline;">数量</div>
<div style="width:15%;text-align:center;border-style:solid;border-width:1pt;border-color:black;display:inline;">金额</div>
</div>
</body>
</html>

 

 

catelist:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分类展示</title>
<script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
</head>
<body>
{% for i in cate %}
<button onclick="showgoods({{ i.id }})">{{ i.name }}</button>
{% endfor %}

<div class="aaaaa"></div>

</body>

<script>

function showgoods(id) {

$.ajax({
url:‘/catelist/‘,
type:‘post‘,
dataType:‘json‘,
data:{‘cid‘:id},
success:function (res) {
if(res.code==200){
var mes = res.goods
var len = mes.length
var html = ‘<table border="1">‘
html += ‘<tr><td>名称</td><td>价格</td><td>尺寸</td><td>处理器</td><td>图片</td></tr>‘
for(var i=0;i<len;i++){
html+=‘<tr><td>‘+mes[i][‘title‘]
+‘</td><td>‘+mes[i][‘price‘]
+‘</td><td>‘+mes[i][‘size‘]
+‘</td><td>‘+mes[i][‘cpu‘]
+‘</td><td><img src="‘
+mes[i]["img"]+‘"></td></tr>‘
}
$(‘.aaaaa‘).html(html)
}
}

})
}

</script>


</html>

 

 

find:

<!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>
</tr>
{% for g in goods %}
<tr>
<td>{{ g.name }}</td>
<td>{{ g.price }}</td>
<td><img src="{{ g.img }}" height="60px" width="60px"></td>
<td><a href="">立即购买</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>
<script src="/static/jquery-1.12.4.min.js"></script>
</head>
<body>
<table border="1">
<tr>
<td><a href="/index/">全部分类</a></td>
{% for i in cate %}
<td>
<a href="javascript:change({{ i.id }})">{{ i.name }}</a></td>

{% endfor %}
</tr>


</table>
<div id="id">
<tr>
<td>商品名</td>
<td>商品价格</td>
<td>商品图片</td>
</tr>
<table border="1">
{% for g in goods %}
<tr>
<td>{{ g.name }}</td>
<td>{{ g.price }}</td>
<td><img src="{{ g.img }}" height="60px" width="60px"></td>
<td><a href="">立即购买</a></td>

</tr>
{% endfor %}
<tr>
<td>
<form action="/find/">
<input type="text" name="find">
<button type="submit">查找</button>
</form>
</td>
</tr>
</table>
</div>
</body>
<a href="/add_goods/">添加商品</a>
<a href="/add_cate/">添加分类</a>
<script>
function change(id) {
$.ajax({
url:‘/index/‘,
type:‘post‘,
dataType:‘json‘,
data:{‘id‘:id},
success:function (res) {
if(res.code == 200){
var mes = res.goods
var len = mes.length
var html = ‘<table border="1">‘
html += ‘<tr><td>商品名称</td><td>商品价格</td><td>图片</td></tr>‘
for(var i=0;i<len;i++){
html+=‘<tr><td>‘+mes[i][‘name‘]
+‘</td><td>‘+mes[i][‘price‘]
+‘</td><td><img src="‘
+mes[i]["img"]+
‘" width="60px" height="60px"></td><td><a href="">立即购买</a></td></tr>‘
}
$(‘#id‘).html(html)
}
}
})
}
</script>
</html>

 

django 王中王5之化妆品

标签:cat   foreign   dump   ==   oct   cad   img   data   尺寸   

原文地址:https://www.cnblogs.com/lhrd/p/10914304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!