eso
eso

Reputation: 103

Django FormView and List of results

Can i pass a model in FormView?

class TodoView(FormView):
    model = Branch
    queryset= Branch.objects.all()
    form_class = FormTodoAdd
    ...

How can i list the results of Branch in my Template?

{% for branch in object_list %} ## don't work
{% for branch in queryset %} ## don't work
{{ branch }}
{% endfor %}

Upvotes: 0

Views: 443

Answers (1)

Ralf
Ralf

Reputation: 16505

Just because you define model attributes on a class doesn't mean that they get passed to the template context.

Your best bet is to pass the variables explicitly to the context, maybe with this:

class TodoView(FormView):
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)

        ctx['model'] = Branch

        return ctx

There may be other ways, but this is the simplest one I can think of.

Upvotes: 1

Related Questions