博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django 王中王8之踏青撒花
阅读量:5098 次
发布时间:2019-06-13

本文共 5330 字,大约阅读时间需要 17 分钟。

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 }}" alt="" 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

你可能感兴趣的文章
vs2008 此安装不支持该项目类型
查看>>
C# Hash算法
查看>>
转:C语言深度剖析三
查看>>
Educational Codeforces Round 69 (Rated for Div. 2) A - DIY Wooden Ladder
查看>>
stm32之CMSIS标准、库目录、GPIO
查看>>
Dima and Lisa
查看>>
《算法4》回顾(一)
查看>>
Repeater用ul li,一行显示多条数据
查看>>
Java并发(四):并发集合ConcurrentHashMap的源码分析
查看>>
5. Longest Palindromic Substring
查看>>
Maven 三种archetype说明
查看>>
oracle自关联表的子删父变功能实现
查看>>
程序员需要具备的基本技能
查看>>
jsoncpp cmake
查看>>
Web消息主体风格(Message Body Style)
查看>>
eclipse- 智能提示设置
查看>>
回调函数实例——数学计算
查看>>
C#文件路径乱码
查看>>
俞伯牙摔琴谢知音的故事
查看>>
【简单dp】2080->最长公共子序列问题 动态规划
查看>>