django 王中王9之获得记录

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

时间: 2024-10-20 01:45:08

django 王中王9之获得记录的相关文章

django 王中王8之踏青撒花

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 adminfrom django.urls import path,re_path,includefrom django.views.static import

django 王中王5之化妆品

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 adminfrom django.urls import pathfrom web import views urlpatterns = [ path('add_go

django 王中王7之牛大头

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 adminfrom django.urls import path,re_pathfrom django.views.static import servefrom l

django 王中王10之游记

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 adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.

django 王中王3之优惠卷

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.

.15-浅析webpack源码之WebpackOptionsApply模块之插件王中王

总体过了一下后面的流程,发现Compiler模块确实不适合单独讲解,这里继续讲解后面的代码: compiler.options = new WebpackOptionsApply().process(options, compiler); 这行代码与之前设置options默认值非常相似,但是复杂程度根本不是一个次元的. 这一节只能简单的看一眼内部到底有多少东西,整理后源码如下: "use strict"; const OptionsApply = require("./Opt

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀

django orm中 get 与filter的区别

django orm中 get 与filter的区别: 以前误以为get与filter的区别仅仅是filter返回多条,而get仅支持一条记录,直到使用related_name属性 才发现,两者的差异远不止如此,最本质的区别是 filter返回的值是一个queryset对象, 而get 返回值是一个定义的model对象, 使用get 返回的结果去访问子表对象可以成功,而使用filter就会出错, 是因为queryset 对象没有realtaed_name属性,无法回溯到子表

[Django]orm中的外键级联删除

这里的系统环境为django1.6   os为win7 今天有个需求说的是添加一个地区表,然后用外键和几个非常重要的实体表来做关联(地区表作为其他表的外键),写完地区的删除操作的时候,测试了下代码,功能正常.可是眼睛移动到控制台输出的时候傻了 connexesql ->DELETE FROM `mngm_device` WHERE `mngm_device`.`area_id` IN (%s, %s, %s) connexesql ->DELETE FROM `ad_ad` WHERE `ad