[Django]-Django urls straight to html template

27๐Ÿ‘

โœ…

As long as there is some uniquely identifying section in the URL, you will not need to create an entry in urls.py for each direct-template url.

For example, you could say that all urls ending in โ€œ.htmlโ€ are referencing a direct file from the templates.

urlpatterns = patterns('django.views.generic.simple',
    (r'(.+\.html)$', 'direct_to_template'),
    # ...
)

Take a look at http://docs.djangoproject.com/en/1.2/ref/generic-views/#django-views-generic-simple-direct-to-template for details.

๐Ÿ‘คPeter Shinners

29๐Ÿ‘

Currently the best way to do this is using TemplateView from generic class-based views:

    from django.urls import path
    from django.views.generic.base import TemplateView

    path(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),

12๐Ÿ‘

Write a url which grabs the static pages youโ€™re interested in

url(r'^(?P<page_name>about|faq|press|whatever)/$', 'myapp.staticpage', name='static-pages')

The staticpage view function in myapp

from django.views.generic.simple import direct_to_template
from django.http import Http404

def staticpage(request, page_name):
    # Use some exception handling, just to be safe
    try:
        return direct_to_template(request, '%s.html' % (page_name, ))
    except TemplateDoesNotExist:
        raise Http404

Of course, you need to follow a naming convention for your templates, but this pattern can be expanded upon as needed.

This is better than the .+\.html pattern because it will treat templates which donโ€™t exist as 404s, whereas .+\.html will blow up with 500 errors if the template doesnโ€™t exist.

๐Ÿ‘คbrianz

12๐Ÿ‘

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<page>.+\.html)$', StaticView.as_view()),
    # ...
)
๐Ÿ‘คkirpit

2๐Ÿ‘

One way to do this would be to write a single custom view that wraps the direct_to_template generic view. The wrapper could accept a parameter and accordingly form the name of the template and pass it to direct_to_template. This way you can route multiple pages with a single URL configuration.

Something like this:

url(r'^foo/(?P<page_name>\w+).html$', 'my_static_wrapper', name = 'my_static_wrapper'),

def my_static_wrapper(request, page_name):
    # form template name and call direct_to_template

That said I suspect that there are better solutions out there though.

๐Ÿ‘คManoj Govindan

0๐Ÿ‘

Slight Changes for latest versions of Django.

from django.views.generic.base import TemplateView

urlpatterns = [
 path('',TemplateView.as_view(template_name='index.html'), name='home'),
]
๐Ÿ‘คDimanjan

Leave a comment