2011-11-10 60 views
-1

這是我的views.py:一個奇怪的Django錯誤

# Create your views here. 

from django.http import HttpResponseRedirect 
from django.shortcuts import render_to_response 
from django.db import models 

    from display.forms import CodeForm 
    from display.forms import CodeFormSet 
    from ExamPy.questions.models import QuestionBase 


    def codepost(request): 
     if request.method == 'POST': 
      form = CodeFormSet(request.POST) 
      if form.is_valid(): 
       titles = [] 
       for i in range(0, self.total_form_count()): 
          form = self.forms[i] 
          title = form.cleaned_data['title'] 
          if title in titles: 
           raise forms.ValidationError("Articles in a set must have distinct titles.") 
           titles.append(title) 
       return render_to_response('quesdisplay.html') 
     else: 
      form = CodeFormSet() 

     return render_to_response('quesdisplay.html', {'form':form}) 

因此,當我點擊提交按鈕,它應該顯示quesdisplay.html沒有任何形式的在裏面。但是,它將我帶到一些甚至不存在的聯繫頁面。

錯誤:

The current URL, contact/, didn't match any of these. 

我已經嘗試了所有可能的方法來調試這一點,但它不可能的,因爲沒有任何東西在這個名爲「接觸」的痕跡。

編輯: 這是我得到的警告:

/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py:101: UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext. 
    warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.") 
[10/Nov/2011 05:34:17] " 
+0

你有沒有爲您在urls.py.「接觸」條目 – danihp

+0

是的。我有。沒有一絲接觸。即使在模板中也沒有。模板有「。」 – Hick

+0

您是否檢查過「聯繫人」的「quesdisplay.html」?在你的警告不出現'聯繫'。你在混合問題? – danihp

回答

3

正如在之前的評論看出,採用的RequestContext解決您的問題。

這裏是關於csrf_token文檔:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it

我們可以讀到:

Use RequestContext, which always uses 'django.core.context_processors.csrf' (no matter what your TEMPLATE_CONTEXT_PROCESSORS setting). If you are using generic views or contrib apps, you are covered already, since these apps use RequestContext throughout.

所以在這裏我們沒有使用一個通用的觀點,既不是的contrib應用程序似乎。 所以我們需要它來傳遞RequestContext,因爲它就像Django中的csrf保護一樣。

from django.core.context_processors import csrf 
from django.shortcuts import render_to_response 

def my_view(request): 
    c = {} 
    c.update(csrf(request)) 
    # ... view code here 
    return render_to_response("a_template.html", c) 

from django.views.generic.simple import direct_to_template 

def app_view(request):    
    return direct_to_template(request, 'app_template.html', app_data_dictionary) 

from django.shortcuts import render_to_response 
from django.template import RequestContext 

def app_view(request): 
    return render_to_response('app_template.html', 
           app_data_dictionary, 
           context_instance=RequestContext(request)) 

而且文檔談到:演員/ csrf_migration_helper.py腳本。 似乎是您的情況:)

希望它可以幫助有幫助;)