python - 有关 Django 模板的问题

标签 python django django-templates django-views

我正在开发一个用于用户注册和登录的应用程序。我已经制作了注册表单,现在我想要制作一个登录页面,因此我制作了一个login.html文件,现在我希望它是放置在模板中。 因此,我创建一个目录并将其放置在该目录中 home/html/templates/registration/login.html

settings.py文件中的Template_dir=()如下:

    TEMPLATE_DIRS = (
        '/home/html/templates',
    )

查看文件为

from django.template import  loader
from registration.models import Registration
from django.http import HttpResponse

def login(request):
    t = loader.get_template('registration/login.html')
    return HttpResponse()

但是当我尝试将此文件作为 localhost:8000/registration/login.html 运行时,我收到 404 错误找不到页面

url.py文件中给出的url如下:

url(r'^registration/$', 'registration.views.login'),

最佳答案

Django 本身不提供 html 文件。模板必须在 view.py 上呈现并作为 HttpResponse 返回。

所以尝试一下:

from django.shortcuts import render

def login(request):
    return render(request, 'registration/login.html')

并且 localhost:8000/registration/ 将返回一个登录页面。

有关 shortcuts functions 的更多信息,请参阅文档和 template language

关于python - 有关 Django 模板的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12157283/

相关文章:

python - 在 Django 模板中包含 url 变量

Django - 如何将字典传递给模板标签

python - 如何在具有数组值的字典中循环

python - 字符串格式化自定义对象

python - 在 Django REST 框架 API 根中包含 list_route 方法

windows - 缓慢的本地 Windows django 开发环境

django - DetailView 模板不显示模型数据

python - 设置封面: Generating test instances

python - 如何在python中播放wav文件?

python - 错误 324 空响应 - AttributeError : 'NoneType' object has no attribute 'select'