4

I have a Django app (I'm fairly new so I'm doing my best to learn the ins and outs), where I would like to have a url endpoint simply redirect to a static html file in another folder (app).

My project file hierarchy looks like:

docs/
 - html/
    - index.html
myapp/
 - urls.py

My urls.py looks like:

from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^docs/$', RedirectView.as_view(url='/docs/html/index.html')),
)

However, when I navigate to http://localhost:8000/docs I see the browser redirect to http://localhost:8000/docs/html/index.html, but the page is not accessible.

Is there any reason that the /docs/html/index.html would not be avalable to the myApp application in a redirect like this?

An pointers would be greatly appreciated.

1
  • I've never tried this with Django, but it seems to me you wouldn't need any normal kit to make a "static" page. Just create a view that responds with a template that doesn't have any context to process. Commented Aug 12, 2014 at 15:58

2 Answers 2

5

NOTE: direct_to_template has been deprecated since Django 1.5. Use TemplateView.as_view instead.

I think what you want is a Template View, not a RedirectView. You could do it with something like:

urls.py

from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    (r'^docs/$', direct_to_template, {
        'template': 'index.html'
    }),
)

Just ensure the path to index.html is in the TEMPLATE_DIRS setting, or just place it in the templates folder of your app (This answer might help).

Sign up to request clarification or add additional context in comments.

2 Comments

dead link for template view.
This interprets index.html as a Django template, not as a static HTML file.
3

I'm pretty sure Django is looking for a URL route that matches /docs/html/index.html, it does not know to serve a static file, when it can't find the route it is showing an error

3 Comments

That would make sense. Any way to tell it that it is a static file that you know of?
STATIFILES_DIR will let you setup the directory
You could also use a TemplateView for the specific url, many different ways available...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.