0

I have a django app and a view for the home page where I want to use a static html file for the homepage. My file structure is like this:

project/
  stats (this is my app)/
    urls.py
    views.py
    stats.html
    (other files here)

In my views.py I have the following code:

from django.shortcuts import render_to_response

def index(request):
    return render_to_response('stats.html')

When I run the urls, I just have the index page. I go to the stats page and I receive a TemplateDoesNotExist Error. Looking for an answer I tried to put stats/stats.html instead for the path, but I still get the same error. What is the correct way of doing this?

polarbits
  • 187
  • 1
  • 3
  • 10

2 Answers2

0

In your project directory, add a directory called 'templates' and inside the templates directory, create 'stats' directory. Put your HTML file in the stats directory.

Then change your settings file at the bottom from

TEMPLATE_DIRS = (

    os.path.join(SETTINGS_PATH, 'templates'),

)

To

TEMPLATE_DIRS = (

    os.path.join(BASE_PATH, 'templates'),

)
csling
  • 326
  • 1
  • 8
  • How do I get django to recognize the templates directory? I put the path into my settings.py as TEMPLATE_DIRS, but django says that it used the default templates path of `C:\Python36-32\lib\site packages\django\contrib\admin\templates\stats.html`. What do I do to redirect the search? – polarbits Aug 25 '17 at 02:05
  • Share your settings file. Have a look at this too. https://stackoverflow.com/questions/3038459/django-template-path – csling Aug 25 '17 at 02:10
  • Updated my answer to suit you better – csling Aug 25 '17 at 02:18
  • For some reason it refuses to recognize the directory. Is there some migration I haven't done? – polarbits Aug 25 '17 at 02:27
  • No, you need to update your installed apps too. Just add an item to the list for stats... 'stats' Easy as that – csling Aug 25 '17 at 02:28
  • Figured it out. I have a newer version of django, so instead of making an entirely separate `TEMPLATE_DIRS` variable, I just had to add the the `DIRS` key in the `TEMPLATES` dictionary of my settings.py. That way everything worked. – polarbits Aug 25 '17 at 02:35
0

You can arrange your project like this

project/
    stats (this is my app)/
        urls.py
        views.py
        stats.html
        templates/
            stats/
                stats.html

In your views.py, write this:

def index(request):
    return render_to_response('stats/stats.html')

make sure there is 'stats' in the INSTALLED_APPS list in the settings.py.

z.h.
  • 205
  • 3
  • 8