昨日、このチュートリアルを使用して最初のアプリに取り組んでいました。それは投票と選択のアプリです。最初のページには質問が表示され、質問をクリックすると、投票できる選択肢が表示されるはずです。
昨日私を助けてくれて、名前空間を使うように言われた素晴らしい人たちがいました。名前空間のチュートリアルを読み、自分の知識をシナリオに適用しようとしましたが、今のところ機能していません。
これは、最初のページである質問をクリックしたときの私のエラーです。
NoReverseMatch at /polls/5/
u"'polls" is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/5/
Django Version: 1.4.3
Exception Type: NoReverseMatch
Exception Value:
u"'polls" is not a registered namespace
Exception Location: C:\hp\bin\Python\Lib\site-packages\django\template\defaulttags.py in render, line 424
Python Executable: C:\hp\bin\Python\python.exe
Python Version: 2.5.2
Python Path:
['C:\\djcode\\mysite',
'C:\\hp\\bin\\Python\\python25.zip',
'C:\\hp\\bin\\Python\\DLLs',
'C:\\hp\\bin\\Python\\lib',
'C:\\hp\\bin\\Python\\lib\\plat-win',
'C:\\hp\\bin\\Python\\lib\\lib-tk',
'C:\\hp\\bin\\Python',
'C:\\hp\\bin\\Python\\lib\\site-packages',
'C:\\hp\\bin\\Python\\lib\\site-packages\\win32',
'C:\\hp\\bin\\Python\\lib\\site-packages\\win32\\lib',
'C:\\hp\\bin\\Python\\lib\\site-packages\\Pythonwin']
Server time: Fri, 15 Feb 2013 21:04:10 +1100
Error during template rendering
In template C:\djcode\mysite\myapp\templates\myapp\detail.html, error at line 5
u"'polls" is not a registered namespace
1 <h1>{{ poll.question }}</h1>
2
3 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
4
5 {% url 'polls:vote' poll.id %}
6 {% csrf_token %}
7 {% for choice in poll.choice_set.all %}
8 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
9 <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
10 {% endfor %}
11 <input type="submit" value="Vote" />
12 </form>
これで、問題がdetail.html、メインURL、およびmyappURLCONFとviews.pyというアプリに隠されていることがわかりました。
現在、私のメインのURLconfは次のとおりです。C:\ djcode \ mysite \ mysite
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
#url(r'^polls/', include('myapp.urls')),
url(r'^polls/', include('myapp.urls', namespace='polls')),
url(r'^admin/', include(admin.site.urls)),
)
私のアプリフォルダはmyappと呼ばれ、これはmyapp URLconfです:C:\ djcode \ mysite \ myapp
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns, include, url
urlpatterns = patterns('myapp.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
myapp内の私のviews.pyは次のとおりです。
from django.http import HttpResponse
from myapp.models import Poll ,choice
from django.template import Context, loader
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
return render_to_response('myapp/index.html', {'latest_poll_list': latest_poll_list})
def results(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('myapp/results.html', {'poll': p})
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('myapp/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
}, context_instance=RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('myapp.views.results', args=(p.id,)))
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('myapp/detail.html', {'poll': p},
context_instance=RequestContext(request))
My detail.html C:\ djcode \ mysite \ myapp \ templates \ myapp
<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% url 'polls:vote' poll.id %}
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>