无法在 Django 上渲染 HTML 文件
Django 4.0.3 和 Python 3.10.2
我无法在我的项目中渲染 html 文件。我缺少什么?下面代码的主要部分。
INSTALLED_APPS 处的 Settings.py:TEMPLATES 处的
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
Settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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': [r'project1\app1\templates\app1'],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},
]
项目 URL:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app1.urls')), # main will be the name of your app
]
App1-Urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.simple_function, name='simple_function'),
]
视图:
def simple_function(request):
print("Print executes correctly, but the render doesn't")
return render(request, r'project1\app1\templates\app1\home.html')
Html 文件路径:app1/templates/app1/home.html
不得不发布这堆信息来澄清。
Django 4.0.3 and Python 3.10.2
I cant render a html file on my project. What am i missing? Main parts of code below.
Settings.py at INSTALLED_APPS:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
Settings.py at TEMPLATES:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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': [r'project1\app1\templates\app1'],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},
]
Project-Urls:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app1.urls')), # main will be the name of your app
]
App1-Urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.simple_function, name='simple_function'),
]
Views:
def simple_function(request):
print("Print executes correctly, but the render doesn't")
return render(request, r'project1\app1\templates\app1\home.html')
Html file path: app1/templates/app1/home.html
Had to post this bunch of information to clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
尝试写
instead of
try to write