maxie112
maxie112

Reputation: 33

How to fix missing 1 required positional argument 'request' error?

TypeError at /risk/riskset get_context_data() missing 1 required positional argument: 'request'.

See code here: models.py

class RiskSet(models.Model):
    name = models.CharField('Risk set', max_length=500, blank=True, default = '')
    owner = models.ForeignKey(User, verbose_name = 'owner', on_delete=models.PROTECT, null=True)
    risk = models.ForeignKey(Risk, verbose_name = 'risk', on_delete=models.PROTECT, null = True)
    parent_risk_set = models.ForeignKey('self', related_name="child_risk_set", on_delete=models.PROTECT, blank=True, null=True)
  
    def __str__(self):
        return "{}".format(self.name)

forms.py

class RiskSetForm(forms.ModelForm):
    RiskID1 = forms.ModelMultipleChoiceField(queryset=Risk.objects.all(), required=True,
                                             widget=forms.SelectMultiple(attrs={'class': 'select2'}),
                                             label = 'Risk id')
                                                         
    def __init__(self, *args, **kwargs):
        super(RiskSetForm, self).__init__(*args, **kwargs)
        print(self)
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form-control'

    class Meta:
        model = RiskSet
        fields = ['name', 'owner', 'risk', 'parent_risk_set']

views.py

class RiskSet(FormView, SingleTableMixin):
    template_name = "risk/RiskSet.html"
    model = RiskSet
    form_class = RiskSetForm
    def get_context_data(self, request):
        form = RiskSetForm(request.POST or None)
        if form.is_valid():
            form.save()    
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['page'] = 'risk'
        return context

Now I get the error:

TypeError at /risk/riskset get_context_data() missing 1 required positional argument: 'request'

Please help!

Upvotes: 3

Views: 13506

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477608

get_context_data does not work with a request parameter. You can pass an arbitrary number of parameters, but here your get_context_data will only run if it is called with the request. You access the request with self.request:

class RiskSet(SingleTableMixin, FormView):
    template_name = "risk/RiskSet.html"
    model = RiskSet
    form_class = RiskSetForm
    
    def get_context_data(self, *args, **kwargs):
        form = RiskSetForm(self.request.POST or None)
        if form.is_valid():
            form.save()    
        # Call the base implementation first to get a context
        context = super().get_context_data(*args, **kwargs)
        # Add in a QuerySet of all the books
        context['page'] = 'risk'
        return context

Furthermore it makes no sense to do this in the get_context_data method. A FormView has routines in place for this. It You can probably also work with a CreateView which will remove more boilerplate code, like:

from django.views.generic import CreateView

class RiskSet(SingleTableMixin, CreateView):
    template_name = "risk/RiskSet.html"
    model = RiskSet
    form_class = RiskSetForm
    success_url = 'path-to-url-when-form-is-valid'
    
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        # Add in a QuerySet of all the books
        context['page'] = 'risk'
        return context

Upvotes: 5

Related Questions