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 admin
from django.urls import path,re_path
from django.views.static import serve
from lianxi7.settings import UPLOAD_ROOT
from mypro import views

urlpatterns = [
path(‘admin/‘, admin.site.urls),
path(‘‘, views.index),
path(‘show/<int:id_>‘, views.show),
path(‘add/‘, views.add),
path(‘upload_img/‘,views.upload_img),
re_path(‘^upload/(?P<path>.*)$‘,serve,{‘document_root‘:UPLOAD_ROOT}),
]

models:

from django.db import models

# Create your models here.
class Shopping(models.Model):
name = models.CharField(max_length=30,verbose_name=‘名称‘)
img = models.TextField(verbose_name=‘图片‘)
count = models.IntegerField(blank=True,null=True,verbose_name=‘人气值‘)
price = models.CharField(max_length=10)

创建自定义过滤器

文件夹 templatetags

创建文件mt_filter

from django import template
register = template.Library()

@register.filter
def my_str(val):
return str(val)+"牛大头"

views:

from django.shortcuts import render,redirect
from django.http import HttpResponse
from mypro.models import Shopping
import json
import os
import random #生成随机数模块
import hashlib
from lianxi7.settings import UPLOAD_ROOT

# Create your views here.

def upload_img(request):
if request.method == ‘POST‘:
img = request.FILES.get(‘file‘)
if img:
with open(os.path.join(UPLOAD_ROOT,img.name), ‘wb‘) as fp:
for buf in img.chunks():
fp.write(buf)
# 迭代读取文件并写入到本地
response = {}
response[‘path‘] = ‘/upload/‘ + img.name
response[‘error‘] = False
return HttpResponse(json.dumps(response))

def add(request):
if request.method == ‘GET‘:
article = Shopping.objects.all().last()
return render(request,‘add.html‘,locals())
if request.method == ‘POST‘:
name = request.POST.get(‘name‘)
price = request.POST.get(‘price‘)
img = request.POST.get(‘img‘)
# 1-100的随机整数
count = random.randint(1,100)
print(count)
Shopping.objects.create(
name = name,
price = price,
img = img,
count=count
)
return redirect(‘/add/‘)

def index(request):
if request.method == ‘GET‘:
shopping = Shopping.objects.all()
money = 1
popular = 1
return render(request,‘index.html‘,locals())
if request.method == ‘POST‘:
popular = request.POST.get(‘popular‘)
money = request.POST.get(‘money‘)
if money:
if money == ‘0‘:
shopping = Shopping.objects.all().order_by(‘price‘)
money = ‘1‘
popular = ‘1‘
return render(request, ‘index.html‘, locals())
elif money == ‘1‘:
shopping = Shopping.objects.all().order_by(‘-price‘)
money = ‘0‘
popular = ‘1‘
return render(request, ‘index.html‘, locals())
if popular:
if popular == ‘0‘:
shopping = Shopping.objects.all().order_by(‘count‘)
popular = ‘1‘
money = ‘1‘
return render(request, ‘index.html‘, locals())
elif popular == ‘1‘:
shopping = Shopping.objects.all().order_by(‘-count‘)
popular = ‘0‘
money = ‘1‘
return render(request, ‘index.html‘, locals())

def show(request,id_):
shopping = Shopping.objects.filter(pk=id_).first()
return render(request,‘show.html‘,locals())

html:

add:

<!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>
<script src="../static/tinymce/js/tinymce/tinymce.min.js"></script>
<script src="../static/tinymce_setup.js"></script>
</head>
<body>
<form method="POST" action=‘‘ enctype="multipart/form-data">
<input type="text" placeholder="名称" name="name">
<br>
<input type="text" placeholder="价格" name="price">
<br>
<input id="rich_content" name="img" value="">
<br>
<button type="submit">提交</button>
</form>
{% if article %}
<h3>{{ article.title }}</h3>
<h4>{{ article.author }}</h4>
{# safe过滤器,识别标签 #}
<div>{{ article.content|safe }}</div>
{% endif %}
</body>
</html>

index:

<!DOCTYPE html>
{% load my_filter %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<form action="" method="post">
<table border="2" cellspacing="0">
<tr>
<td><button type="submit" name="money" value="{{ money }}">牛大头</button></td>
<td><button type="submit" name="popular" value="{{ popular }}"a>人气值</button></td>
</tr>
<tr>
<td>编号</td>
<td>商品名称</td>
<td>商品价格</td>
<td>人气值</td>
<td>详情</td>
</tr>
{% for foo in shopping %}
<tr>
<td>{{ forloop.counter0 }}</td>
<td><a href="show/{{ foo.id }}">{{ foo.name }}</a></td>
<td>{{ foo.price | my_str}}</td>
<td>{{ foo.count }}</td>
<td>{{ foo.img | safe }}</td>
</tr>
{% endfor %}
</table>
</form>
</body>
</html>

show:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>详情</title>
</head>
<body>
{{ shopping.name }}
{{ shopping.price }}
{{ shopping.img | safe }}
<a href="/">首页</a>
</body>
</html>

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

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

django 王中王7之牛大头的相关文章

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