Django error: TemplateResponseMixin requires either a definition of ‘template_name’ or an implementation of ‘get_template_names()’

This is such a bizarre error message and I have spent hours trying to figure out what on earth I’ve been doing wrong. This is my situation: I have several models for which I am creating generic views for (ListView, DetailView, CreateView, UpdateView, DeleteView). These mostly contain postgis geographical data that I am storing as polygonfields and so on. To display these nicely in my forms I want to use the django-leaflet LeafletWidget. To do THIS I have to create a modelform. So for example my files look like:

models.py:

class Project(models.Model):
    current_name = models.CharField(max_length=50)
    location = models.PolygonField()
    objects = models.GeoManager()

views.py:

class ProjectCreate(CreateView):
    model = models.Project
    template_name_suffix = '_create_form'
    form_class = forms.ProjectCreateForm

forms.py:

class ProjectCreateForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = ('current_name', 'location')
        widgets = {'location': LeafletWidget()}

urls.py:

urlpatterns = [
    url(r'^project/create$', views.ProjectCreate.as_view(), name='project_create'),
]

now, if you leave out the model = x bit in the createview (which i think is perfectly reasonable as you have to specify in the modelform in the forms.py what model you want to use, then you get this error:

TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

Absolutely infuriating. I can’t believe how many hours it took for me to track this down. All because I left it yesterday and didn’t put the model = x bit back into the code when I removed it to see what happened. I also can’t believe I actually forgot about it though – senility is clearly on the horizon.

It is actually in the docs though – https://docs.djangoproject.com/en/1.8/topics/class-based-views/generic-editing/ scroll down to the first note inset. Lesson learned: always read the docs rather than googling for stackoverflow answers!

One thought on “Django error: TemplateResponseMixin requires either a definition of ‘template_name’ or an implementation of ‘get_template_names()’

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>