Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display static page in Django

Tags:

django

I am trying to display contents of a static page in Django project.

urls.py :-

from django.conf.urls import patterns, include, url 
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'spollow.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    (r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
    url(r'^admin/', include(admin.site.urls)),
)

index.html is in the same directory as urls.py

I am getting 500 internal server error. Any ideas where I am going wrong?

like image 859
jisu Avatar asked Jan 27 '26 14:01

jisu


2 Answers

First of all, what is the stacktrace from the 500 error saying that the error may be? You may be using Django 1.6 and the call to direct_to_template is deprecated.

On Django 1.5 or newer you can use TemplateView

Here's the example from the documentation

https://docs.djangoproject.com/en/dev/topics/class-based-views/

from django.conf.urls import patterns
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^about/', TemplateView.as_view(template_name="about.html")),
)

You can use the direct_to_template view on Django 1.4 or older

Here's the relevant documentation

https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    (r'^foo/$',             direct_to_template, {'template': 'foo_index.html'}),
    (r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)

If it is the latter, I would use a module instead of string, (look at the import on the example).

Other than that, without the 500 details it will be shooting in the dark, you may not have the right template, or an incorrect path, or a million different things.

Bonus note

If you just want to serve static pages, it might be better to serve them through the actual webserver in front of django (nginx, apache, etc), specially if you are expecting a high volume of traffic.

like image 153
Juan Carlos Moreno Avatar answered Jan 29 '26 04:01

Juan Carlos Moreno


If Your error is due to unable to find index.html

if yours is an app(ie: created by python manage.py startapp <app>) then:

Then django will search for template files in <app>/templates directory, if you added the app to INSTALLED_APPS in settings.py.

so you need to create a folder templates inside your <app> and put index.html inside it.

if you don't have any apps, you want to add the template path manually :

open settings.py, then edit TEMPLATE_DIRS

TEMPLATE_DIRS = (
    # Put the full path of the template dir here, like "/home/html/django_templates" or
    # "C:/www/django/templates". 
)
like image 45
suhailvs Avatar answered Jan 29 '26 03:01

suhailvs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!