我正在使用 jquery 表单插件http://jquery.malsup.com/form/#getting-started通过 ajax 上传图片。当我尝试上传时,它显示模型没有属性 _committed 这是我的 Html:
<form id="uploadform" method="post" enctype="multipart/form-data" action="/cover/">{% csrf_token %}
{{ form.background }}
</form>
Javascript:
https://gist.github.com/2381406
模型.py:
class BackgroundModel(models.Model):
user = models.OneToOneField(User)
background = models.ImageField(upload_to='backgrounds')
class BackgroundModelForm(ModelForm):
class Meta:
model = BackgroundModel
exclude = ('user',)
视图.py:
@login_required
def backgroundview(request):
if request.is_ajax():
form = BackgroundModelForm(request.POST, request.FILES)
if form.is_valid():
try:
g = BackgroundModel.objects.get(user=request.user)
except BackgroundModel.DoesNotExist:
data = form.save(commit=False)
data.user = request.user
data.save()
else:
g.background = form
g.save()
HttpResponse(" ")
else:
form = BackgroundModelForm()
return render_to_response("cover.html", {'form': form}, context_instance=RequestContext(request))
网址.py:
url(r'^cover/$', 'cover.views.backgroundview'),