Nicolas
Nicolas

Reputation: 444

Django: Url routing to the wrong file, but correct location: TemplateDoesNotExist Error

I am trying to display a file named visit-form.html, but for some reason Django keeps looking for a file named visit_form.html (the difference being the underscore). Therefore, I keep getting the TemplateDoesNotExist error.

I've tried clearing the browser cache, invalidating the Pycharm cache (File>invalidate caches). I have searched throughout the entire program for a visit_form.html file or code, but none can be found (I found one in an old file path and deleted the file with no change).

The error is as follows, and the first Url is in the correct file location, but the problem seems to be the filename I can't get rid of.

TemplateDoesNotExist at /clincher/visit/add/2
clincher/visit_form.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/clincher/visit/add/2
Django Version: 2.0.4
Exception Type: TemplateDoesNotExist
Exception Value:    clincher/visit_form.html
Exception Location: /Users/nrsmoll/venv/lib/python3.6/site-packages/django/template/loader.py in select_template, line 47
Python Executable:  /Users/nrsmoll/venv/bin/python
Python Version: 3.6.4
Python Path:    
['/Users/nrsmoll/PycharmProjects/clincher',
 '/Users/nrsmoll/PycharmProjects/clincher',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
 '/Users/nrsmoll/venv/lib/python3.6/site-packages',
 '/Users/nrsmoll/venv/lib/python3.6/site-packages/setuptools-28.8.0-py3.6.egg',
 '/Users/nrsmoll/venv/lib/python3.6/site-packages/pip-9.0.1-py3.6.egg',
 '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']
Server time:    Wed, 6 Jun 2018 09:20:19 +0000`

and then:

Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.filesystem.Loader: /Users/nrsmoll/PycharmProjects/clincher/templates/clincher/visit_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/nrsmoll/PycharmProjects/clincher/clincher/templates/clincher/visit_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/nrsmoll/venv/lib/python3.6/site- 
packages/django/contrib/admin/templates/clincher/visit_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/nrsmoll/venv/lib/python3.6/site- 
packages/django/contrib/auth/templates/clincher/visit_form.html (Source does not exist)

Urls.py

urlpatterns = [
    # /main/
   path('', views.index, name='index'),
    #/main/<main_id>/
    path('main/', views.MainListView.as_view(), name='main'),
    path('main/<int:pk>', views.MainDetailView.as_view(), name='main-detail'),
    path('visit/add/<int:pk>', views.VisitCreate.as_view(), name='visit-form'),
    url(r'main/add/$', views.MainCreate.as_view(), name='main-add'),
    #/main/main/2/
    url(r'^(?P<pk>[0-9]+)/$', views.MainUpdate.as_view(), name='main-update'),
    #/main/main/2/
    url(r'^(?P<pk>[0-9]+)/delete/$', views.MainDelete.as_view(), name='main-delete'),
]

Views.py

class VisitCreate(CreateView):
    model = Visit
    fields = ['fk_visit_main', 'visit_progress_notes']

    def get_success_url(self):
        return reverse('clincher:main-detail', args={'pk': self.object.id})

Settings.py

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

The original link to the visit-form (correct file) is:

Html Template

<td><a href="{% url 'clincher:visit-form' main.id %}"  role="button" class="btn btn-primary btn-xs">New Consult</a></td>

Upvotes: 0

Views: 338

Answers (1)

Alasdair
Alasdair

Reputation: 308849

The template name is unrelated to the name in your URL patterns.

You can set the template name in your view:

class VisitCreate(CreateView):
    model = Visit
    fields = ['fk_visit_main', 'visit_progress_notes']

    template_name = 'clincher/visit-form.html'

However, since the create view uses <app_name>/<model_name>_form.html (all lowercase) by default, it might be better to rename your template to use an underscore instead of a hyphen.

Upvotes: 1

Related Questions