Django:TemplateDoesNotExist

时间:2018-06-10 00:26:41

标签: django django-templates django-views

我试图加载模板,但它似乎一直在不存在的路径中搜索模板。我希望加载clincher/visit_form,但它一直在查看clincher/,似乎无法摆脱&#34; <td><a href="{% url 'clincher:visit_form' main.id %}" role="button" class="btn btn-primary btn-xs">New Consult</a></td>&#34;模板位置的一部分。它以前是以这种方式设置的,但是当我将模板位置移动到相应的应用程序时更改了它。

模板网址: from django.contrib import admin from django.urls import include, path from django.views.generic import RedirectView from clincher import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.index, name='index'), path('clincher/', include('clincher.urls'), name='clincher'), path('accounts/', include('accounts.urls')), path('accounts/', include('django.contrib.auth.urls')), path('', RedirectView.as_view(url='clincher/templates/')), ]

主/ Urls.py:

from django.conf.urls import url
from . import views
from django.urls import path

app_name = 'clincher'

urlpatterns = [
    path('', views.index, name='index'),
    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'),
]

夹钳/ Urls.py

class VisitCreate( CreateView):
    login_url = '/login/'
    redirect_field_name = 'index'
    model = Visit
    fields = [ 'visit_label', 'visit_type', 'visit_progress_notes']
    template = 'templates/visit_form.html'

    def form_valid(self, form):
        form.instance.fk_visit_user = self.request.user
        form.instance.fk_visit_main = Main.object.get(self.kwargs['pk'])
        return super(VisitCreate, self).form_valid(form)

    def get_success_url(self):
        return reverse('main_detail', args={'pk': self.object.id})

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

INSTALLED_APPS = [

    'clincher.apps.clincherConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
]

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Settings.py

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/clincher/visit/add/2

Django Version: 2.0.4
Python Version: 3.6.4
Installed Applications:
['clincher.apps.clincherConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'accounts']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

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)
django.template.loaders.app_directories.Loader: /Users/nrsmoll/PycharmProjects/clincher/accounts/templates/clincher/visit_form.html (Source does not exist)

Traceback:

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  156.                 response = response.render()

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/template/response.py" in render
  106.             self.content = self.rendered_content

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/template/response.py" in rendered_content
  81.         template = self.resolve_template(self.template_name)

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/template/response.py" in resolve_template
  63.             return select_template(template, using=self.using)

File "/Users/nrsmoll/venv/lib/python3.6/site-packages/django/template/loader.py" in select_template
   47.         raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)

Exception Type: TemplateDoesNotExist at /clincher/visit/add/2
Exception Value: clincher/visit_form.html

最后,我的追溯:

ds1[ds1.isin(filter(None, ds2))]

1    b
2    c
dtype: object

我所需要的只是摆脱&#34;紧要关头&#34; &#34; clincher / visit_form.html&#34;。

的一部分

2 个答案:

答案 0 :(得分:0)

您最有可能将template放在以下路径中:

.../PycharmProject/clincher/templates/visit_form.html

如果您在目录中添加另一个文件夹来存储模板文件,Djanog希望它们存储的方式应该有效。那条道路是:

.../PycharmProject/clincher/templates/clincher/visit_form.html

在您的网址中,Django会将您的应用名称放在前面。

如果您不想更改目录结构,可以按如下方式替换'DIRS'值{我在Mac上运行此功能 - 无法在PC上测试}:

# Current
'DIRS': [os.path.join(BASE_DIR, 'templates')],

# Fixed
'DIRS': ['templates'],

这应该尝试在您的应用下查找模板。话虽如此,这不是Django的设计方式。请参阅文档here。如果向下滚动一点,您将看到如何更改DIRS列表的几个示例。

#From the docs
'DIRS': [
    '/home/html/example.com',
    '/home/html/default',
],

但要注意 - 最好使用相对路径引用而不是硬编码路径。

答案 1 :(得分:0)

你的一般架构对我来说似乎有点不清楚/不整洁。

您的visit_form位于您的Clincher下,因此系统查找clincher / visit / add / pk是很正常的。此外,您还要将此网址指向您的html模板:

{% url 'clincher:visit_form' main.id %}"

。因此,您应该在&#34;模板中创建 visit_form.html 。然后,在clicnher下的文件夹。

在您的视图中,您可以输入更好的success_url,而不是 redirect_field_name =&#39; index&#39;

例如:

success_url = reverse_lazy('clincher:visit_form')

另外,您在url中的索引为空。 ?