Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

django - How to embed a form multiple times with ListView (and once with DetailView)

I have the following listview and detailview structure. I would like to pass in a form to be shown with each object that is listed in the listview (and also with the one object when there is detailview). How can I get this to show up with each object?

class ObjectListView(LoginRequiredMixin, ListView):
    model = Object
    ...
    date = (datetime.now() - timedelta(hours = 7)).date()
    queryset = Object.objects.filter(date=date)

    def get(self, request, *args, **kwargs):
        ...
    return super().get(request, *args, **kwargs)

class ObjectDetailView(LoginRequiredMixin, DetailView):
    model = Object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to add the form in your template file for that view! i.e. if your template file is object_form.html you will have to render the form in the for loop. For example:

<ul>
{% for obj in object_list %}
<li>{{ obj.some_obj_field }}
<form>{# and in here render your form #}</form>
</li> 
{% endfor %}
</ul>

... and likewise in object_detail.html but without the for loop since you'll have one object


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...