Django urls straight to html template

后端 未结 5 1944
我在风中等你
我在风中等你 2021-02-01 01:58

Learning django & python.

Just set up a new site after doing the tutorial. Now for arguments sake say I want to add a bunch of About us, FAQ basic html pages with ve

5条回答
  •  野的像风
    2021-02-01 02:34

    If you're using the class based views because direct_to_template has been deprecated, you can create a simple wrapper that renders your own templates directly:

    from django.views.generic import TemplateView
    from django.template import TemplateDoesNotExist
    from django.http import Http404
    
    class StaticView(TemplateView):
        def get(self, request, page, *args, **kwargs):
            self.template_name = page
            response = super(StaticView, self).get(request, *args, **kwargs)
            try:
                return response.render()
            except TemplateDoesNotExist:
                raise Http404()
    

    and in your router:

    from myapp.static.views import StaticView
    
    urlpatterns = patterns('',
        url(r'^(?P.+\.html)$', StaticView.as_view()),
        # ...
    )
    

提交回复
热议问题