Python 在/add_team/'处输入错误;dict';对象不可调用
views.py:Python 在/add_team/'处输入错误;dict';对象不可调用,python,django,Python,Django,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):
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)
添加_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 %}
当我进入浏览器时,它显示如下:
/add_team/'dict'对象处的TypeError不是可调用的请求方法:
发布请求URL:Django版本:
2.1.1异常类型:TypeError异常值:“dict”对象不是可调用的异常位置:
C:\Users\Acer\Desktop\teammanager\teams\views.py,第52行
Python可执行文件:
C:\Users\Acer\Desktop\teammanager\u env\Scripts\python.exe python
版本:
3.7.0
form.cleaned_data
是一个字典,因此您可以通过订阅或使用.get(..)
方法(在缺少键的情况下返回None
或默认值)来获取元素,因此您应该重写:
team.name = form.cleaned_data('name')
team.details = form.cleaned_data('detials')
致:
然后,视图看起来像:
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)
class AddTeamView(视图):
模板名称='add_team.html'
def get(自我,请求):
form=TeamForm()
上下文={'form':form}
返回渲染(请求“add_team.html”,上下文)
def post(自我,请求):
表单=团队表单(request.POST)
如果form.is_有效():
team=form.save()
上下文={'form':form,'name':team.name,'details':team.details}
返回渲染(请求、self.template\u名称、上下文)
您还应该考虑使用<代码> CREATEVIEW ,而不是简单的视图,当<代码> POST(..)>代码>成功时,重定向,因为在帖子的情况下呈现,在用户刷新页面时会导致错误(参见后重定向获取模式)。
<代码>窗体。因此,您可以通过订阅或使用
.get(..)
方法(在缺少键的情况下返回None
或默认值)来获取元素,因此您应该重写:
team.name = form.cleaned_data('name')
team.details = form.cleaned_data('detials')
致:
然后,视图看起来像:
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)
class AddTeamView(视图):
模板名称='add_team.html'
def get(自我,请求):
form=TeamForm()
上下文={'form':form}
返回渲染(请求“add_team.html”,上下文)
def post(自我,请求):
表单=团队表单(request.POST)
如果form.is_有效():
team=form.save()
上下文={'form':form,'name':team.name,'details':team.details}
返回渲染(请求、self.template\u名称、上下文)
您还应该考虑使用<代码> CREATEVIEW ,而不是简单的视图,当<代码> POST(..)>代码>成功时,重定向,因为在一个POST的情况下进行渲染,当用户刷新页面时会导致错误(参见后重定向获取模式)。需要从另一个页面获取新数据
重写:team.name=form.cleanned\u数据('name'))
team.details=form.cleaned_数据('detials'))
致:
team.name=form.cleaned\u data.get('name'))
team.details=form.cleaned\u data.get('detials'))
希望这有帮助 清除表单中的数据后,需要从另一个页面获取新数据 重写:
team.name=form.cleanned\u数据('name'))
team.details=form.cleaned_数据('detials'))
致:
team.name=form.cleaned\u data.get('name'))
team.details=form.cleaned\u data.get('detials'))
希望这有帮助 它是
form。cleaned_data['detials']
不是form。cleaned_data('detials')
(可能是您输入了一个打字错误,details
,而不是detials
)。它是表单。cleaned_data['detials']
不是表单。cleaned_data('detials')
(可能是你打错了,details
,而不是detials
)。
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)