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,re_path,include
from django.views.static import serve
from three.settings import UPLOAD_ROOT
urlpatterns = [
# path(‘admin/‘, admin.site.urls),
re_path(‘^upload/(?P<path>.*)$‘,serve,{‘document_root‘:UPLOAD_ROOT}),
path(‘web1/‘,include(‘web1.urls‘)),
]
副urls:
from django.contrib import admin
from django.urls import path,re_path,include
from web1 import views
urlpatterns = [
path(‘index/‘,views.Index.as_view()),
path(‘add_cate/‘,views.AddCate.as_view()),
path(‘add_goods/‘,views.AddGoods.as_view()),
path(‘show_goods/‘,views.ShowGoods.as_view()),
]
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=50)
class Meta:
db_table = ‘cate‘
class Goods(models.Model):
id = models.AutoField(primary_key=True)
image_url = models.CharField(max_length=255)
title = models.CharField(max_length=255)
price = models.DecimalField(max_digits=7,decimal_places=2)
content = models.CharField(max_length=255)
cate = models.ForeignKey(Cate,on_delete=models.CASCADE)
class Meta:
db_table = ‘goods‘
创建自定义过滤器
文件夹 templatetags
创建文件mt_filter
# -*- encoding: utf-8 -*-
from django import template
register = template.Library()
@register.filter
def my_str(val):
return "$"+str(val)+‘起‘
views:
from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from web1.models import *
from web.views import uploadfile
import json
# Create your views here.
class AddCate(View):
def get(self,request):
return render(request,‘qimo2/add_cate.html‘)
def post(self,request):
if request.method == ‘POST‘:
name = request.POST.get(‘name‘)
if name:
cate = Cate(name=name)
cate.save()
return redirect(‘/web1/add_cate/‘)
class AddGoods(View):
def get(self,request):
cate = Cate.objects.all()
return render(request,‘qimo2/add_goods.html‘,locals())
def post(self,request):
title = request.POST.get(‘title‘)
image_url = request.FILES.get(‘image_url‘)
uploadfile(image_url)
price = request.POST.get(‘price‘)
content = request.POST.get(‘content‘)
cate = request.POST.get(‘cate‘)
goods = Goods(title=title,image_url=‘/upload/‘+image_url.name,
price=price,content=content,cate_id=cate)
goods.save()
return redirect(‘/web1/add_goods/‘)
class Index(View):
def get(self,request):
cate = Cate.objects.all()
return render(request,‘qimo2/index.html‘,locals())
def post(self,request):
mes = {}
id = request.POST.get(‘id‘)
if id:
goods = Goods.objects.filter(cate=id).all()
goodlist = []
for i in goods:
dict = {}
dict[‘image_url‘] = i.image_url
dict[‘title‘] = i.title
dict[‘id‘] = i.id
dict[‘price‘] = float(i.price)
goodlist.append(dict)
mes[‘code‘] = 200
mes[‘goodlist‘] = goodlist
return HttpResponse(json.dumps(mes))
class ShowGoods(View):
def get(self,request):
id = request.GET.get(‘id‘)
if id:
goods = Goods.objects.filter(id=id)
return render(request,‘qimo2/show_goods.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="name"><br>
<button type="submit">添加</button>
</form>
<a href="/web1/add_goods/">点击进入添加商品</a><br>
<a href="/web1/index/">点击进入首页</a>
</body>
</html>
add_goods:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
添加标题:<input type="text" name="title"><br>
添加价格:<input type="text" name="price"><br>
添加内容:<input type="text" name="content"><br>
选择分类:
<select name="cate" id="">
{% for i in cate %}
<option value="{{ i.id }}">{{ i.name }}</option>
{% endfor %}
</select><br>
选择图片:<input type="file" name="image_url">
<button type="submit">添加</button>
</form>
<a href="/web1/add_cate/">点击进入添加分类</a>
<a href="/web1/index/">点击进入首页</a>
</body>
</html>
index:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/jquery-1.12.4.min.js"></script>
</head>
<body>
{% for i in cate %}
<a href="javascript:get_goods({{ i.id }})">{{ i.name }}</a>
{% endfor %}
<div class="goods">
</div>
</body>
<script>
function get_goods(id) {
$.ajax({
url:‘/web1/index/‘,
type:‘post‘,
dataType:‘json‘,
data:{‘id‘:id},
success:function (res) {
if(res.code == 200){
var mes = res.goodlist
var len = mes.length
var html = ‘<ul>‘
for(var i=0;i<len;i++){
html += ‘<li><a href="/web1/show_goods/?id=‘+ mes[i][‘id‘] +‘">‘ +
‘<img src="‘+ mes[i][‘image_url‘] +‘" width="70px" height="70px"></a>‘ +
‘</li><li>‘+ "$"+mes[i][‘price‘]+"起" +‘</li><li>‘+ mes[i][‘title‘] +‘</li>‘
}
html += ‘</ul>‘
$(‘.goods‘).html(html)
}
}
})
}
</script>
</html>
show_goods:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% load my_filter %}
<table border="2">
<tr>
<td>照片</td>
<td>标题</td>
<td>介绍</td>
<td>价格</td>
</tr>
<tr>
{% for i in goods %}
<td><img src="{{ i.image_url }}" width="70ps" height="70px"></td>
<td>{{ i.title }}</td>
<td>{{ i.content }}</td>
<td>{{ i.price | my_str}}</td>
{% endfor %}
</tr>
</table>
</body>
</html>
原文地址:https://www.cnblogs.com/lhrd/p/10914199.html