上下文必须是dict而不是元组Django形式

时间:2017-07-10 10:21:12

标签: python django django-forms

我正在尝试通过POST方法获取表单数据,但得到“上下文必须是dict而不是tuple”错误。我已经谷歌了很多,根据Django 1.11的文档我们只需要使用常规字典而不是Context实例。 我被困在这里,请帮帮我!

这是错误:

TypeError at /docker/auth/
context must be a dict rather than tuple.
Request Method: POST
Request URL:    http://127.0.0.1:8000/docker/auth/
Django Version: 1.11.3
Exception Type: TypeError
Exception Value:    
context must be a dict rather than tuple.
Exception Location: /Users/abdul/IstioVirEnv/lib/python3.6/site-packages/django/template/context.py in make_context, line 287
Python Executable:  /Users/abdul/IstioVirEnv/bin/python
Python Version: 3.6.1
Python Path:    
['/Users/abdul/Documents/IGui',
 '/Users/abdul/IstioVirEnv/lib/python36.zip',
 '/Users/abdul/IstioVirEnv/lib/python3.6',
 '/Users/abdul/IstioVirEnv/lib/python3.6/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Users/abdul/IstioVirEnv/lib/python3.6/site-packages']
Server time:    Mon, 10 Jul 2017 10:12:30 +0000

这是我的views.py:

import json

from django.shortcuts import render
from django.views.generic import CreateView
from . import forms
from django.http import HttpResponse

class DockerAuth(CreateView):
    form_class = forms.DockerAuthForm

    def get(self, request, *args, **kwargs):
        return render(request, 'dockerDep/docker_login.html', {})

    def post(self, request, *args, **kwargs):
        lform = forms.DockerAuthForm(request.POST)
        if lform.is_valid():
            data = lform.cleaned_data
            name = data['docker_name']
            password = data['docker_pass']
            args = {
                "mname": name,
                "mpass": password
            }
        return render(request, 'dockerDep/response.html', args)

这是我的forms.py:

from django.forms import forms
from .import models

class DockerAuthForm(forms.Form):
    class Meta:
        fields = ('docker_name', 'docker_pass')
        model = models.DockerAuth

这是我的models.py:

from django.db import models

class DockerAuth(models.Model):
    docker_name = models.CharField(max_length=255)
    docker_pass = models.CharField(max_length=255)

这是我的HTML模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DOCKER</title>
</head>
<body>
    <form method="post" action="">
        {% csrf_token %}
        <input type="text" name="docker_name" title="Name">
        <input type="password" name="docker_pass" title="Password">
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:5)

看看:

def post(self, request, *args, **kwargs):
    lform = forms.DockerAuthForm(request.POST)
    if lform.is_valid():
        data = lform.cleaned_data()
        name = data['docker_name']
        password = data['docker_pass']
        args = {
            "mname": name,
            "mpass": password
        }
    return render(request, 'dockerDep/response.html', args)

您在此行中捕获args作为参数:

def post(self, request, *args, **kwargs):

*意味着它是一个元组。在此args 成为元组之后。因此,如果你不改变lform.is_valid()中的args(形式无效的情节),它将保持元组并将传递给render()

修改

定义&#39;默认&#39; context(它将为空)

更改args的{​​{1}}名称以避免冲突

context