我使用Django 1.8.15和python 2.7.12。 我有一个模特:
class Page(models.Model):
title = models.CharField(max_length=255,)
description = models.TextField(blank=True,)
img = models.ImageField(upload_to="img/", blank=True, null=True)
...
def save(self, *args, **kwargs):
try:
this = Page.objects.get(id=self.id)
if this.img != self.img:
this.img.delete(save=False)
except: pass
super(Page, self).save(*args, **kwargs)
用于更新我的模型的ModelForm:
class PageForm(forms.ModelForm):
class Meta:
model = Page
fields = ('title', 'description', 'img')
视图:
def page_update_view(request, template, id):
mypage = get_object_or_404(Page, id=id)
context_dict = {}
form = PageForm(instance=mypage)
if request.method == "POST":
form = PageForm(request.POST, request.FILES or None, instance=mypage)
if form.is_valid():
form.save()
return redirect('page_update', id=id)
else:
print(form.errors)
context_dict["form"] = form
return render(request, template, context_dict)
模板中的表格:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="update" />
</form>
当我保存没有图像的表单时,它工作正常。但是当我上传图片时总会出现错误:
Exception Type: OSError
Exception Value: [Errno 22] Invalid argument
完整追溯:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\mysite\mysite\pages\views.py" in page_update_view
588. form.save()
File "C:\Python27\lib\site-packages\django\forms\models.py" in save
459. construct=False)
File "C:\Python27\lib\site-packages\django\forms\models.py" in save_instance
105. instance.save()
File "C:\mysite\mysite\pages\models.py" in save
215. super(Page, self).save(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
734. force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
762. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in _save_table
824. for f in non_pks]
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in pre_save
314. file.save(file.name, file, save=False)
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in save
93. self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "C:\Python27\lib\site-packages\django\core\files\storage.py" in save
63. name = self._save(name, content)
File "C:\Python27\lib\site-packages\django\core\files\storage.py" in _save
248. fd = os.open(full_path, flags, 0o666)
任何人都可以帮助弄清楚它有什么问题。它不仅适用于Windows,在Linux上运行良好。
答案 0 :(得分:1)
您尝试将图像上传到img/
,这不是有效的Windows路径(反斜杠分隔符)。如果您希望它在Linux和Windows中都能运行,您可以执行以下操作:
import os
def get_image_path(instance, filename):
return os.path.join("img", filename)
class Page(models.Model):
...
img = models.ImageField(upload_to=get_image_path, blank=True, null=True)
...