運行Django的服務器,而我收到以下錯誤:未找到參數'('',)'和關鍵字參數'{}'的'結果'反向。 1種模式(S)試圖
Reverse for 'results' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'library/(?P<student_id>[0-9]+)/results/$']
注:錯誤熄滅,如果我刪除index.html文件這一行。
<form action="{% url 'library:results' student.id %}" method="post">
但是如果沒有這個,我不能做post請求。
這是我result.html模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hey {{ student.name }}
</body>
</html>
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from .models import Student, Choice
from django.urls import reverse
def vote(request, student_id):
student = get_object_or_404(Student, pk=student_id)
try:
selected_choice = Student.Choice_set.get(pk=request.POST['Choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'library/results.html', {
'student': student,
'error_message': "You didn't select a choice.",
})
else:
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('library:results', args=(student.id,)))
def index(request):
students_list = Student.objects.all()
template = loader.get_template('library/index.html')
context = {
'students_list': students_list,
}
return HttpResponse(template.render(context, request))
def detail(request, student_id):
try:
student = Student.objects.get(pk=student_id)
except Student.DoesNotExist:
raise Http404("Student does not exist")
return render(request, 'library/detail.html', {'student': student})
def results(request, student_id):
student = get_object_or_404(Student, pk=student_id)
return render(request, 'library/results.html', {'student': student})
<h1>{{ student.name }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'library:results' student.id %}" method="post">
{% csrf_token %}
{% for student in students_list %}
<input type="checkbox" name="student" id="student{{ forloop.counter }}" value="{{ student.id }}"/>
<label for="student{{ forloop.counter }}">{{ student.name }}</label><br/>
{% endfor %}
<input type="submit" value="Vote" />
</form>
我已經嘗試了各種各樣的東西,但都沒有工作。我無法從代碼中刪除表單動作,因爲這會使其無用。
秀我們的你的urls.py –
from django.conf.urls import url from。進口觀點 APP_NAME = '圖書館' URL模式= [ URL(R '^ $',views.index,名稱= '指數'), URL(R'^(θP [0-9] +)/ $',views.detail,name ='detail'), url(r'^(?P [0-9] +)/ results/$',views.results,name ='results' ), url(r'^(?P [0-9] +)/ vote/$',views.vote,name ='vote'), –
將它們添加到您的帖子中。 –