3
votes

I am trying to create my own project with Django version 1.9.x, but I can't make the index page at / to work.

This is my folder structure:

rxe/
    urls.py
    wsgi.py
    settings.py
blog/
    migrations/
    static/
        blog/
            css/
            js/
            img/
    templates/
        blog/
           index.html
    admin.py
    models.py
    tests.py
    urls.py
    views.py
manage.py

rxe/urls.py:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'$', include('blog.urls')), 
]

blog/urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name = 'index'),
]

blog/views.py:

from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView

class IndexView(TemplateView):
    template_name = 'blog/index.html'

When accessing localhost:8000/, the error:

TemplateDoesNotExist at /

blog/index.html

And right below it, in Template-loader postmortem:

django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/admin/templates/blog/index.html (Source does not exist)

django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/auth/templates/blog/index.html (Source does not exist)

It works if I return an HttpResponse('Hello'), like in the tutorial, but this same tutorial does not have any view for / path, and I want one there.

Edit

rxe/settings.py, how it was created:

TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
    },
]
1
@RodXavier settings.py file? - fermoga
@warath-coder To where? Isn't there the correct place? - fermoga
@RodXavier I updated the question. Is that it? In the official tutorial, I do not record of having to modify the template. - fermoga
in blog/templates/index.html, then in your code just put 'index.html' in the render statement. - warath-coder

1 Answers

4
votes

You must add the blog app to your project by including it in your INSTALLED_APPS setting:

INSTALLED_APPS = [...,
                  'blog']

Otherwise the template loader will not process the blog directory since it is not seen as an app.