0

I want to have a URL like these --

/chart/2012
/chart/2009
/chart/1996

...each #, is a year. So I added this line into my app's urls.py --

url(r'^chart/(?P<year>\d+)$',views.chart,name="chart"),

but it turns in a 404 when I go to the URL's. Shouldn't the \d+ capture the digits into the year variable?

(and yes, I do have a chart function defined in my views.py & it works when I don't try to use a variable with it)

UPDATE:

Here's the full urls.py --

from django.conf.urls import patterns,url
from musichart import views

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^chart/(?P<year>\d+)$',views.chart,name="chart"),
)

Here's my views.py --

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader #Context
from musichart.models import Station,Song,Album,Related,Artist


def index(request):
    template = loader.get_template('chart/index.htm')
    context = RequestContext(request, {
        'title': "Here is the title",
        'testvar': "blah blah blah testing 1 2 3",
        'numero': 17,
    })
    return HttpResponse(template.render(context))


def chart(request, year):
    template = loader.get_template('chart/chart.htm')
    context = RequestContext(request, {
        'title': "Here is the title",
        'testvar': "blah blah blah testing 1 2 3",
        'numero': 17,
    })
    return HttpResponse(template.render(context))

As you can see, it's bare bones at the moment and just sort of testing to make sure things go through before I go any further. And the 404 page says --

Using the URLconf defined in msite.urls, Django tried these URL patterns, in this order:
^admin/
^accounts/
^chart/ ^$ [name='index']
^chart/ ^chart/(?P<year>\d+)$ [name='chart']
^health/
The current URL, chart/2010, didn't match any of these.
9
  • does the chart function take a year parameter? Commented May 29, 2013 at 19:53
  • Even if it didn't, he wouldn't get a 404 error, as a 404 indicates a mismatch of the URL. Commented May 29, 2013 at 19:54
  • 1
    The 404 gives a list of urls that could be matched along with the actual URL. Could you post the actual URL as reported by the 404 page? Commented May 29, 2013 at 19:56
  • Can you show the usage in the template ? Commented May 29, 2013 at 20:01
  • Can you show also the full urls definitions? Commented May 29, 2013 at 20:26

1 Answer 1

1

My guess is you're including the "chart" URI component already in the project-level (IE, root) urlconf, so including it again the app's urlconf is throwing off the resolver. Basically try removing the "chart/" from the chart url, like so:

from django.conf.urls import patterns,url
from musichart import views

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^(?P<year>\d+)$',views.chart,name="chart"),
)

And in addition, double check that you don't have a trailing space after "chart" when including the app-level urlconf from the root urlconf.

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

1 Comment

Oh my goodness, I can't believe that I didn't catch that. I was so caught up in thinking it must've been a regex mistake 'cause I've never really done that before, that I totally missed the real issue. Thanks!!

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.