Django相关文件配置
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = False
os.path.join(BASE_DIR, 'template')
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
from django.urls import path,include
urlpatterns = [
path('',include('子应用名称.urls')),
]
from django.urls import path
from 子应用名称 import views
urlpatterns = [
path('',views.index),
]
class Cate(models.Model):
name = models.CharField(max_length=50,verbose_name='分类名称')
def __str__(self):
return self.name
class Computer(models.Model):
cate = models.ForeignKey(Cate,verbose_name='所属分类',on_delete=models.CASCADE)
name = models.CharField(max_length=50,verbose_name='电脑名称')
img_path = models.CharField(max_length=100,verbose_name='电脑图片')
price = models.DecimalField(max_digits=6,decimal_places=2,verbose_name='电脑价格')
size = models.CharField(max_length=50,verbose_name='显示器尺寸')
cpu = models.CharField(max_length=50,verbose_name='处理器规格')
def __str__(self):
return self.name
from 子应用名称.models import *
admin.site.register(表类名)
import os
from 主应用名称.settings import STATICFILES_DIRS
def add_computer(request):
if request.method == 'POST':
name = request.POST.get('name')
price = request.POST.get('price')
cate_id = request.POST.get('cate_id')
path = request.FILES.get('img_path')
if not all([name,price,size,cpu,cate_id,path]):
error = '输入框不能为空!'
return render(request,'add_computer.html',locals())
else:
cate = Cate.objects.filter(id=cate_id).first()
file_path = name + '.' + path.name.split('.')[-1]
file = 'img/' + file_path
with open(os.path.join(STATICFILES_DIRS[0],file),'wb') as fp:
fp.write(path.read())
Computer.objects.create(name=name,price=price,cate=cate,img_path=file)
return redirect('/')
cate = Cate.objects.all()
return render(request,'add_computer.html',locals())
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label>电脑名称:</label><input type="text" name="name"><br><br>
<label>价格:</label><input type="text" name="price"><br><br>
<label>所属分类:</label>
<select name="cate_id">
{% for i in cate %}
<option value="{{i.id}}">{{i.name}}</option>
{% endfor %}
</select><br><br>
<label>电脑图片:</label><input type="file" name="img_path"><br><br>
{{error}}
<br>
<input type="submit" value="添加">
</form>
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
def index(request):
computer = Computer.objects.all()
p = Paginator(computer,3)
id = request.GET.get('id')
try:
computer = p.page(id)
except EmptyPage:
computer = p.page(1)
except PageNotAnInteger:
computer = p.page(1)
return render(request,'index.html',locals())
{% if computer.has_previous %}
<a href='/?id={{ computer.previous_page_number }}'>上一页</a>
{% else %}
{% endif %}
{% if computer.has_next %}
<a href='/?id={{ computer.next_page_number }}'>下一页</a>
{% else %}
{% endif %}
from django.http import HttpResponse
def show_cate(request):
cate = Cate.objects.all()
if request.method == 'POST':
id = request.POST.get('id')
cate = Cate.objects.filter(id=id).first()
computer = Computer.objects.filter(cate=cate).all()
from django.core import serializers
computer = serializers.serialize('json',computer,ensure_ascii=False)
return HttpResponse(computer)
return render(request,'show_cate.html',locals())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分类展示页面</title>
<script src="/static/js/jquery-1.10.2.min.js"></script>
<script src="/static/js/jquery.cookie.js"></script>
</head>
<body>
<a href="/">点击返回首页</a><br><br>
<hr>
{% for i in cate %}
<button id="{{i.id}}">{{i.name}}</button>
{% endfor %}
<br><br>
<div id="content"></div>
</body>
<script>
$(document).ready(function(){
$('button').click(function(){
var id = this.id
//console.log(id)
$.ajax({
url:'/show_cate/',
type:'post',
data:{
'csrfmiddlewaretoken': $.cookie('csrftoken'),
id:id
},
success:function(res){
var res = JSON.parse(res)
//console.log(res)
var html = ''
for (var i=0;i<res.length;i++){
html += "<li>"+res[i]['fields']['name']+"</li><br>"
}
$('#content').html(html)
}
})
})
})
</script>
</html>