form表单代码段如下
<form class="form-horizontal form-material" >
<span>添加卡牌</span>
<div class="form-group" style="margin-top: 10px">
<label class="col-md-12" style="font-size: 20px">卡牌名</label>
<div class="col-md-12">
<input id="c_cid" type="text" placeholder="不超过16个字" class="form-control form-control-line">
</div>
</div>
<div class="form-group" style="margin-top: 10px">
<label class="col-md-12" style="font-size: 20px">卡牌图案</label>
<div class="col-md-12">
<input id="c_photo" type="file" accept = "image/*">
</div>
</div>
<div class="form-group" style="margin-top: 10px">
<label class="col-md-12" style="font-size: 20px">卡牌功能</label>
<div class="col-md-12">
<input id="c_content" type="text" placeholder="卡牌实战功能" class="form-control form-control-line">
</div>
</div>
<div class="form-group" style="margin-top: 10px">
<label class="col-md-12" style="font-size: 20px">卡牌背景</label>
<div class="col-md-12">
<input id="c_back" type="text" placeholder="卡牌背景故事" class="form-control form-control-line">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button id="publishk" class="btn btn-success" >添加</button>
</div>
</div>
</form>
form表达的js代码:
function deletek(ths) {
var c_cid = $(ths).attr("iddd");
$.ajax({
url: window.location.href,
type: "POST",
data: {'type': 'delete', 'c_cid': c_cid},
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}"); // csrf
},
success: function (data) {
// data是服务器端返回的字符串
var dic = JSON.parse(data)
if (!dic.status) alert(JSON.parse(data).msg);
else window.location.href = "/card-manage";
}
});
}
$('#publishk').click(function () {
var c_cid = $('#c_cid').val();
$.ajax({
url: window.location.href,
type: "POST",
data: {'type': 'create',
'c_cid': c_cid,
'c_photo': this.c_photo,
'c_back': this.c_back,
'c_content': this.c_content},
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}"); // csrf
},
success: function (data) {
// data是服务器端返回的字符串
var dic = JSON.parse(data);
if (!dic.status) alert(JSON.parse(data).msg);
else window.location.href = "/card-manage";
}
});
});
django代码:
def card_manage(request):
# 验证登录
if not request.session.get('admin_uid'):
return redirect('/my-admin')
if request.method == 'GET':
# get返回所有卡牌
cards = models.Card_List.objects.filter()
response = {
'cards': cards,
}
return render(request, 'card-manage.html', response)
if request.method == 'POST':
p_type = request.POST.get('type')
response = {'msg': '', 'status': False}
# 删除卡牌
if p_type == 'delete':
c_cid = request.POST.get('c_cid')
models.Card_List.objects.filter(id=c_cid).delete()
response['status'] = True
# 添加卡牌
if p_type == 'create':
c_cid = request.POST.get('c_cid')
c_content = request.POST.get('c_content')
c_back = request.POST.get('c_back')
models.Card_List.objects.create(c_cid=c_cid,c_content = c_content,c_back = c_back)
response['status'] = True
# 存卡牌图片
c_photo = request.FILES.get('c_photo', None)
c_photo_path = 'static/img/c_photo/' + str(c_cid) + '_' + c_photo.name
if c_photo:
# 保存文件
import os
f = open(os.path.join(c_photo_path), 'wb')
for line in c_photo.chunks():
f.write(line)
f.close()
# 吧图片路径存入数据库
models.Card_List.objects.filter(id=c_cid).update(c_photo='/' + c_photo_path)
return HttpResponse(json.dumps(response))
程序是个博客系统,想给管理员添加个功能出了问题,每次运行以后页面成功显示,就是form表单的数据存不到数据库里。数据库用的MySQL8,python3.7.。
你调试一下,看看views中进入的是GET还是PSOT,如果是POST,看看是否获取到了前端返回的数据