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 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

时间: 2024-10-20 01:45:13

django 王中王8之踏青撒花的相关文章

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 adminfrom django.urls import pathfrom web import views urlpatterns = [ path('add_go

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