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.site.urls),
path(‘‘,include("web.urls")),
]
副urls:
from django.contrib import admin
from django.urls import path,re_path,include
from django.views.static import serve
from w999.settings import UPLOAD_ROOT
from django.views.generic import TemplateView
from . import views
urlpatterns = [
# path(‘admin/‘, admin.site.urls),
path(‘‘,views.Liu.as_view()),
path(‘addcate‘,views.AddCate.as_view()),
path(‘addcategory‘,views.AddCategory.as_view()),
path(‘show‘,views.Show.as_view()),
path(‘showdet‘,views.Showdet.as_view()),
path(‘submit_image‘,views.submit_image),
re_path(‘^upload/(?P<path>.*)$‘,serve,{‘document_root‘:UPLOAD_ROOT}),
]
models:
from django.db import models
# Create your models here.
class Cate(models.Model):
name = models.CharField(max_length=50)
class Meta:
db_table = ‘cate‘
def to_dict(self):
return{
‘name‘:self.name
}
class Category(models.Model):
cate = models.ForeignKey(Cate,on_delete=‘CASCADE‘)
way = models.CharField(max_length=50)
price = models.IntegerField()
create_time = models.DateTimeField(auto_now_add=True)
end_time = models.DateTimeField()
waydet = models.CharField(max_length=50)
t_img = models.TextField()
class Meta:
db_table = ‘category‘
def to_dict(self):
return {
‘id‘:self.id,
‘way‘:self.way,
‘price‘:float(self.price),
‘create_time‘:str(self.create_time),
‘end_time‘:str(self.end_time),
‘waydet‘:self.waydet,
‘t_img‘:self.t_img,
‘cate‘:self.cate.to_dict()
}
views:
from django.shortcuts import render,redirect
import json
from django.http import HttpResponse
from django.views import View
from . import models
from w999 import settings
import os
# Create your views here.
class Liu(View):
def get(self,request):
return HttpResponse(‘欢迎进入‘)
def uploadfile(img):
f = open(os.path.join(settings.UPLOAD_ROOT,‘‘,img.name),‘wb‘)
#获取文件流
for chunk in img.chunks():
f.write(chunk)
#关闭文件流
f.close()
#图文混排图片提交
def submit_image(request):
file = request.FILES.get("file")
uploadfile(file)
mes = {}
mes[‘path‘] = ‘/upload/‘+file.name
mes[‘error‘] = False
return HttpResponse(json.dumps(mes))
class AddCate(View):
def get(self,request):
return render(request,‘addcate.html‘)
def post(self,request):
name = request.POST.get(‘name‘)
addc=models.Cate(name=name)
addc.save()
return redirect(‘/addcate‘)
class AddCategory(View):
def get(self,request):
catelist = models.Cate.objects.all()
return render(request,‘addcategory.html‘,locals())
def post(self,request):
way = request.POST.get(‘way‘)
price = request.POST.get(‘price‘)
startname = request.POST.get(‘startname‘)
endname = request.POST.get(‘endname‘)
det = request.POST.get(‘det‘)
cid = request.POST.get(‘cid‘)
rich_content =request.POST.get(‘rich_content‘)
addc=models.Category(way=way,price=price,create_time=startname,end_time=endname,waydet=det,t_img=rich_content,cate_id=cid)
addc.save()
return redirect(‘/addcategory‘)
class Show(View):
def get(self,request):
catelist = models.Cate.objects.all()
return render(request,‘show.html‘,locals())
def post(self,request):
mes = {}
if request.method == ‘POST‘:
id_ = request.POST.get(‘id‘)
print(id_)
if id_:
category = models.Category.objects.filter(cate_id=id_).all()
categorylist = []
for i in category:
categorylist.append(i.to_dict())
mes[‘code‘] = 200
mes[‘categorylist‘] = categorylist
return HttpResponse(json.dumps(mes))
class Showdet(View):
def get(self,request):
id = request.GET.get(‘id‘)
print(id)
category = models.Category.objects.filter(id=id).all()
return render(request,‘showdet.html‘,locals())
html:
add_cate:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加分类</title>
</head>
<body>
<h1>
来!添加分类记录
</h1>
<form action="" method="post">
添加分类<input type="text" name="name">
<button type="submit">点我添加</button>
</form>
</body>
</html>
addcategory:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="/static/admin/js/jquery-1.12.4.min.js"></script>
<script src="/static/admin/tinymce/js/tinymce/tinymce.min.js"></script>
<script src="/static/admin/js/tinymce_setup.js"></script>
<title>添加二级分类</title>
</head>
<body>
<h1>
添加二级分类
</h1>
<form action="" method="post" enctype="multipart/form-data">
获取方式:<input type="text" name="way"><br>
面额:<input type="text" name="price"><br>
获取时间:<input type="datetime-local" name="startname"><br>
有效期:<input type="datetime-local" name="endname"><br>
获取详情:<input type="text" name="det"><br>
记录分类:<select name="cid">
{% for i in catelist %}
<option value="{{ i.id }}">{{ i.name }}</option>
{% endfor %}
</select>
图片上传:<div id="rich_content" name="rich_content"></div>
<button type="submit">点我添加</button>
</form>
</body>
</html>
show:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="/static/admin/js/jquery-1.12.4.min.js"></script>
<title>Document</title>
</head>
<body>
<table border="2">
{% for i in catelist %}
<tr>
<td rowspan="3">
<button type="button">
<a href="javascript:get_category({{ i.id }})">{{ i.name }}<a>
</button>
</td>
</tr>
{% endfor %}
</table>
<div class="category"></div>
</body>
<script type="text/javascript">
function get_category(id) {
$.ajax({
url:‘/show‘,
type:‘post‘,
dataType:‘json‘,
data:{‘id‘:id},
success:function (res) {
if(res.code == 200){
var mes = res.categorylist
var len = mes.length
var html = ‘<table border="2">‘
html += ‘<tr><td>获取方式</td><td>面额</td><td>获取时间</td><td>有效期至</td><td>获取详情</td></tr>‘
for(var i=0;i<len;i++){
html += ‘<tr><td>‘+mes[i][‘way‘]+‘</td><td>‘+mes[i][‘price‘]+‘</td><td>‘+mes[i][‘create_time‘]+‘</td><td>‘+mes[i][‘end_time‘]+‘</td><td><a href="/showdet?id=‘+ mes[i][‘id‘] +‘">‘+mes[i][‘waydet‘]+‘</a></td></tr>‘
}
html += ‘</table>‘
$(‘.category‘).html(html)
}
}
})
}
</script>
</html>
showdet:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
{% for i in category %}
<li>{{ i.t_img|safe }}</li>
{% endfor %}
</ul>
</body>
</html>
原文地址:https://www.cnblogs.com/lhrd/p/10914351.html