django 王中王15之淘宝

seetting:

STATIC_URL = '/static/'

STATICFILES_DIRS =[
os.path.join(BASE_DIR, 'static')
]

 

urls:

from django.contrib import admin
from django.urls import path
from web import views

urlpatterns = [
path('add_goods/', views.add_goods),
path('', views.index),
]

 

models:

from django.db import models

# Create your models here.


class Goods(models.Model):
name = models.CharField(max_length=50)
price = models.CharField(max_length=50)
cate = models.CharField(max_length=50)
img = models.CharField(max_length=255)

 

 

 

views:

from django.shortcuts import render
from web import models
from Django_15 import settings
import os
from django.core.paginator import PageNotAnInteger, EmptyPage, Paginator


# Create your views here.
def add_goods(request):
if request.method == 'GET':
return render(request, 'add_goods.html')
if request.method == 'POST':
name = request.POST.get('name')
price = request.POST.get('price')
cate = request.POST.get('cate')
file = request.FILES.get('img')
if file:
base_path = settings.STATICFILES_DIRS[0]
file_path = os.path.join(base_path, 'img/' + file.name)
with open(file_path, 'wb') as fp:
if file.multiple_chunks:
for buf in file.chunks():
fp.write(buf)
else:
fp.write(file.read())

models.Goods.objects.create(
name=name,
price=price,
cate=cate,
img='img/' + file.name,
)
return render(request, 'add_goods.html',locals())


def index(request):
all = models.Goods.objects.all()
p = Paginator(all, 3)
page_id = request.GET.get('page_id')
if page_id:
try:
all = p.page(page_id)
except PageNotAnInteger:
all = p.page(1)
except EmptyPage:
all = p.page(1)
else:
all = p.page(1)
return render(request, 'index.html', locals())

 

 

html:

add_goods:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加产品</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
请输入产品名称:<input type="text" name="name"><br>
请输入产品价格:<input type="text" name="price"><br>
请输入产品分类:<input type="text" name="cate"><br>
请上传产品图片:<input type="file" name="img"><br>
<button type="submit">上传</button>
</form>
</body>
</html>

 

 

 

index:

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
<meta charset="UTF-8">
<title>首页展示</title>
</head>
{% load static %}
<body>
{% for a in all %}
{# <div>#}
<div style="float: left;width: 450px" >
<img src="{% static a.img %}" style="height: 300px; width: 300px"><br>
{{ a.name }}<br>
{{ a.cate }}<br>
价格:¥{{ a.price }}<br>
{{ a.name }}
</div>
{# </div>#}
{% endfor %}

<div>
{% if all.has_previous %}
<a href="/?page_id={{ all.previous_page_number }}">上一页</a>
{% else %}
上一页
{% endif %}
{% for num_ in p.page_range %}
{% if all.number == num_ %}
<a href="/?page_id={{ num_ }}">{{ num_ }}</a>
{% else %}
<a href="/?page_id={{ num_ }}">{{ num_ }}</a>
{% endif %}
{% endfor %}

{% if all.has_next %}
<a href="/?page_id={{ all.next_page_number }}">下一页</a>
{% else %}
下一页
{% endif %}

</div>

{# 跳转 #}
<input type="text" class="number">
<button οnclick="tiao()">跳转</button>
<script type="text/javascript">
function tiao() {
var number = $('.number').val();
window.location.href = '/?page_id=' + number
}
</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/lhrd/p/10914427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值