简体   繁体   中英

Convert the Django direct_to_template function to use class based view

I am updating a Django project which used the direct_to_template as a function ie:

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

As described a short way down this page

I have seen the SO question here and read the documentation on this page which decribe the migration of statements of the form

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

to look like

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

Unfortunatelty I cannot seem to figure out how to change statements from the form that I have into a working new form.

How might one modify this code to work with the new Templateview form?

You shouldn't be using the generic views for this, that's not what they are for. If you want to render a template, you should use the render shortcut : it takes exactly the same arguments.

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

To use TemplateView you import TemplateView into your urls.py:

from django.views.generic.base import TemplateView

Then you just add the urlconf:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM