Django多图上传
Django multiple image upload
我试图让用户为每个项目上传多张图片。 Django 文档 enter link description here 展示了如何在 generell 中执行此操作,但我认为我呈现我的表单的方式与他们所做的不同。所以我不知道如何在我的输入字段中添加 'multiple' 属性。此外,他们在 views.py 和函数内部还有一个额外的 class。
views.py
def createProject(request):
form = ProjectForm()
if request.method == 'POST':
form = ProjectForm(request.POST, request.FILES)
if form.is_valid():
project = form.save(commit=False)
project.save()
context = {'form':form}
return render(request, 'projects/project_form.html', context)
models.py
class Project(models.Model):
title = models.CharField(max_length=200)
featured_images = models.ImageField(null=True, blank=True, default="default.jpg")
forms.py
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = ['title', 'featured_images']
project_form.html 或模板文件
<form class="form" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
<div class="form__field">
<label for="formInput#text">{{field.label}}</label>
{{field}}
</div>
{% endfor %}
</form>
首先您必须稍微调整您的表单以允许多次上传:
# forms.py
class ProjectForm(ModelForm):
class Meta:
featured_images = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
model = Project
fields = ['title', 'featured_images']
然后在视图中你必须抓取多个条目:
# views.py
def createProject(request):
form = ProjectForm()
if request.method == 'POST':
form = ProjectForm(request.POST, request.FILES.getlist('featured_images'))
if form.is_valid():
project = form.save(commit=False)
project.save()
context = {'form':form}
return render(request, 'projects/project_form.html', context)
如果可行,请告诉我。
在 Django 中有多种方法可以将多个图像关联到一个字段。我喜欢做的一种方式是:
- 首先创建两个模型,一个用于您想要的东西(在本例中为项目),另一个用于图像。
class MyModel(models.Model):
field = models.CharField(max_lenght=255)
class MyModelImage(models.Model):
image = models.ImageField(upload_to="/where_you_want_to")
field = models.ForeignKey(MyModel,related_name="images",on_delete=models.CASECADE)
- 然后使用 Django 内联表单集,您可以一次添加多个图像。
希望对您有所帮助
编辑:
# this is how you model might look
class ParentModel(models.Model):
# your model
pass
class ImageModel(models.Model):
image = models.ImageField(upload_to="your_path/")
parent = models.ForeignKey(ParentModel,on_delete=models.CASECADE,related_name="images")
# this is for view
# create your custom inlineforset form
# with first argument is your model with which you want to associate multiple images and second is your image model
CustomInlineForm = inlineformset_factory(ParentModel, ImageModel)
# use this custom inline form like this
formset = CustomInlineForm(request.FILES or None, instance=your_instance)
# optionally you can also get all your image using related name also like this:
parent_instance.images
我试图让用户为每个项目上传多张图片。 Django 文档 enter link description here 展示了如何在 generell 中执行此操作,但我认为我呈现我的表单的方式与他们所做的不同。所以我不知道如何在我的输入字段中添加 'multiple' 属性。此外,他们在 views.py 和函数内部还有一个额外的 class。
views.py
def createProject(request):
form = ProjectForm()
if request.method == 'POST':
form = ProjectForm(request.POST, request.FILES)
if form.is_valid():
project = form.save(commit=False)
project.save()
context = {'form':form}
return render(request, 'projects/project_form.html', context)
models.py
class Project(models.Model):
title = models.CharField(max_length=200)
featured_images = models.ImageField(null=True, blank=True, default="default.jpg")
forms.py
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = ['title', 'featured_images']
project_form.html 或模板文件
<form class="form" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
<div class="form__field">
<label for="formInput#text">{{field.label}}</label>
{{field}}
</div>
{% endfor %}
</form>
首先您必须稍微调整您的表单以允许多次上传:
# forms.py
class ProjectForm(ModelForm):
class Meta:
featured_images = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
model = Project
fields = ['title', 'featured_images']
然后在视图中你必须抓取多个条目:
# views.py
def createProject(request):
form = ProjectForm()
if request.method == 'POST':
form = ProjectForm(request.POST, request.FILES.getlist('featured_images'))
if form.is_valid():
project = form.save(commit=False)
project.save()
context = {'form':form}
return render(request, 'projects/project_form.html', context)
如果可行,请告诉我。
在 Django 中有多种方法可以将多个图像关联到一个字段。我喜欢做的一种方式是:
- 首先创建两个模型,一个用于您想要的东西(在本例中为项目),另一个用于图像。
class MyModel(models.Model):
field = models.CharField(max_lenght=255)
class MyModelImage(models.Model):
image = models.ImageField(upload_to="/where_you_want_to")
field = models.ForeignKey(MyModel,related_name="images",on_delete=models.CASECADE)
- 然后使用 Django 内联表单集,您可以一次添加多个图像。
希望对您有所帮助
编辑:
# this is how you model might look
class ParentModel(models.Model):
# your model
pass
class ImageModel(models.Model):
image = models.ImageField(upload_to="your_path/")
parent = models.ForeignKey(ParentModel,on_delete=models.CASECADE,related_name="images")
# this is for view
# create your custom inlineforset form
# with first argument is your model with which you want to associate multiple images and second is your image model
CustomInlineForm = inlineformset_factory(ParentModel, ImageModel)
# use this custom inline form like this
formset = CustomInlineForm(request.FILES or None, instance=your_instance)
# optionally you can also get all your image using related name also like this:
parent_instance.images