/ django中的MultiValueDictKeyError

时间:2017-03-16 14:48:28

标签: django python-2.7

我刚学会烧瓶后正在学习django。我有一个我必须做的项目,现在在django有相同的项目。我使用相同的代码然后“转换”了它。它在/ farm中抛出MultiValueDictKeyError的错误。如果有人可以指导我以哪种方式工作

完整追溯如下

回溯:

File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\exception.py" in inner
  42.             response = get_response(request)

File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\project7\ningagold\apps\ninjagold\views.py" in building
  47.   if request.POST["building"] == "farm":

File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\utils\datastructures.py" in __getitem__
  85.             raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /farm
Exception Value: "'building'"

HTML

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta charset="utf-8">
  <script type="text/javascript" src='http://code.jquery.com/jquery-1.10.2.min.js'></script> 
  <script>
  </script>

</head>
<body>
<div><P>Farm</P><form action="/farm" method="post">
   {% csrf_token %}
  <input type="hidden" name="building" value="farm" />
  <input type="submit" value="Find Gold!"/>
</form> </div>
<div><p>Cave</p><form action="/farm" method="post">
  {% csrf_token %}
  <input type="hidden" name="building" value="cave" />
  <input type="submit" value="Find Gold!"/>
</form> </div>
<div><p>House</p><form action="/farm" method="post">
  {% csrf_token %}
  <input type="hidden" name="building" value="house" />
  <input type="submit" value="Find Gold!"/>
</form> </div>
<div><p>Casino</p><form action="/farm" method="post">
  {% csrf_token %}
  <input type="hidden" name="building" value="casino" />
  <input type="submit" value="Find Gold!"/>
</form> </div>
<div><p>
</p></div>
</form>
</body>
</html>

views.py

from django.shortcuts import render, redirect, HttpResponse
import random
import string

def index(request):
    return render(request, 'ninjagold/index.html')

def building(request):
    if request.method == ['POST']:
        try:
            request.session['farmprint'] += ""
        except Exception:
            request.session['farmprint'] = ""
        try:
            request.session['caveprint'] += ""
        except Exception:
            request.session['caveprint'] = ""
        try:
            request.session['houseprint'] += ""
        except Exception:
            request.session['houseprint'] = ""
        try:
            request.session['casinoprint'] += ""
        except Exception:
            request.session['casinoprint'] = ""
    request.session['farm'] = 0
    request.session['cave'] = 0
    request.session['house'] = 0
    request.session['casino'] = 0
    if 'gold' not in request.session:
        request.session['gold'] = 0
    if request.POST["building"] == "farm":
        request.session['farm'] = random.randint(10, 20)
        print request.session['farm']
    elif request.POST["building"] == "cave":
        request.session['cave'] = random.randint(5, 10)
        print request.session['cave']
    elif request.POST["building"] == "house":
        request.session['house'] = random.randint(2, 5)
        print request.session['house']
    elif request.POST["building"] == "casino":
        request.session['casino'] = random.randint(-50, 50)
        print request.session['casino']
    if request.POST["building"] == "farm":
        request.session['farmprint'] += 'Earned' +str(request.session['farm']) + 'golds from the farm!\n'

    if request.POST["building"] == "cave":
        request.session['farmprint'] += "Earned" +str(request.session['cave']) + 'golds from the cave!\n'    
    if request.POST["building"] == "house":
        sessrequest.sessionion['farmprint'] += "Earned" +str(request.session['house']) + 'golds from the house\n'
    if request.POST["building"] == "casino":
        if request.session['casino'] > 0:
            request.session['farmprint'] += "Earned" +str(request.session['casino']) + 'golds from the casino\n'    
    if request.POST["building"] == "casino":
        if request.session['casino'] < 0:
            request.session['farmprint'] += "Earned" +str(request.session['casino']) + 'golds from the casino\n'    
        return render(request.session, 'ninjagold/index.html')
    else:
        return render(request.session, 'ninjagold/index.html')

urls.py

from django.conf.urls import url
from . import views
from django.template import RequestContext

urlpatterns = [
    url(r'^$', views.index),
    url(r'^farm$', views.building)
]

1 个答案:

答案 0 :(得分:0)

尝试:

if request.POST.get("building", False) == "farm":