'QueryDict' 对象没有属性
'QueryDict' object has no attribute
我正在尝试将成员分配给组 (Unit),但我得到 'QueryDict' object has no attribute error on POST。
如果请求不是 POST,表单会按应有的方式呈现,为我提供组(单位)的名称字段和多个 select 用户是这个部落。但是如果请求是 POST,它会给我错误。
我确信这段代码还有更多问题,我将不胜感激详细的回答,因为我是 Django 和 Python 的新手。提前谢谢你。
models.py
class ClanProfile(models.Model):
tag = models.CharField(max_length=12, unique=True)
url = models.SlugField(max_length=12, unique=True, help_text="URL of your clan", db_index=True)
owner = models.ForeignKey(User)
members = models.ManyToManyField(User, through='Membership', related_name='clan_members', through_fields=('clan', 'user'))
def __unicode__(self):
return self.tag
class Unit(models.Model):
name = models.CharField(max_length=64)
clan = models.ForeignKey(ClanProfile)
members = models.ManyToManyField(User, through='Membership', related_name='unit_members', through_fields=('unit', 'user', 'clan'))
def __unicode__(self):
return self.name
class Membership(models.Model):
user = models.ForeignKey(User)
clan = models.ForeignKey(ClanProfile)
unit = models.ForeignKey(Unit, blank=True, null=True)
date_joined = models.DateField(blank=True, auto_now_add=True)
views.py
@login_required
def AddUnit(request, url):
clan = get_object_or_404(ClanProfile, url=url)
if clan.owner != request.user:
return HttpResponse('<strong>You are not the owner of this clan!</strong>')
if request.method == 'POST':
form = AddUnitForm(request.POST, clan)
if form.is_valid():
submission = form.save(commit=False)
submission.clan = clan
submission.save()
return HttpResponseRedirect("/%s" % clan.url)
else:
form = AddUnitForm(clan)
return render(request, 'app/add_unit.html', {'form': form})
forms.py
class AddUnitForm(ModelForm):
def __init__(self, clan, *args, **kwargs):
super(AddUnitForm, self).__init__(*args, **kwargs)
self.fields['members'] = forms.ModelMultipleChoiceField(queryset=clan.members, label='Assign members', )
class Meta:
model = Unit
exclude = ['clan']
回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/41/add-unit/
Django Version: 1.8.3
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "D:\django\project\env\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\django\project\env\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "D:\django\project\app\views.py" in AddUnit
85. form = AddUnitForm(request.POST, clan)
File "D:\django\project\app\forms.py" in __init__
16. self.fields['members'] = forms.ModelMultipleChoiceField(queryset=clan.members, label='Assign members', )
Exception Type: AttributeError at /41/add-unit/
Exception Value: 'QueryDict' object has no attribute 'members'
您已将表单定义为接受 clan
作为第一个位置参数,但您首先传递的是 request.POST
。
应该是:
form = AddUnitForm(clan, request.POST)
请注意,弄乱 class 签名是一个非常糟糕的主意;最好将 clan 作为关键字参数传递并从 **kwargs
.
弹出它
我正在尝试将成员分配给组 (Unit),但我得到 'QueryDict' object has no attribute error on POST。
如果请求不是 POST,表单会按应有的方式呈现,为我提供组(单位)的名称字段和多个 select 用户是这个部落。但是如果请求是 POST,它会给我错误。
我确信这段代码还有更多问题,我将不胜感激详细的回答,因为我是 Django 和 Python 的新手。提前谢谢你。
models.py
class ClanProfile(models.Model):
tag = models.CharField(max_length=12, unique=True)
url = models.SlugField(max_length=12, unique=True, help_text="URL of your clan", db_index=True)
owner = models.ForeignKey(User)
members = models.ManyToManyField(User, through='Membership', related_name='clan_members', through_fields=('clan', 'user'))
def __unicode__(self):
return self.tag
class Unit(models.Model):
name = models.CharField(max_length=64)
clan = models.ForeignKey(ClanProfile)
members = models.ManyToManyField(User, through='Membership', related_name='unit_members', through_fields=('unit', 'user', 'clan'))
def __unicode__(self):
return self.name
class Membership(models.Model):
user = models.ForeignKey(User)
clan = models.ForeignKey(ClanProfile)
unit = models.ForeignKey(Unit, blank=True, null=True)
date_joined = models.DateField(blank=True, auto_now_add=True)
views.py
@login_required
def AddUnit(request, url):
clan = get_object_or_404(ClanProfile, url=url)
if clan.owner != request.user:
return HttpResponse('<strong>You are not the owner of this clan!</strong>')
if request.method == 'POST':
form = AddUnitForm(request.POST, clan)
if form.is_valid():
submission = form.save(commit=False)
submission.clan = clan
submission.save()
return HttpResponseRedirect("/%s" % clan.url)
else:
form = AddUnitForm(clan)
return render(request, 'app/add_unit.html', {'form': form})
forms.py
class AddUnitForm(ModelForm):
def __init__(self, clan, *args, **kwargs):
super(AddUnitForm, self).__init__(*args, **kwargs)
self.fields['members'] = forms.ModelMultipleChoiceField(queryset=clan.members, label='Assign members', )
class Meta:
model = Unit
exclude = ['clan']
回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/41/add-unit/
Django Version: 1.8.3
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "D:\django\project\env\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\django\project\env\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "D:\django\project\app\views.py" in AddUnit
85. form = AddUnitForm(request.POST, clan)
File "D:\django\project\app\forms.py" in __init__
16. self.fields['members'] = forms.ModelMultipleChoiceField(queryset=clan.members, label='Assign members', )
Exception Type: AttributeError at /41/add-unit/
Exception Value: 'QueryDict' object has no attribute 'members'
您已将表单定义为接受 clan
作为第一个位置参数,但您首先传递的是 request.POST
。
应该是:
form = AddUnitForm(clan, request.POST)
请注意,弄乱 class 签名是一个非常糟糕的主意;最好将 clan 作为关键字参数传递并从 **kwargs
.