如何配置我的django项目:模板不存在错误?



我是Django框架的新手,试图通过youtube教程来更好地理解。我创建了一个简单的博客文章网站,在我尝试分别更新urls.py、views.py和home.html文件之前,它一直工作正常。现在,当我在本地机器上测试URL时,我返回了一个500的内部服务器错误,我不明白。我相信我的项目配置正确,因为我在settings.py文件中配置了所有变量。我已经在

中添加了我的博客名称
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'**DrewThe1stBlog**',

], ROOT_URLCONF = 'DrewsBlog.urls":url配置谁能告诉我我做错了什么?

我已经尝试调试代码,但我无法理解堆栈跟踪。我试过将我的python路径添加到Templates_DIR中,但这些都不起作用。

加如下:

Environment:
> 
> Request Method: GET
> Request URL: http://localhost:8000/
> 
> Django Version: 4.1.7
> Python Version: 3.8.5
> Installed Applications:
> ['django.contrib.admin',
>  'django.contrib.auth',
>  'django.contrib.contenttypes',
>  'django.contrib.sessions',
>  'django.contrib.messages',
>  'django.contrib.staticfiles',
>  'DrewThe1stBlog']
> 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']
> 
> Template loader postmortem
> Django tried loading these templates, in this order:
> 
> Using engine django:
> django.template.loaders.filesystem.Loader: /Users/drewcampbell/PersonalWebsitePython/DrewsBlog/DrewThe1stBlog/templates/DrewThe1stBlog/post_list.html/DrewThe1stBlog/post_list.html (Source does not exist)
> django.template.loaders.app_directories.Loader: /Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/contrib/admin/templates/DrewThe1stBlog/post_list.html (Source does not exist)
> django.template.loaders.app_directories.Loader: /Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/contrib/auth/templates/DrewThe1stBlog/post_list.html (Source does not exist)
> django.template.loaders.app_directories.Loader: /Users/drewcampbell/PersonalWebsitePython/DrewsBlog/DrewThe1stBlog/templates/DrewThe1stBlog/post_list.html (Source does not exist)
> 
> 
> 
> Traceback (most recent call last):
> File "/Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/core/handlers/exception.py", line 56, in inner
> response = get_response(request)
> File "/Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/core/handlers/base.py", line 220, in _get_response
> response = response.render()
> File "/Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/template/response.py", line 114, in render
> self.content = self.rendered_content
> File "/Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/template/response.py", line 90, in rendered_content
> template = self.resolve_template(self.template_name)
> File "/Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/template/response.py", line 72, in resolve_template
> return select_template(template, using=self.using)
> File "/Users/drewcampbell/PersonalWebsitePython/virtual/lib/python3.8/site-packages/django/template/loader.py", line 47, in select_template
> raise TemplateDoesNotExist(", ".join(template_name_list), chain=chain)
> 
> Exception Type: TemplateDoesNotExist at /
> Exception Value: DrewThe1stBlog/post_list.html
> 

请参阅附件中的python文件截图

Settings.pyurls.pyviews.pyhome.html

您看到的错误是" TemplateDoesNotExist"错误,这意味着Django找不到你试图在views.py文件中使用的模板文件。根据您提供的堆栈跟踪,缺少的模板文件是DrewThe1stBlog/post_list.html。

这里有一些你可以检查的东西来解决这个错误:

  1. 确保settings.py文件中的TEMPLATES设置是正确配置的。这个设置应该包括Django用来呈现模板的模板引擎列表,以及Django用来查找模板文件的模板目录列表。下面是一个例子:

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

这个例子将DIRS选项设置为包含一个名为'templates'的目录,该目录位于项目的基本目录中。如果模板文件位于不同的目录中,则需要将该目录添加到DIRS列表中。

  1. 确保模板文件存在于正确的位置。在您提供的堆栈跟踪中,Django正在查找/Users/drewcampbell/PersonalWebsitePython/DrewsBlog/DrewThe1stBlog/templates/DrewThe1stBlog/post_list.html处的模板文件,但是没有找到它。仔细检查文件是否存在于该位置,以及文件名是否拼写正确。

  2. 确保在views.py文件中将正确的模板名称传递给渲染方法。在get方法中,应该有这样一行:

return render(request, 'DrewThe1stBlog/post_list.html', {'posts': posts})

确保渲染方法的第一个参数是模板文件的正确名称。

希望这些建议能帮助你解决"TemplateDoesNotExist"你看到的错误

相关内容

  • 没有找到相关文章

最新更新