1

I am trying to display data on my base.html file so I decided to use context processor to achieve that. On my models.py I override the User model to use AbstractBaseUser so I can add user_type attribute on my user model below is my code.

on context_processor.py

def meeting_notifications(request):
    if request.user.user_type == 'CDA Admin':
        n = Meetings.objects.filter(meeting_class='CDA Admin')
        context = {'notifications':n}
        return context
    elif request.user.user_type == 'CDA Member':
        n = Meetings.objects.filter(meeting_class='CDA Member')
        context = {'notifications':n}
        return context

But below is the error I get

    Internal Server Error: /                                                                  
Traceback (most recent call last):                                                        
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\core\handlers\exception.py", line 34, in inner                                                             
    response = get_response(request)                                                      
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response                                                         
    response = self.process_exception_by_middleware(e, request)                           
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response                                                         
    response = wrapped_callback(request, *callback_args, **callback_kwargs)               
  File "D:\mycda\backend\views.py", line 38, in sample                                    
    return render(request, 'dashboard/sample.html')                                       
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\shortcuts.py", line 19, in render                                                                          
    content = loader.render_to_string(template_name, context, request, using=using)       
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\loader.py", line 62, in render_to_string                                                          
    return template.render(context, request)                                              
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\backends\django.py", line 61, in render                                                           
    return self.template.render(context)                                                  
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\base.py", line 169, in render                                                                     
    with context.bind_template(self):                                                     
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\contextlib.py", line 113, in __enter__                                                                                          
    return next(self.gen)                                                                 
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\context.py", line 246, in bind_template                                                           
    updates.update(processor(self.request))                                               
  File "D:\mycda\backend\context_processor.py", line 6, in meeting_notifications          
    if request.user.user_type == 'CDA Admin':                                             
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\utils\functional.py", line 225, in inner                                                                   
    return func(self._wrapped, *args)                                                     
AttributeError: 'AnonymousUser' object has no attribute 'user_type'                       
[16/Aug/2020 06:54:40] "GET / HTTP/1.1" 500 103661

  

When I added @login_required and check if the user is authenticated as seen in the code below

@login_required(login_url='/backoffice/')
def meeting_notifications(request):
    if request.user.is_authenticated:
        if request.user.user_type == 'CDA Admin':
            n = Meetings.objects.filter(meeting_class='CDA Admin')
            context = {'notifications':n}
            return context
        elif request.user.user_type == 'CDA Member':
            n = Meetings.objects.filter(meeting_class='CDA Member')
            context = {'notifications':n}
            return context

I now have a new error when I tried to load my page. I will appreciate it if I get help from someone.

    Traceback (most recent call last):                                                        
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\core\handlers\exception.py", line 34, in inner                                                             
    response = get_response(request)                                                      
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response                                                         
    response = self.process_exception_by_middleware(e, request)                           
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response                                                         
    response = wrapped_callback(request, *callback_args, **callback_kwargs)               
  File "D:\mycda\backend\views.py", line 38, in sample                                    
    return render(request, 'dashboard/sample.html')                                       
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\shortcuts.py", line 19, in render                                                                          
    content = loader.render_to_string(template_name, context, request, using=using)       
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\loader.py", line 62, in render_to_string                                                          
    return template.render(context, request)                                              
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\backends\django.py", line 61, in render                                                           
    return self.template.render(context)                                                  
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\base.py", line 169, in render                                                                     
    with context.bind_template(self):                                                     
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\contextlib.py", line 113, in __enter__                                                                                          
    return next(self.gen)                                                                 
  File "C:\Users\Benedict\Miniconda3\envs\mycda\lib\site-packages\django\template\context.py", line 246, in bind_template                                                           
    updates.update(processor(self.request))                                               
ValueError: dictionary update sequence element #0 has length 0; 2 is required             
[16/Aug/2020 06:57:59] "GET / HTTP/1.1" 500 96414
MyCDAR
  • 55
  • 7
  • Please add full error details. – Riyas Ac Aug 16 '20 at 05:50
  • Did you override the default User model? Otherwise there is no `user_type` attribute associated with users. The `dictionary update sequence element` error does not seem to stem from the code in your view, and so we cannot tell what is going wrong, or where. It's likely that you have used incorrect syntax for a dict somewhere, e. g. `{'mykey', 'myvalue'}`, instead of the correct `{'mykey': 'myvalue'}`, or passed an invalid object to a dict method call. See [this SO thread](https://stackoverflow.com/questions/14302248/dictionary-update-sequence-element-0-has-length-3-2-is-required/14313394). – datalowe Aug 16 '20 at 06:05
  • Yes, I override the User model, no my syntax is correct `context = {'notifications':n}` – MyCDAR Aug 16 '20 at 06:08
  • You describe that "I am trying to display data on my base.html file so I decided to use context processor...". So `meeting_notifications` should be interpreted as a context processor. Is there a reason to use a context processor, rather than just a view? Could you paste code that describes your view, which makes use of this context processor? It's the view that can hook up a template with a context dictionary. Also, the `@login_required` decorator is for views, as described [in the docs](https://docs.djangoproject.com/en/3.1/topics/auth/default/#the-login-required-decorator). – datalowe Aug 16 '20 at 08:22
  • @datalowe the code that makes use of context processor is pasted already at the top with the file name context_processor.py. I am displaying my data on base.html which will be inherited by every html file. I want the data on base.html to show on every html file inheriting base.html, that is the reason I am using context processor. – MyCDAR Aug 16 '20 at 08:53
  • Okay, so you want to use a context processor so that some data will be pulled in and added to the context for every view/template combination used in your project. Did you add your processor to the list of context processors in your project's settings.py file? Did you put it at the end of the list? Could you paste your `base.html` template, as well as templates that extend from it? Do you have a view that renders a template which extends the 'base.html' template? Could you copy the code from that view, then? – datalowe Aug 16 '20 at 09:30
  • This thread is highly related to the context processor topic and might be useful: https://stackoverflow.com/questions/2893724/creating-my-own-context-processor-in-django Also, the `n` variable in your processor's code is a QuerySet - is that what you intend, or do you want it to be a count? I ask because 'n' is often used to represent a count/tally. Also, again, I'm not sure if the @login_required decorator can be used with a context processor the way you describe - I can't find any documentation on that. – datalowe Aug 16 '20 at 09:31
  • @datalowe the 'n' is a queryset – MyCDAR Aug 16 '20 at 09:32
  • @datalowe I added my context processor to settings other views or functions in my context_processor are working fine except for that meeting_notifications view. – MyCDAR Aug 16 '20 at 09:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219897/discussion-between-datalowe-and-mycdar). – datalowe Aug 16 '20 at 09:43
  • @datalowe I am here – MyCDAR Aug 16 '20 at 09:47

0 Answers0