当我尝试从表单发送数据到视图时,所有数据都成功发送,除了FileField Data。我正在使用模板的CURD数据表屏幕截图。 我也使用ajax。 我是Django的初学者,请帮助我找不到错误。
屏幕截图
view.py
def list(request):
certificatedata = certificateDb.objects.all()
context = {
'certificatedata': certificatedata
}
return render(request, 'list.html',context)
def save_all(request,form,template_name):
data = dict()
if request.method == 'POST':
if form.is_valid():
form.save()
data['form_is_valid'] = True
certificatedata = certificateDb.objects.all()
data['list'] = render_to_string('list_2.html',{'certificatedata':certificatedata})
else:
data['form_is_valid'] = False
context = {
'form':form
}
data['html_form'] = render_to_string(template_name,context,request=request)
return JsonResponse(data)
def certificate_create(request):
if request.method == 'POST':
form = CertificateForm(request.POST, request.FILES )
print('request.FILES',request.FILES)
else:
form = CertificateForm()
return save_all(request,form,'certificate_create.html')
form.py
from django import forms
from SharedApps_Application.models import serviceDb
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField
class CertificateForm(forms.ModelForm):
startdate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
expiredate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
class Meta:
model = certificateDb
fields = ('application', 'startdate', 'expiredate', 'environment_type','file' )
urls.py
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
urlpatterns=[
url(r'^list/$', views.list, name='list'),
url(r'^list/create$', views.certificate_create, name='certificate_create'),
url(r'^list/(?P<id>\d+)/update$', views.certificate_update, name='certificate_update'),
url(r'^list/(?P<id>\d+)/delete$', views.certificate_delete, name='certificate_delete'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Create_certifcate.html
{% load crispy_forms_tags %}
<form method="post" enctype="multipart/form-data" data-url="{% url 'certificate_create' %}" class="create-form" >
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title" >Create certificate</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{form|crispy}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Certificate</button>
</div>
</form>
certificate.js
$(document).ready(function(){
var ShowForm = function(){
var btn = $(this);
$.ajax({
url: btn.attr("data-url"),
type: 'get',
dataType:'json',
beforeSend: function(){
$('#modal-certificate').modal('show');
},
success: function(data){
$('#modal-certificate .modal-content').html(data.html_form);
}
});
};
var SaveForm = function(){
var form = $(this);
$.ajax({
url: form.attr('data-url'),
data: form.serialize(),
type: form.attr('method'),
dataType: 'json',
success: function(data){
if(data.form_is_valid){
$('#Certificate-table tbody').html(data.list);
$('#modal-certificate').modal('hide');
jQuery("body").load(window.location.href);
} else {
$('#modal-certificate .modal-content').html(data.html_form)
jQuery("body").load(window.location.href);
}
}
})
return false;
}
// create
$(".show-form").click(ShowForm);
$("#modal-certificate").on("submit",".create-form",SaveForm);
//update
$('#Certificate-table').on("click",".show-form-update",ShowForm);
$('#modal-certificate').on("submit",".update-form",SaveForm)
//delete
$('#Certificate-table').on("click",".show-form-delete",ShowForm);
$("#modal-certificate").on("submit",".delete-form",SaveForm);
});