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.
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'),
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
- [Django]-Django data migration when changing a field to ManyToMany
- [Django]-Uwsgi installation error in windows 7
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.
- [Django]-ValueError: Missing staticfiles manifest entry for 'favicon.ico'
- [Django]-How can I get all the request headers in Django?
- [Django]-How to write django test meant to fail?
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()),
# ...
)
- [Django]-Model form save. Get the saved object
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
- [Django]-Django test runner not finding tests
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.
- [Django]-How to convert a Django QuerySet to a list?
- [Django]-Authenticate by IP address in Django
- [Django]-How can I make a trailing slash optional on a Django Rest Framework SimpleRouter
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'),
]
- [Django]-Is virtualenv recommended for django production server?
- [Django]-When to use get, get_queryset, get_context_data in Django?
- [Django]-How do I use Django templates without the rest of Django?