短い話ですが、今週はDjangoの基礎を学び始め、チュートリアルを作成してチュートリアルを作成しましたが、うまくいきましたが、コードを少し変更しようとしました。 Djangoの私の経験がないため(正直なところ、HTMLでも)、私はこのNoReverseMatchエラーを取得しました。私が基本的にしたことは、クラスの名前を変更しようとしたことで、それはアプリケーションでの使用であり、プロジェクト(クラスTText)に沿ったオブジェクトです。 次に、/ poll/*(* TTextのID番号)と/ polls/* /の両方に現れるエラーがlocalhost Webページに表示されます。 最初のものは、detail.html
で5行目以下のエラーを与える:results.html
でDjango - コードの単純な変更後のNoReverseMatch
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<text_id>[0-9]+)/vote/$']
そして第二に、9行目:
Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<pk>[0-9]+)/$']
そして、ここでは、このDjangoのアプリの私の設定です:
/mysite
urls.py
/polls
models.py
urls.py
views.py
/templates
/polls
detail.html
index.html
results.html
ここで、/mysite/urls.py
は:
から/polls/models.py
:また
from __future__ import unicode_literals
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class TText(models.Model):
text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
text = models.ForeignKey(TText, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
から/polls/urls.py
:また
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<text_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
から/polls/views.py
:また
from django.shortcuts import get_object_or_404, render
# Create your views here.
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from .models import Choice, TText
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_text_list'
def get_queryset(self):
"""Return the last five published questions."""
return TText.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = TText
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = TText
template_name = 'polls/results.html'
def vote(request, text_id):
text = get_object_or_404(TText, pk=text_id)
try:
selected_choice = text.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'text': text,
'error_message': "You didn't select a choice.",
})
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('polls:results', args=(text.id,)))
から/polls/templates/polls/detail.html
:
<h1>{{ text.text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' text.id %}" method="post">
{% csrf_token %}
{% for choice in text.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>
も:/polls/templates/polls/index.html
:
{% if latest_text_list %}
<ul>
{% for text in latest_text_list %}
<li><a href="{% url 'polls:detail' text.id %}">{{ text.text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
も - /polls/templates/polls/results.html
:
<h1>{{ text.text}}</h1>
<ul>
{% for choice in text.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' text.id %}">Vote again?</a>
PS:それは、初めてここにスタック上の質問のポストですので、その上の任意の間違いを指摘して自由に感じます同じように。 オハイオ州、はい、私は同様の問題の他の記事を読んだが、私は自分の問題とそれらのいずれかをマッチさせていません。あなたの時間をありがとう!
このエラーを表示するのはどのページですか? –