Django:Url路由到错误的文件,但位置正确:TemplateDoesNotExist错误

时间:2018-06-06 10:00:17

标签: python django django-templates django-urls

我正在尝试显示名为visit-form.html的文件,但由于某些原因,Django一直在寻找名为visit_form.html的文件(区别在于下划线)。因此,我不断收到TemplateDoesNotExist错误。

我已经尝试清除浏览器缓存,使Pycharm缓存无效(文件>无效缓存)。我在整个程序中搜索了visit_form.html文件或代码,但没有找到(我在旧的文件路径中找到了一个并且没有更改就删除了文件)。

错误如下,第一个Url位于正确的文件位置,但问题似乎是我无法摆脱的文件名。

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`

然后:

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',
        ],
    },
},
]

visit-form(正确文件)的原始链接是:

Html模板

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

1 个答案:

答案 0 :(得分:1)

模板名称与网址格式中的名称无关。

您可以在视图中设置模板名称:

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

    template_name = 'clincher/visit-form.html'

但是,由于默认情况下the create view uses <app_name>/<model_name>_form.html(全部小写),因此最好将模板重命名为使用下划线而不是连字符。