Django:将实例跨内联表单转移到模板



>我有相关的模型 娱乐拼贴 与图像字段,现在不知道如何将实例模型转移到编辑表单。我需要在编辑表单中显示现有图像并能够添加新图像。

models.py

class Entertainment(models.Model):
main_photo = models.ImageField(upload_to = 'where/')
place = models.CharField(max_length=200)
description = models.CharField(max_length=200)
event_date = models.DateTimeField(auto_now_add=False,  blank=True, null = True) 
class EntertainmentCollage(models.Model):
img = models.ImageField(upload_to = 'entertainment/portfolio', blank = True)
album = models.ForeignKey(Entertainment, blank = True, null = True)

views.py

def edit_where(request, pk):
place = Entertainment.objects.get(id=pk)
FormSet2 = inlineformset_factory(Entertainment, EntertainmentCollage, fields =['img',], extra=6) 
form = WhereCreateForm(instance=place)
form2 = FormSet2()
if request.user.is_authenticated():
if request.method == "POST":
form = WhereCreateForm(request.POST, request.FILES, instance=place)
if form.is_valid():      
form2 = FormSet2(request.POST or None, request.FILES) 
if form2.is_valid():
form.save()  
form2.save()                    
return redirect('entertainment:where_list') 
else:
form = WhereCreateForm()
form2 = FormSet2() 
return render(request, "entertainment/where_edit.html", {'form': form, 'form2': form2})

.html

<section>
<div class="container">
<div class="row">
<div class="col-md-3">
<h2>Editing Where</h2>
<br>

<form method = "post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.eventname }}</p>
<p>{{ form.place }}</p>
<p>{{ form.event_date }}</p>
</div>
<div class="col-md-9">
<section class="admin-section">
{{ form2.management_form }} 
<div class="row">
{% for frm in form2 %}
<div class="col-md-4 admin__block" is-cover="false">
<div class="cover__wrapper edit__wrapper">
<a class="delete-button">Delete</a>
<a class="make-cover-button">Cover</a>
<img src="{{ frm.url }}" alt="">
</div>
</div>
{% endfor %}
</div>
<a href="#" class="button">Add photo</a>
</section>
<section>
<h4>Description</h4>
{{ form.description }}
<input type="submit" value="Save">
<a href="#" class="button">Cancel</a>
</section>
</form>
</div>
</div>
</div>
</section>

如果我尝试添加

collage = EntertainmentCollage.objects.filter(album = place)

到我的views.py并使form2(实例=拼贴(错误发生QuerySet'对象没有属性"pk">

___更新___

我需要像 http://joxi.ru/Dr8X8w4tkGw1zr 一样获得页面

其中图像取自模型娱乐拼贴, 如果添加了它们,那么我可以在表格中看到它们。

我意识到我的问题被问错了。我只需要显示查询集中的所有图像,并使用内联表单添加一个按钮来添加新图像。

最新更新