如何解决我的第一个Django应用-如何导入模板?
我正在django 3.1.1中创建我的第一个django应用。有一些针对旧django的视频教程,但它们并不总是有效...我想为首页和关于本节创建HTML页面。我已经写了一些HTML文件,但是
def home(request):
return render(request,'home.html')
不想工作。我添加了文件树以供您查看文件的结构。
RemoveBigFile
├── RBF1module
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── RemoveBigFile
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ ├── views.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── RemoveBigFile.sublime-project
├── RemoveBigFile.sublime-workspace
├── db.sqlite3
├── manage.py
└── templates
├── about.html
└── home.html
这就是我收到的错误消息:
TemplateDoesNotExist at /
home.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.1
Exception Type: TemplateDoesNotExist
Exception Value:
home.html
Django还要求我将模板放在django的主要安装目录之一(称为模板)中,据我所知,如果这样做,我将无法将我的应用程序发送给其他人(这就是我的目的)我打算在完成后处理它)。 我使用我的RemoveBigFile / RemoveBigFile views.py将django指向HTML模板。
编辑:根据要求,我从settings.py添加我的模板定义
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'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中没有任何内容。我应该在DIRS括号中放置模板的路径吗?
我还有一个问题-将模板放在manage.py或settings.py所在的文件夹中更好吗?
解决方法
好吧,您自己的灵感就找到了它:) 谢谢@Selcuk和@ m.arthur。感谢您也贡献了@Mahmoud Ishag:)
答案在于,我一定不是将这个应用程序创建为项目,并且模板中缺少短字符串:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [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': [BASE_DIR / 'templates']
请比较我在主要文章中的代码。
, Django在名为AttributeError: 'module' object has no attribute 'decrypt_pwd'
的文件夹中查找模板,您需要在app文件夹中创建该文件夹并将try to import dotenv variable "variablename" which is not defined in any .env files/
放在其中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。