在我开始的Django课程中,我整天都在为这个问题而苦苦挣扎。我收到此错误:
未找到带有参数'('',)'的'detail'的相反内容。尝试了1个模式:['polls /(?P [0-9] +)/ $']
下面您可以看到我的代码。我希望有人可以帮我这个忙。非常感谢。
views.py:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from django.http import Http404
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
print ("esto es CONTEXT: ")
print (context)
print(request)
print ("FIN DE CONTEXT")
return render(request, 'polls/index.html', context)
# Create your views here.
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
urls.py:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
index.html:
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a>
</li>