码迷,mamicode.com
首页 > 其他好文 > 详细

django 王中王7之牛大头

时间:2019-05-23 21:12:14      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:pattern   models   count   tags   import   aci   red   ppi   orm   

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>

 

django 王中王7之牛大头

标签:pattern   models   count   tags   import   aci   red   ppi   orm   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!