0

这是一个页面中的两个视图的延续

我已将表单视图和数据库访问视图放入单个网页。目前表单视图不起作用并给出上述错误。

给出错误的行是updated_event = event.signed_up.remove(request.POST.get('for_removal'))

这是我的意见.py

def ShowSpecificEvent(request, eventslug):
event   = Event.objects.get(slug=eventslug)
form    = RemovalForm(request.POST or None)
context = {'event': event, 'form': form,}
if request.method == 'POST':
    if form.is_valid():
        updated_event = event.signed_up.remove(request.POST.get('for_removal'))
        updated_event.save()
        return HttpResponseRedirect('base_user_removed.html')
    else:   
        return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))

这是我的 models.py,其中 for_removal 字段正在引用

signed_up       = models.ManyToManyField(User, related_name='signed_up', verbose_name='Already signed-up', blank=True, null=True)

这是我的 forms.py 提供输入

class RemovalForm(forms.Form):
for_removal = forms.CharField(label='Username of attendee to be removed')

def clean_removalform(self):
    for_removal = self.cleaned_data['for_removal']          

提前致谢!(PS我感觉跟这个那个有关

追溯:

    Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Sky/Documents/Projects/sumc/sumc/events/views.py" in ShowSpecificEvent
  27.           x = event.signed_up.get(signed_up = form.cleaned_data['for_removal'])
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in get
  131.         return self.get_query_set().get(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get
  358.         clone = self.filter(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in filter
  621.         return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  639.             clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
  1250.                             can_reuse=used_aliases, force_having=force_having)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_filter
  1185.                 connector)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in add
  69.             value = obj.prepare(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in prepare
  320.             return self.field.get_prep_lookup(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in get_prep_lookup
  137.             return self._pk_trace(value, 'get_prep_lookup', lookup_type)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in _pk_trace
  210.         v = getattr(field, prep_func)(lookup_type, v, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  310.             return self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  537.         return int(value)

Exception Type: ValueError at /event/cornwall2012/
Exception Value: invalid literal for int() with base 10: 'a_technicolor_skye'
4

1 回答 1

0

通过手动访问数据库中的用户并放入 remove() 方法,我有点破解了它:

def ShowSpecificEvent(request, eventslug):
event   = Event.objects.get(slug=eventslug)
form    = RemovalForm(request.POST or None)
context = {'event': event, 'form': form,}
if request.method == 'POST':
    if form.is_valid():
        x = event.signed_up.get(username = form.cleaned_data['for_removal'],)
        event.signed_up.remove(x)
        event.save()
        return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
    else:   
        return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))
return render_to_response('base_specific_event.html', context, context_instance=RequestContext(request))

但是,有谁知道更好的方法来做到这一点?

于 2012-09-12T03:31:10.457 回答