cybertextron
cybertextron

Reputation: 10961

how to display a view in Django?

I'm totally new to Django, and I'm trying to understand how does it work (I'm more used to PHP and Spring frameworks. I have a project called testrun and inside it an app called graphs, so my views.py looks like:

#!/usr/bin/python
from django.http import HttpResponse

def index(request):
   return HttpResponse("Hello, World. You're at the graphs index.")

then, in graphs/urls.py:

from django.conf.urls import patterns, url, include
from graphs import views

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

finally, at testrun/urls.py:

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

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testrun.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^graphs/', include('graphs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

However, when I try to access http://127.0.0.1:8000/graphs/ I get:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

What am I doing wrong that I can't get that simple message to be displayed in the browser?

Upvotes: 0

Views: 72

Answers (1)

user234932
user234932

Reputation:

To expand on my comment, the first argument to patterns() function is

a prefix to apply to each view function

You can find more information here:

https://docs.djangoproject.com/en/dev/topics/http/urls/#syntax-of-the-urlpatterns-variable

Therefore in graphs/urls.py you need to fix the patterns call like so:

urlpatterns = patterns('', # <-- note the `'',`
   url(r'^$', views.index, name='index'),
)

Upvotes: 2

Related Questions