AttributeError:'WSGIRequest'对象没有属性'data'

时间:2017-04-09 06:42:50

标签: python django

我正在学习没有模特的Django form 下一阶段将是模型形式,最后形成+模型+酥脆 现在我被request.data

拦住了

Django:1.10 Python:3.6.0

根本原因是request没有属性data

(Pdb) dir(request)
['COOKIES', 'FILES', 'GET', 'META', 'POST', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_body', '_encoding', '_files', '_get_files', '_get_post', '_get_raw_host', '_get_scheme', '_initialize_handlers', '_load_post_and_files', '_mark_post_parse_error', '_messages', '_post', '_post_parse_error', '_read_started', '_set_post', '_stream', '_upload_handlers', 'body', 'build_absolute_uri', 'close', 'content_params', 'content_type', 'csrf_processing_done', 'encoding', 'environ', 'get_full_path', 'get_host', 'get_port', 'get_raw_uri', 'get_signed_cookie', 'is_ajax', 'is_secure', 'method', 'parse_file_upload', 'path', 'path_info', 'read', 'readline', 'readlines', 'resolver_match', 'scheme', 'session', 'upload_handlers', 'user', 'xreadlines']

base.py

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',
]


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR.path('templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

forms.py

from django import forms


class PersonDetailForm(forms.Form):
    name = forms.CharField(max_length=100)
    age = forms.IntegerField()

views.py

class ColorStudyView(View):
    template_name = 'colors/study.html'
    form_class = PersonDetailForm

    def get(self, request, format=None):
        form = self.form_class
        return render(
            request,
            self.template_name,
            {'form': form}
        )

    def post(self, request, format=None):
        form = self.form_class(request.data)
        if form.is_valid():
            return HttpResponse("Good POST")
        else:
            return render(
                request,
                self.template_name,
                {'form': form}
            )

study.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Study</title>
    <form method="post">
        {% csrf_token %}
        <table>{{ form.as_table }}</table>
        <input type="submit" value="Submit" />
    </form>
</head>
<body>

</body>
</html>

GET:工作正常 POST:Django提出AttributeError: 'WSGIRequest' object has no attribute 'data'

我想念哪里?

0 个答案:

没有答案