Django 窗体 ImageField
Django form ImageField
我正在尝试为包含 ImageField 的模型建立 CreateView。我可以从 django 管理页面成功上传和显示图像。但是,当我从自己的表单上传图片时,django 不会将图片上传到 "upload_to" 文件夹。我在下面写我的代码:
models.py
from django.db import models
class Album(models.Model):
title = models.CharField(max_length=127)
artist = models.CharField(max_length=63)
release_date = models.DateField()
logo = models.ImageField(blank=True, upload_to='album_logos', default='album_logos/no-image.jpg')
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Album
class AlbumCreateForm(forms.ModelForm):
class Meta:
model = Album
fields = [
'title',
'artist',
'release_date',
'logo'
]
views.py
class AlbumCreateView(CreateView):
form_class = AlbumCreateForm
template_name = 'music/album_create.html'
success_url = '/albums/'
album_create.html
{% extends 'base.html' %}
{% block content %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</form>
{% endblock %}
当我尝试使用 "album_create.html" 创建相册并使用 Django 的默认格式上传图像时,徽标图像不会上传到 "album_logos" 文件夹并采用默认值。我哪里做错了?
这可能对您有所帮助
from django.utils.safestring import mark_safe
class PictureWidget(forms.widgets.Widget):
def render(self, name, value, attrs=None):
html = Template("""<img src="$link"/>""")
return mark_safe(html.substitute(link=value)
class AlbumCreateForm(forms.ModelForm):
logo = ImageField(widget=PictureWidget)
class Meta:
model = Album
fields = [
'title',
'artist',
'release_date',
'logo'
]
并在表单标签中添加添加此 属性
enctype="multipart/form-data"
我按照 django 文档中的说明,通过为 "form" 标记指定 "enctype" 属性来更改 album_create.html,我的问题已解决。
文档
forms.py
from django import forms
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
处理此表单的视图将接收 request.FILES 中的文件数据,这是一个包含表单中每个 FileField(或 ImageField,或其他 FileField 子类)键的字典。因此,上述表格中的数据可以作为 request.FILES['file'] 访问。
请注意,如果请求方法是 POST 并且发布请求 具有属性 enctype="multipart/form-data",则 request.FILES 将仅包含数据。否则,request.FILES将为空。
已更新album_create.html
{% extends 'base.html' %}
{% block content %}
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</form>
{% endblock %}
我正在尝试为包含 ImageField 的模型建立 CreateView。我可以从 django 管理页面成功上传和显示图像。但是,当我从自己的表单上传图片时,django 不会将图片上传到 "upload_to" 文件夹。我在下面写我的代码:
models.py
from django.db import models
class Album(models.Model):
title = models.CharField(max_length=127)
artist = models.CharField(max_length=63)
release_date = models.DateField()
logo = models.ImageField(blank=True, upload_to='album_logos', default='album_logos/no-image.jpg')
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Album
class AlbumCreateForm(forms.ModelForm):
class Meta:
model = Album
fields = [
'title',
'artist',
'release_date',
'logo'
]
views.py
class AlbumCreateView(CreateView):
form_class = AlbumCreateForm
template_name = 'music/album_create.html'
success_url = '/albums/'
album_create.html
{% extends 'base.html' %}
{% block content %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</form>
{% endblock %}
当我尝试使用 "album_create.html" 创建相册并使用 Django 的默认格式上传图像时,徽标图像不会上传到 "album_logos" 文件夹并采用默认值。我哪里做错了?
这可能对您有所帮助
from django.utils.safestring import mark_safe
class PictureWidget(forms.widgets.Widget):
def render(self, name, value, attrs=None):
html = Template("""<img src="$link"/>""")
return mark_safe(html.substitute(link=value)
class AlbumCreateForm(forms.ModelForm):
logo = ImageField(widget=PictureWidget)
class Meta:
model = Album
fields = [
'title',
'artist',
'release_date',
'logo'
]
并在表单标签中添加添加此 属性
enctype="multipart/form-data"
我按照 django 文档中的说明,通过为 "form" 标记指定 "enctype" 属性来更改 album_create.html,我的问题已解决。
文档
forms.py
from django import forms
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
处理此表单的视图将接收 request.FILES 中的文件数据,这是一个包含表单中每个 FileField(或 ImageField,或其他 FileField 子类)键的字典。因此,上述表格中的数据可以作为 request.FILES['file'] 访问。
请注意,如果请求方法是 POST 并且发布请求 具有属性 enctype="multipart/form-data",则 request.FILES 将仅包含数据。否则,request.FILES将为空。
已更新album_create.html
{% extends 'base.html' %}
{% block content %}
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create</button>
</form>
{% endblock %}