【问题标题】:Convert the Django direct_to_template function to use class based view将 Django direct_to_template 函数转换为使用基于类的视图
【发布时间】:2015-05-28 04:27:00
【问题描述】:

我正在更新一个使用 direct_to_template 作为函数的 Django 项目,即:

return direct_to_template(request, 'bprofile/init.html', targs)

如前所述this page

我看过SO question here 并阅读了documentation on this page,其中描述了表单语句的迁移

('^about/$', direct_to_template, {'template': 'about.html'})

看起来像

('^about/$', TemplateView.as_view(template_name='about.html'))

不幸的是,我似乎无法弄清楚如何将报表从我拥有的表格更改为一个有效的新表格。

如何修改此代码以使用新的 Templateview 表单?

【问题讨论】:

    标签: python django


    【解决方案1】:

    您不应该为此使用通用视图,这不是它们的用途。如果你想渲染一个模板,你应该使用render shortcut:它采用完全相同的参数。

    return render(request, 'bprofile/init.html', targs)
    

    【讨论】:

    • 是的,做到了。我只是在“direct_to_template(”上做了一个简单的查找和替换为“render(”),问题就解决了。干杯。
    【解决方案2】:

    要使用TemplateView,您需要将 TemplateView 导入 urls.py:

    from django.views.generic.base import TemplateView
    

    然后你只需添加 urlconf:

    ('^about/$', TemplateView.as_view(template_name='bprofile/init.html'))
    

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 2012-10-23
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多