Jess
Jess

Reputation: 1849

Django - setting up urls.py and views.py

I'm new to Django and am trying to configure my urls.py and views.py docs. This is probably a very simple issue but I can't for the life of me set up my urls.py and views.py docs so that localhost/index points to an index.html file I have created. I have followed the Django Project tutorial to the letter and tried many, many variations but this just isn't clicking for me. Any help would be much appreciated!

The index.html file is located at mysite/templates/index.html

My folder structure is like this...

 mysite/
      mysite/
           __init__.py
           settings.py
           urls.py
           wsgi.py
      app/
           __init__.py
           admin.py
           models.py
           tests.py
           urls.py
           views.py
      templates/
           css
           img
           js
           index.html

My views.py contains:

 from django.http import HttpResponse
 from django.shortcuts import render_to_response
 from django.template import Context, loader
 from django.http import Http404

 def index(request):
     return render(request, "templates/index.html")

UPDATE: My folder structure now looks like this:

 mysite/
      mysite/
           __init__.py
           settings.py
           urls.py
           wsgi.py
           templates/
                     index.html
      app/
           __init__.py
           admin.py
           models.py
           tests.py
           urls.py
           views.py
      static/
           css
           img     
           js

Upvotes: 3

Views: 7504

Answers (2)

Thierry Lam
Thierry Lam

Reputation: 46264

On top of setting up your TEMPLATE_DIRS in settings.py:

import os

ROOT_PATH = os.path.dirname(__file__)

TEMPLATE_DIRS = (    
    os.path.join(ROOT_PATH, 'templates'),
)

mysite/urls.py

urlpatterns = patterns('',
    url(r'^$', include('app.urls', namespace='app'), name='app'),
)

app/urls.py

urlpatterns = patterns('app.views',
    url(r'^$', 'index', name='index'),
)

In your views.py code as is, change templates/index.html to index.html and the template should go under:

mysite/mysite/templates/index.html

On another note, your css, js and img folders are best placed somewhere else like a mysite/static folder.

Upvotes: 3

Aamir Rind
Aamir Rind

Reputation: 39649

Have you defined the template path in TEMPLATE_DIRS.

settings.py

# at start add this
import os, sys

abspath = lambda *p: os.path.abspath(os.path.join(*p))

PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)

TEMPLATE_DIRS = (
    abspath(PROJECT_ROOT, 'templates'), # this will point to mysite/mysite/templates
)

Then move your templates folder to mysite > mysite > templates

Then instead of return render(request, "templates/index.html") just do like this return render(request, "index.html"). This should work.

Your directory structure should be:

mysite/
      mysite/
          __init__.py
          settings.py
          urls.py
          wsgi.py

          templates/
              index.html
          static/
              css/
              js/
          app/
               __init__.py
               admin.py
               models.py
               tests.py
               urls.py
               views.py

Upvotes: 2

Related Questions