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'对象不可调用异常位置: 帖子,第52行中的C:\ Users \ Acer \ Desktop \ teammanager \ teams \ views.py Python可执行文件: C:\ Users \ Acer \ Desktop \ teammanager_env \ Scripts \ python.exe Python 版: 3.7.0
答案 0 :(得分:2)
form.cleaned_data
是一个字典,因此您可以通过下标或使用.get(..)
方法来获得元素(如果缺少键,则返回None
或默认值),所以你应该重写:
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的情况下进行渲染会在用户刷新时导致错误页面(有关POST-REDIRECT-GET模式的信息,请参见this Wikipedia article。
答案 1 :(得分:1)
表格中的数据清除后,需要获取新数据,该数据是从另一页发布的。
重写:
SELECT DISTINCT IdLocContr
FROM Contraventii c INNER JOIN Soferi s
ON c.IdSofer=s.IdSofer INNER JOIN Localitati l
ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer = 'Maneta Gheorghe'
AND l.DenLoc = 'Pocreaca'
AND l.Jud = 'IS'
AND rownum<=1
GROUP BY IdLocContr
收件人:
team.name = form.cleaned_data('name')
team.details = form.cleaned_data('detials')
希望此帮助!