django 王中王5之化妆品

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>

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

时间: 2024-10-30 04:14:58

django 王中王5之化妆品的相关文章

django 王中王8之踏青撒花

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 adminfrom django.urls import path,re_path,includefrom django.views.static import

django 王中王7之牛大头

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 adminfrom django.urls import path,re_pathfrom django.views.static import servefrom l

django 王中王10之游记

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 adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.

django 王中王9之获得记录

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 adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.url

django 王中王3之优惠卷

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,include urlpatterns = [ path('admin/', admin.

.15-浅析webpack源码之WebpackOptionsApply模块之插件王中王

总体过了一下后面的流程,发现Compiler模块确实不适合单独讲解,这里继续讲解后面的代码: compiler.options = new WebpackOptionsApply().process(options, compiler); 这行代码与之前设置options默认值非常相似,但是复杂程度根本不是一个次元的. 这一节只能简单的看一眼内部到底有多少东西,整理后源码如下: "use strict"; const OptionsApply = require("./Opt

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀

Django admin 中抛出 &#39;WSGIRequest&#39; object has no attribute &#39;user&#39;的错误

这是Django版本的问题,1.9之前,中间件的key为MIDDLEWARE_CLASSES, 1.9之后,为MIDDLEWARE.所以在开发环境和其他环境的版本不一致时,要特别小心,会有坑. 将settings里的MIDDLEWARE_CLASSES默认配置顺序改成如下 MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.middleware.common.CommonMiddlewar

django数据库中的时间格式与页面渲染出来的时间格式不一致的处理

django数据库中的时间格式与页面渲染出来的时间格式不一致的处理. 在数据库里,时间是这样显示的: date: 2012-07-21 12:27:22 | date | datetime | NO | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 在模板里用: <span>{{ post.date }}</span> 打印出来,时间格式是这样的: July 21, 20