Maximilian Kindshofer
Maximilian Kindshofer

Reputation: 2843

Getting the Name of the Model in a Django template using the Form tag

I want to Display the name of the model in my generic form template in Django. I thought about accessing the name via the form template tag. I know that I can easily hand over this information via the context dict - but I want to use the Information from the formclass since I already given the info in the ModelForm's meta.

Is there a way to access this information inside the template?

Update:

Since I can not access the value directly I assigned the form a name in the views.py:

def form_view(request):
    form = Form(request.POST or None)
    form.name = 'Name'
    ...
    render (request, 'generic_form_tempalte.html', {'form':form}

and in the template:

{% extends '__base.html' %}
<h1 class="page-header">{{form.name}}</h1>
...

This works - however it is one step more

Upvotes: 2

Views: 624

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600041

Not really. The model is available via form._meta.model, but you can't access underscore-prefix attributes in a template, unfortunately.

Upvotes: 5

Related Questions