このような URL が必要です --
/chart/2012
/chart/2009
/chart/1996
...各 #, は年です。そこで、この行をアプリの urls.py に追加しました --
url(r'^chart/(?P<year>\d+)$',views.chart,name="chart"),
しかし、URL にアクセスすると 404 になります。数字を変数\d+
にキャプチャするべきではありませんか?year
(そして、はい、views.py で定義されたチャート関数があり、変数を使用しようとしない場合に機能します)
アップデート:
完全な 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"),
)
ここに私の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))
ご覧のとおり、現時点では最低限の作業であり、先に進む前に問題が解決することを確認するためのテストのようなものです。そして404ページは言う -
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.