从 Django 视图上传图像
模型:
from django.db import models
class Image(models.Model):
advertisement = models.ForeignKey('Advertisement')
image = ImageWithThumbsField(upload_to=store_path, blank=True)
class Advertisement(models.Model):
#some code here
class AdvertOne(Advertisement):
#some code again
class AdvertTwo(Advertisement):
#and again
表单:
from models import Advertisement
from django.forms import *
class AdvertisementForm(ModelForm):
message = CharField(max_length=400, widget=Textarea)
image = ImageField(required=False)
问:我应该如何形成上传视图?
Models:
from django.db import models
class Image(models.Model):
advertisement = models.ForeignKey('Advertisement')
image = ImageWithThumbsField(upload_to=store_path, blank=True)
class Advertisement(models.Model):
#some code here
class AdvertOne(Advertisement):
#some code again
class AdvertTwo(Advertisement):
#and again
Forms:
from models import Advertisement
from django.forms import *
class AdvertisementForm(ModelForm):
message = CharField(max_length=400, widget=Textarea)
image = ImageField(required=False)
Q: How should I form an upload view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该有效。确保在表单标记中包含
enctype="multipart/form-data"
以便上传文件。Something like this should work. Make sure you inlcude
enctype="multipart/form-data"
in your form tag so the files get uploaded.