无法在 Django 上渲染 HTML 文件

发布于 2025-01-11 16:37:48 字数 1716 浏览 0 评论 0原文

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

这个项目的 Github

不得不发布这堆信息来澄清。

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

Github of this project

Had to post this bunch of information to clarify.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2025-01-18 16:37:48

而不是

return render(request, r'project1\app1\templates\app1\home.html')

尝试写

return render(request, 'app1/home.html')

instead of

return render(request, r'project1\app1\templates\app1\home.html')

try to write

return render(request, 'app1/home.html')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文