Django Forms 上传图像 - 除图像之外的表单数据均有效
我正在尝试使用 Django 进行上传。当我尝试上传它时没有收到错误,并且新项目已保存,但没有图像。其他表格数据都在那里。过去两天我一直在谷歌上搜索堆栈溢出,但找不到修复方法。我也不知道哪里出了问题!任何建议都会很棒。非常感谢。 :)
来自views.py:
if request.method == 'POST':
form = ItemForm(request.POST, request.FILES, user=request.user, forsale=True)
if form.is_valid():
new_item = form.save()
return item_view(request, marketplace_id, new_item.id)
else:
form = ItemForm(user=request.user)
来自models.py:
class Item(models.Model):
photo = StdImageField(upload_to='images/', blank=True, size=(500, 500, True), thumbnail_size=(210, 150, True))
user = models.ForeignKey(User)
...
def __unicode__(self):
return self.name
class ItemForm(ModelForm):
class Meta:
model = Item
fields = ['name', 'description', 'price', 'shipping', 'photo', 'section']
def __init__(self, *args, **kwargs):
self._user = kwargs.pop('user')
super(ItemForm, self).__init__(*args, **kwargs)
来自模板:
<form enctype="multipart/form-data" action="" method="post" data-ajax="false">{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper" data-role="fieldcontain">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<input type="submit" value="Save">
</form>
据我所知,模板是正确的。 Item 模型看起来不错,StdImageField 是 ImageField 的包装器。它在管理面板中运行良好。 表格看起来也不错,没什么特别的。 在我的views.py中,我检查POST请求,调用ItemForm并传入request.POST和request.FILES(以及forsale,这是一个布尔值)。我检查表格是否有效,如果有效,我会保存它。为什么它不起作用?该项目已保存,但在管理面板中没有与“照片”关联的文件。
I'm trying to get uploading working with Django. I don't get an error when I try to upload it, and the new Item is saved, but there is no image. The other form data is all there. I've been googling and on stack overflow for the past 2 days and cannot find a fix. I don't even know where it's going wrong! Any advice would be amazing. Thanks so much. :)
from views.py:
if request.method == 'POST':
form = ItemForm(request.POST, request.FILES, user=request.user, forsale=True)
if form.is_valid():
new_item = form.save()
return item_view(request, marketplace_id, new_item.id)
else:
form = ItemForm(user=request.user)
from models.py:
class Item(models.Model):
photo = StdImageField(upload_to='images/', blank=True, size=(500, 500, True), thumbnail_size=(210, 150, True))
user = models.ForeignKey(User)
...
def __unicode__(self):
return self.name
class ItemForm(ModelForm):
class Meta:
model = Item
fields = ['name', 'description', 'price', 'shipping', 'photo', 'section']
def __init__(self, *args, **kwargs):
self._user = kwargs.pop('user')
super(ItemForm, self).__init__(*args, **kwargs)
from the template:
<form enctype="multipart/form-data" action="" method="post" data-ajax="false">{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper" data-role="fieldcontain">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<input type="submit" value="Save">
</form>
As I see it, the template is correct. The Item model seems fine, StdImageField is a wrapper for ImageField. It works fine in the admin panel.
The Form seems fine as well, nothing special.
In my views.py fine, I check for a POST request, call the ItemForm and pass in request.POST and request.FILES (and forsale which is a boolean). I check if the form is valid, if so, I save it. Why isn't it working? The Item saves but in the admin panel there is no file associated with 'photo'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查与“images/”连接的 MEDIA_ROOT 设置是否是 Django 进程的有效且可写的路径。
使用 pdb 还
可以帮助您查找问题(请查看 request.FILES 以查看发布过程中是否一切正常)。
Check that MEDIA_ROOT setting joined with "images/" is a valid and writable path for Django process.
Also using pdb
can help you in the process of finding the issue, (f.i. have a look at request.FILES to see if everything looks ok there during post).