Django Bug

1. MultiValueDictKeyError

MultiValueDictKeyError


Possible cause and solutions:

  • There is no dict key named ‘user’, or you have a naming conflict.
  • In views.py your code

    If you use GET method:

    request.GET.get('user')
    
    #instead of request.GET['user']
    

    If you use POST method:

    request.POST.get('user')
    
    #instead of request.POST['user']
    


2. CSRF verification failed. Request aborted

這裏寫圖片描述

Possible cause and solutions:

  • Add {% csrf_token %}:

    <form action="" method="POST">
    {% csrf_token %}
    ......
    ......
    </form>
  • Some solutions may suggest adding two lines in settings.py:

    ‘django.middleware.csrf.CsrfViewMiddleware’,
    ‘django.middleware.csrf.CsrfResponseMiddleware’,

    but there is no need to add these two lines if your django version is above 1.3. You just need to annotate them.

  • In views.py use RequestContext instead of Context:

    from django.template import RequestContext
    
    #......
    
    return render_to_response(‘index.html’,
                  {'param1′: 'aaa','param2′: 'bbb',},
                  context_instance=RequestContext(request)
                 )
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章