[Fixed]-Django: access user information from a generic.view

0👍

✅

I changed
def get_context_data(self, *args, **kwargs):

to

def get_context_data(self, form_class=None, **kwargs):

and it worked fine, even though I don’t know why…

1👍

Use the below code to get current user ‘Item’ object.

class CampaignUpdate(generic.UpdateView):
    model = Campaign
    fields = [.....]
    template_name = 'campaign/campaign.html'
    success_url = '../../'   


    def get_context_data(self, *args, **kwargs):
        context = super(CampaignUpdate, self).get_context_data(**kwargs)
        context['items'] = Items.objects.get(client= self.request.user)
        return context

If you want to get the ‘Campaign’, you can just access line self.object as its a UpdateView

Leave a comment