我正在使用表格通過dropzone.js上傳圖片。圖片被保存並上傳正常。但是它創建了一個帶有空字段的新模型。我想將圖片保存到我參考的現有模型中。但我似乎無法找到引用它的解決方案。這裏是我的代碼:保存已上傳的圖像到exisitng Django中的模型
views.py(我猜的錯誤是在這裏)
def client_view(request, pk):
client = get_object_or_404(Client, pk=pk)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
picture = form.save()
else:
form = UploadFileForm()
data = {'form': form}
return render_to_response('client_view.html', locals(), RequestContext(request), {"img": picture})
return render(request, 'client_view.html', {'client': client})
urls.py
url(r'^client/(?P<pk>\d+)/$', client_view, name='client_view'),
forms.py
class UploadFileForm(forms.ModelForm):
class Meta:
model = Client
fields = ('image',)
models.py
class Client(models.Model):
customer_nr = models.IntegerField(null=True)
last_name = models.CharField(max_length=30, null=True)
first_name = models.CharField(max_length=30, null=True)
birthdate = models.DateField(null=True)
address = models.CharField(max_length=50, null=True)
image = models.ImageField("Image", upload_to='files/%Y/%m', null=True, blank=True)
client_view.html
<form action="{% url 'client_view' pk=client.pk %}" class="dropzone" id="myDropzone">
{% csrf_token %}
<div class="fallback">
<input name="file" type="file" multiple/>
</div>
</form>
<script type="text/javascript">
Dropzone.options.myDropzone = {
paramName: "image",
autoProcessQueue : true,
parallelUploads: 1,
init: function() {
this.on("success", function(file, responseText) {
console.log(responseText);
});
}
};
</script>
任何幫助將非常感激!謝謝!
也適用,謝謝! :) – Progph
@Progph [感謝評論部分](http://meta.stackexchange.com/a/126184) –