【问题标题】:TypeError at /add_team/ 'dict' object is not callable/add_team/ 'dict' 对象的 TypeError 不可调用
【发布时间】:2019-02-27 10:53:04
【问题描述】:

views.py:

class AddTeamView(View):
    template_name = 'add_team.html'

    def get (self, request):
        form = TeamForm()
        context = {'form': form}
        return render(request, 'add_team.html', context)

    def post(self, request):
        form = TeamForm(request.POST)
        if form.is_valid():
            team = Team()
            team.name = form.cleaned_data('name')
            team.details = form.cleaned_data('detials')
            context = {'form': form, 'team.name':team.name,'team.details':team.details}

        return render(request, self.template_name, context)

add_team.html:

    {% extends 'base.html' %}
{% block title %}
add team
{% endblock %}

{% block content %}
<form action="/add_team/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>
{% endblock %}

forms.py:

from django import forms


class TeamForm(forms.Form):
    name = forms.CharField(label='name of team')
    details = forms.CharField(label='details of team')

当我打开浏览器时,它出现了:

/add_team/ 'dict' 对象的 TypeError 不可调用请求方法: POST 请求 URL:http://127.0.0.1:8000/add_team/ Django 版本: 2.1.1 异常类型:TypeError 异常值:'dict'对象不可调用异常位置: C:\Users\Acer\Desktop\teammanager\teams\views.py 在帖子中,第 52 行 Python可执行文件: C:\Users\Acer\Desktop\teammanager_env\Scripts\python.exe Python 版本: 3.7.0

【问题讨论】:

  • 这是form.cleaned_data['detials'] 不是form.cleaned_data('detials')(可能你打错了,details 而不是detials)。

标签: python django


【解决方案1】:

form.cleaned_data是一个字典,所以你通过下标获取元素,或者使用.get(..)方法(返回None或默认值,以防key丢失),所以你应该重写:

team.name = form.cleaned_data('name')
team.details = form.cleaned_data('detials')

到:

team.name = form.cleaned_data['name']
team.details = form.cleaned_data['details']  # typo: detials -> details

话虽如此,最好还是写一个ModelForm

class TeamForm(forms.ModelForm):
    name = forms.CharField(label='name of team')
    details = forms.CharField(label='details of team')

然后视图看起来像:

class AddTeamView(View):
    template_name = 'add_team.html'

    def get (self, request):
        form = TeamForm()
        context = {'form': form}
        return render(request, 'add_team.html', context)

    def post(self, request):
        form = TeamForm(request.POST)
        if form.is_valid():
            team = form.save()
            context = {'form': form, 'name':team.name,'details':team.details}

        return render(request, self.template_name, context)

您还应该考虑使用CreateView,而不是简单的视图,并在post(..) 成功完成时重定向,因为在 POST 的情况下呈现,可能会在用户刷新页面时导致错误(请参阅this Wikipedia article 用于 POST-REDIRECT-GET 模式)。

【讨论】:

    【解决方案2】:

    表单中的数据清洗后,需要获取新的数据,这些数据是从另一个页面发布的。

    重写:

    team.name = form.cleaned_data('name')
    team.details = form.cleaned_data('detials')
    

    收件人:

    team.name = form.cleaned_data.get('name')
    team.details = form.cleaned_data.get('detials')
    

    希望得到帮助!

    【讨论】:

      猜你喜欢
      • 2011-10-01
      • 2012-10-31
      • 2017-05-27
      • 2016-03-07
      • 2019-08-10
      • 1970-01-01
      • 2020-03-24
      • 2019-08-09
      相关资源
      最近更新 更多