【问题标题】:Django strange error turns my template name into 0x000000000461A128Django 奇怪的错误将我的模板名称变成 0x000000000461A128
【发布时间】:2015-07-18 21:46:05
【问题描述】:

我知道我搞砸了,因为谷歌没有返回任何结果。我对计算机不太了解,但0x00000blah 看起来像是一些内存地址。刷新几次后,这个数字就会改变。

环境 Django 1.8、Python 3.4、chrome、Win7

预期行为 我想去../accounts/login/ 在我的网址中也称为url(r'^login/$', 'django.contrib.auth.views.login', name='loginHandle') 输入用户名、密码,如果它已注册,它会重定向到../accounts/profile,它应该呈现模板profile.html,它会说类似Hello {{ username }} ! .相反,我得到了这个错误

TemplateDoesNotExist 位于 /accounts/profile/

django.template.backends.django.Template 对象位于 0x0000000004D0E400

views.py

def profile(request):
    profileTemplate = loader.get_template('registration/profile.html')
    return render(request, profileTemplate, {
        'Kitty': 10,
    })

追溯

Using loader django.template.loaders.filesystem.Loader:
D:\users\kitty\python\firstdjangosite\templates\<django.template.backends.django.Template object at 0x0000000004D0E400> (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
D:\Users\kitty\Python\lib\site-packages\django\contrib\admin\templates\<django.template.backends.django.Template object at 0x0000000004D0E400> (File does not exist)
D:\Users\kitty\Python\lib\site-packages\django\contrib\auth\templates\<django.template.backends.django.Template object at 0x0000000004D0E400> (File does not exist)
D:\users\kitty\python\firstdjangosite\polls\templates\<django.template.backends.django.Template object at 0x0000000004D0E400> (File does not exist)
D:\users\kitty\python\firstdjangosite\login\templates\<django.template.backends.django.Template object at 0x0000000004D0E400> (File does not exist)
D:\users\kitty\python\firstdjangosite\accounts\templates\<django.template.backends.django.Template object at 0x0000000004D0E400> (File does not exist)

路径详情

D:\Users\Kitty\Python\FirstDjangoSite\accounts\templates>dir /s /b /o:gn
accounts
registration
accounts\index.html
accounts\invalid.html
accounts\loggedin.html
accounts\logout.html
registration\login.html
registration\profile.html

我之前做过加载器和渲染,它们工作正常。例如,如果我将 profileTemplate 更改为不存在的“registration/helloworld.html”,它会显示

TemplateDoesNotExist 位于 /accounts/profile/

注册/helloworld.html

感谢您的宝贵时间,请帮助我感到困惑

【问题讨论】:

    标签: python django


    【解决方案1】:

    render 快捷方式采用模板名称。

    def profile(request):
        return render(request, 'registration/profile.html', {
            'Kitty': 10,
        })
    

    令人惊讶的错误消息是因为您传递了一个加载的模板而不是模板名称。 django.template.backends.django.Template object at 0x0000000004D0E400repr()profileTemplate

    【讨论】:

    • 感谢您的快速响应,这是有道理且有效的!干杯(当 SO 允许时会接受)
    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 2011-08-06
    • 1970-01-01
    • 2018-08-04
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多