Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django urls straight to html template

Tags:

python

django

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 very limited dynamic elements do you go ahead and write a new line in my urls.py file for each page? or is their some neat way to say map all * *.html to the relevant .html file directly?

In general even if it does require a view will I have to write a new line in the url.py file for every page?

like image 891
Derek Organ Avatar asked Aug 04 '10 04:08

Derek Organ


People also ask

How connect Django with HTML?

In the Django Intro page, we learned that the result should be in HTML, and it should be created in a template, so let's do that. Create a templates folder inside the members folder, and create a HTML file named myfirst.html .

What does {% %} mean in Django?

This tag can be used in two ways: {% extends "base.html" %} (with quotes) uses the literal value "base.html" as the name of the parent template to extend. {% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template.

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .

How Django URLs work with regular expressions?

We use regular expressions to match these url paths to their corresponding request handler (aka View). If a path, either dynamic or static, is matched the View will then handle the request and return a response to the User. If a path is not matched, Django will automatically send a 404 Page Not Found response.


4 Answers

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.

like image 147
Peter Shinners Avatar answered Oct 16 '22 18:10

Peter Shinners


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

from django.views.generic.base import TemplateView

url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),
like image 33
Igor Pomaranskiy Avatar answered Oct 16 '22 16:10

Igor Pomaranskiy


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.

like image 12
brianz Avatar answered Oct 16 '22 16:10

brianz


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()),
    # ...
)
like image 12
kirpit Avatar answered Oct 16 '22 18:10

kirpit