2016-11-16 59 views
0

運行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>
我views.py文件

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})
的index.htmp模板是本

<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>

我已經嘗試了各種各樣的東西,但都沒有工作。我無法從代碼中刪除表單動作,因爲這會使其無用。

+0

秀我們的你的urls.py –

+0

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'), –

+0

將它們添加到您的帖子中。 –

回答

1

student在該點不存在,所以沒有對於student.id的值。

您應該移動您的標籤,使其位於表單元素之前 - 並且匹配的endfor需要在表單關閉後進行。

1

問題是index.html文件。

你傳入student.id到形式的行動,但有學生沒有定義的變量,你應該將動作和表單標籤裏面的最終代碼loop.The會是這樣的:

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 

{% for student in students_list %} 

    <h1>{{ student.name }}</h1> 

    <form action="{% url 'library:results' student.id %}" method="post"> {% csrf_token %} 

     <input type="checkbox" name="student" id="student{{ forloop.counter }}" value="{{ student.id }}"/> 
     <label for="student{{ forloop.counter }}">{{ student.name }}</label><br/> 
     <input type="submit" value="Vote" /> 

    </form> 

{% endfor %} 
+0

它刪除了以前的錯誤,但顯示CSRF驗證失敗時,我點擊一個複選框,並提交。 –

+0

@surabhigupta這是一個新問題。我解決了你的最後一個問題,所以你應該接受它作爲正確的答案。我也會幫你解決這個問題。讓我檢查一下。 –

+0

@surabhigupta您是否在表單開始標籤末尾添加了{%csrf_token%}? –

相關問題