【问题标题】:Django 1.7 Changed name of template now TemplateDoesNotExist ErrorDjango 1.7 现在更改了模板的名称 TemplateDoesNotExist 错误
【发布时间】:2016-05-06 02:13:34
【问题描述】:

所以我将注册模板的名称从 user_form.html 更改为 register.html。我还在 urls.py 文件中对其进行了更改以反映新名称,但是当我尝试转到注册页面时,我得到以下信息:

Exception Type: TemplateDoesNotExist 
Exception Value:web/user_form.html 

Template-loader postmortem

Django tried loading these templates, in this order:

Using loader django.template.loaders.filesystem.Loader:

Using loader django.template.loaders.app_directories.Loader:

../swl/root/lib/pythpy2.7.egg/django/contrib/admin/templates/web/user_form.html (File does not exist)

../django/contrib/auth/templates/web/user_form.html (File does not exist)

../templates/web/user_form.html (File does not exist)

..tastypie/templates/web/user_form.html (File does not exist)

../bootstrap3/templates/web/user_form.html (File does not exist)

../web/templates/web/user_form.html (File does not exist)

为什么模板加载器仍然在寻找旧的文件名?

编辑:这是我的注册网址:

url(r'register/?$', UserCreate.as_view(), { 'template_name':'accounts/register.html' }, name = 'register'),

【问题讨论】:

  • 显示您在 urls.py 中所做的更改。

标签: python html django templates django-templates


【解决方案1】:

尝试将所有文​​件中的所有user_form.html 替换为registration.html。您可以使用文本编辑器或 IDE 的 Find/replace in files 功能(或类似功能)。

url 更改为:

url(r'register/?$',
    UserCreate.as_view(template_name='accounts/register.html'),
    name = 'register')

【讨论】:

  • 感谢@valdimir,我做到了,但我仍然收到错误。
  • @user5852854 尝试更改UserCreate.as_view(...)
  • 我要把它改成什么?
  • 你先生太棒了-谢谢!
【解决方案2】:

当没有显式声明名称时,基于类的视图将尝试使用默认模板名称。 (CreateView 使用 (appname)/(model)_form.html) 你的选择是:

  1. 使用默认名称命名您的模板
  2. 在 URL 中指定模板(如 Vladimir 的回答)
  3. 在视图代码中指定模板名称,如下所示:

    类 UserCreate(CreateView):

    template_name = "accounts/register.html"
    

希望对你有帮助

【讨论】:

    【解决方案3】:

    重新启动 apache 服务器。 对于 Ubuntu 服务器 20:

    /etc/init.d/apache2 restart
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多