我有一个表单,用户在其中选择了一种颜色,然后该值返回给我,但是在用户选择颜色时,返回了MultiValueDictKeyError,但我不知道为什么

这是我的看法:

def primeira_pergunta(request):
    trans = Tranfer()
    my_list = trans.shared[0]
    respostaum = ['amarelo', 'vermelho', 'roxo', 'cinza']
    selecionada = 'amarelo'
    if request.method == 'POST':
        respostaum = request.POST['respostaum']
        selecionada = request.POST['selecionada']
    #if selecionada != 'Escolha uma':
        f = Cheiro.objects.get(pk=my_list)
        u = f.pk
        f.resposta1 = selecionada
        f.save()
    else:
        selecionada = 'Escolha uma'

    context = {'respostaum': respostaum, 'selecionada': selecionada}

    return render(request, 'cheiro/primeira_pergunta.html', context)


这是我的模板:

<form method="POST">
    {% csrf_token  %}

    <label for="respostaum">Selecione unidade</label>
    <select class="form-control" id="respostaum" name="respostaum">
        {% for selecionada in respostaum %}
        <option value="{{ selecionada }}">{{ selecionada }}</option>
        {% endfor %}
    </select>
    <input type="submit">

</form>


我尝试更换
对于此selecionada = request.POST['selecionada']并返回整数错误的selecionada = request.POST.get('selecionada'),返回null。

追溯:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/cheiro/primeira_pergunta/

Django Version: 2.1.5
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'apps.cheiro',
 'apps.core']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\datastructures.py" in __getitem__
  77.             list_ = super().__getitem__(key)

During handling of the above exception ('selecionada'), another exception occurred:

File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\graficos\quiz\apps\cheiro\views.py" in primeira_pergunta
  70.        selecionada = request.POST['selecionada']

File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\datastructures.py" in __getitem__
  79.             raise MultiValueDictKeyError(key)

Exception Type: MultiValueDictKeyError at /cheiro/primeira_pergunta/
Exception Value: 'selecionada'

最佳答案

views.py中尝试以下代码,以保持模板不变(如问题所示):

def primeira_pergunta(request):
    trans = Tranfer()
    my_list = trans.shared[0]
    respostaum = ['amarelo', 'vermelho', 'roxo', 'cinza']
    if request.method == 'POST':
        selecionada = request.POST['respostaum']
    #if selecionada != 'Escolha uma':
        f = Cheiro.objects.get(pk=my_list)
        u = f.pk
        f.resposta1 = selecionada
        f.save()

    context = {'respostaum': respostaum}

    return render(request, 'cheiro/primeira_pergunta.html', context)

07-26 06:11