0. 页面渲染
0.1 url
# 拍卖列表
path('auction/list/', views.auction_data_view, name='auction_list'),
0.2 view
from django.shortcuts import render, redirect, HttpResponse
from web.forms import AuctionList
from api import models
from django.forms.models import model_to_dict
def auction_data_view(request):
"""专场展示页面"""
queryset_auction = models.Auction.objects.all()
auction_list = queryset_auction
return render(request, 'auction_list.html', context={'auction_list': auction_list})
0.3 html
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8">
<title>拍卖列表</title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="{% static 'publc/font-awesome-4.7.0/css/font-awesome.min.css' %}">
</head>
<body>
<div>
<h3>拍卖专场</h3>
<a href="{% url 'auction_add' %}" class="btn btn-info">添加</a>
</div>
<div>
<div class="bs-example" data-example-id="hoverable-table">
<table class="table table-hover">
<thead>
<tr>
<th>序号</th>
<th>封面</th>
<th>专场</th>
<th>预展时间</th>
<th>拍卖时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for auction in auction_list %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
<td><img src="https://{{ auction.cover }}" alt="" width="60rpx"></td>
<td>{{ auction.title }}</td>
<td>{{ auction.preview_start_time }}</td>
<td>{{ auction.auction_start_time }}</td>
<td>{{ auction.get_status_display }}</td>
<td><a href="{% url 'auction_edit' pk=auction.id %}"><i class="fa fa-wrench"
aria-hidden="true"></i></a>|
<a href="{% url 'auction_del' pk=auction.id %}"><i class="fa fa-window-close"
aria-hidden="true" style="color: red"></i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>
1. 添加
1.1 url
# 拍卖场添加
path('auction/add/', views.auction_add_view, name='auction_add'),
1.2 view
from django.shortcuts import render, redirect, HttpResponse
from web.forms import AuctionList
from api import models
def auction_add_view(request):
"""专场添加页面"""
if request.method == 'GET':
form = AuctionList()
return render(request, 'auction_add_edit.html', context={'form': form})
form = AuctionList(data=request.POST)
if form.is_valid():
form.save()
return redirect('auction_list')
return render(request, 'auction_add_edit.html', context={'errors': form.errors})
1.3 form
ps 编辑图片跟上传图片,类型不一致,
编辑:FieldFile
上传:InMemoryUploadedFile
import uuid
from django import forms
from utils.centent.cos import upload_img
from api import models
# 编辑(为改变)
from django.db.models.fields.files import FieldFile
# 上传
from django.core.files.uploadedfile import InMemoryUploadedFile
class BootStrapForm:
except_bootstrap_styles = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
if name in self.except_bootstrap_styles:
continue
old_class = field.widget.attrs.get('class', "")
field.widget.attrs['class'] = '{} form-control'.format(old_class)
field.widget.attrs['placeholder'] = f'请输入{field.label}'
class AuctionList(BootStrapForm, forms.ModelForm):
except_bootstrap_styles = ['cover']
class Meta:
model = models.Auction # 对应的Model中的类
exclude = ['create_time', 'look_count', 'bid_count', 'goods_count', 'total_price', 'video']
def clean(self):
cover_file_object = self.cleaned_data.get('cover')
# 判断是没有传图片 or 编辑(为修改)
if not cover_file_object or isinstance(cover_file_object, FieldFile):
return self.cleaned_data
# print('obj', cover_file_object)
# 获取上传的文件名称,
# 进行拼接,
ext = cover_file_object.name.rsplit('.', maxsplit=1)[-1]
file_name = "{0}.{1}".format(str(uuid.uuid4()), ext)
# 调用上传文件,上传到cos
result_url = upload_img(Bucket='19803630852-1645846574-1305448189', region='ap-chengdu', Body=cover_file_object,
Key=file_name)
# 给cover字段添加路由
self.cleaned_data['cover'] = result_url
# print('url', result_url)
return self.cleaned_data
1.4 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拍卖后台</title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="text-align: center;border: 1px solid olivedrab;width: 520px;margin: 0 auto">
<form method="post">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
<button type="submit" class="btn btn-success">保存</button>
</form>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>
2. 编辑
2.1 url
# 拍卖场编辑
re_path(r'auction/edit/(?P<pk>\d+)/$', views.auction_edit_view, name='auction_edit'),
2.2 view‘
from django.shortcuts import render, redirect, HttpResponse
from web.forms import AuctionList
from api import models
def auction_edit_view(request, pk):
"""专场编辑页面"""
old_auction_onj = models.Auction.objects.filter(pk=pk).first()
if request.method == 'GET':
form = AuctionList(instance=old_auction_onj)
return render(request, 'auction_add_edit.html', context={'form': form})
form = AuctionList(data=request.POST, instance=old_auction_onj)
if form.is_valid():
form.save()
return redirect('auction_list')
return render(request, 'auction_add_edit.html', context={'errors': form.errors})
2.3 form
from django import forms
from api import models
class BootStrapForm:
except_bootstrap_styles = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
if name in self.except_bootstrap_styles:
continue
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['placeholder'] = f'请输入{field.label}'
class AuctionList(BootStrapForm, forms.ModelForm):
class Meta:
model = models.Auction # 对应的Model中的类
exclude = ['create_time', 'look_count', 'bid_count', 'goods_count', 'total_price', 'video']
2.4 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拍卖后台</title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="text-align: center;border: 1px solid olivedrab;width: 520px;margin: 0 auto">
<form method="post">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
<button type="submit" class="btn btn-success">保存</button>
</form>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>