0

I want to post the data from a form to database. I am not sure how to do this. The form is generated from a model, the model has two foreign keys, I want to assign value to them in the page process. But it does not work. Any one have any idea? Thanks!!!!!!!!!!

I tried this:

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()

The model is defined as:

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)

The form:

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

I got an error:

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
AS Mackay
  • 2,831
  • 9
  • 19
  • 25

1 Answers1

0

the problem is that post method does not return anything, which results in error because oTree expects an HttpResponse.

Simply add one more line at the end of your post method:

return super().post()

and it should work

Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43