gpt4 book ai didi

django - TemplateDoesNotExist 在/django

转载 作者:行者123 更新时间:2023-12-03 18:27:27 25 4
gpt4 key购买 nike

出错了

enter image description here

views.py:

def index(request):
user = request.user
return render(request, 'main_app/index.html',{'user': user})

试过这个:

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

但没有帮助

指令:

Internal Server Error: /
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/workspace/main_app/views.py", line 10, in index
return render(request, 'main_app/index.html',{'user': user})
File "/usr/local/lib/python3.4/dist-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py", line 61, in render_to_string
template = get_template(template_name, using=using)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py", line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: main_app/index.html

文件结构: enter image description here

Django 2.0

设置.py

INSTALLED_APPS = [
# 'main_app.apps.MainAppConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main_app'

]

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

]

最佳答案

您应该将模板从 main_app/templates 移动到嵌套的子文件夹 main_app/templates/main_app

例如,在 django tutorial 中对此进行了解释

关于django - TemplateDoesNotExist 在/django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49909618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com