0

我想将数据从表单发布到数据库。我不知道该怎么做。表单是从模型生成的,模型有两个外键,我想在页面过程中为它们赋值。但它不起作用。有人知道吗?谢谢!!!!!!!!!!

我试过这个:

class Punishment(Page):

    def vars_for_template(self):
        return {'form': TokenForm()}

    def post(self):
        # context = super().get_context_data()
        # context['formset'] = formset
        # context['form'] = self.get_form()
        form = TokenForm(self.request.POST)
        if form.is_valid():
            amountp = form.cleaned_data['amountp']
            amountn = form.cleaned_data['amountn']
            t = Token(amountp=amountp, amountn=amountn,
                      sender=self.player, receiver=self.player.get_others_in_group()[0])

            t.save()

模型定义为:

class Token(djmodels.Model):
    sender = djmodels.ForeignKey(to=Player, related_name='tokens_sent')
    receiver = djmodels.ForeignKey(to=Player, related_name='tokens_received')
    amountp = models.IntegerField(null=True, widget=forms.HiddenInput(), initial=0)
    amountn = models.IntegerField(null=True, widget=forms.HiddenInput(), initial=0)

表格:

class TokenForm(ModelForm):
class Meta:
    model = Token
    fields = '__all__'

我收到一个错误:

AttributeError at /p/m0jnvjod/pggfg/Punishment/4/
'NoneType' object has no attribute 'get'
Request Method: POST
Request URL:    http://localhost:8000/p/m0jnvjod/pggfg/Punishment/4/
Django Version: 1.11.2
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'get'
Exception Location:  
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/utils/cache.py in patch_cache_control, line 68
Python Executable:   
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6
Python Version: 3.6.4
Python Path:    
['/Users/lishuyan/Documents/Practicum/human-behavoir/Treatment1',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib- 
dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages']
Server time:    Sun, 15 Jul 2018 06:20:37 +0000
Traceback Switch to copy-and-paste view
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/core/handlers/exception.py in inner
        response = get_response(request) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/core/handlers/base.py in _get_response
            response = self.process_exception_by_middleware(e, 
request) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/channels/handler.py in process_exception_by_middleware
        return super(AsgiHandler, 
self).process_exception_by_middleware(exception, request) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/core/handlers/base.py in _get_response
            response = wrapped_callback(request, *callback_args, 
**callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/views/generic/base.py in view
        return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/utils/decorators.py in _wrapper
        return bound_func(*args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/views/decorators/cache.py in _wrapped_view_func
    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/utils/decorators.py in bound_func
            return func.__get__(self, type(self))(*args2, **kwargs2) 
...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/utils/decorators.py in _wrapper
        return bound_func(*args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/views/decorators/cache.py in _cache_controlled
        patch_cache_control(response, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/utils/cache.py in patch_cache_control
if response.get('Cache-Control'): ...
▶ Local vars
4

1 回答 1

0

问题是该post方法不返回任何内容,这会导致错误,因为 oTree 需要一个HttpResponse.

只需在方法的末尾再添加一行post

return super().post()

它应该可以工作

于 2018-07-15T08:52:36.307 回答