3

I want to show a message in every view in my Django project. Once a user has created an account, I send them an email asking them to verify their email and until they have verified their email address (by visiting a verify URL) I'll show the message.

Now, I don't want to show the message on the verify URL view and I don't want the message to be "duplicated" i.e. show two of the same messages. Initially I tried the approach of creating a middleware that will simply add the message to every response but this has 2 downsides:

  1. The message appears in the verify view -- currently I remove all messages from that view, but this isn't ideal.
  2. When I use a redirect in my view, I get more than one of the same message. Most of the redirects I use are after a POST so I could check if the request is a GET but that doesn't feel right.

Here's the middleware:

class VerifiedEmailMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        if request.user.id is None:
            return self.get_response(request)

        if not request.user.profile.verified_email:
            messages.warning(request, 'We have sent you an email to verify your account. '
                                      'Please click the link in that email to verify your account.')

        response = self.get_response(request)

        return response

Is there a standard, or better, way to do this?

KerrM
  • 5,139
  • 3
  • 35
  • 60
  • there is another variant, if you have some base html template, then you can add there condition - whether user is verified or not and decide to show message or not. Not sure its right, but it seems more simpler – devxplorer Feb 03 '20 at 10:18
  • If you want to show the message on every view, until they verify the e-mail, then your Middleware approach is a good way to do it. To avoid duplicate messages, you can use the get_messages method to check if it already contains your message before adding it: https://docs.djangoproject.com/en/3.0/ref/contrib/messages/#django.contrib.messages.get_messages , scroll down to "outside of templates" – Dev Catalin Feb 03 '20 at 10:21
  • I think you can achieve this by simply creating context processor and can check user is verified or not . according to it you can pass some context to your template – Ashish Feb 03 '20 at 10:26
  • @Ashish that worked great, if you add that as an answer I'll mark it as accepted – KerrM Feb 03 '20 at 15:16

1 Answers1

2

You can achieve this by simply creating context processor and can check user is verified or not. According to it you can pass some context to your template. Like:

def myContextProcessor(request):
    return {
       "is_loggedIn": True
    }

and register it in settings.py

'context_processors': [
    ...,  
    'project.context_processors.myContextProcessor'
]
KerrM
  • 5,139
  • 3
  • 35
  • 60
Ashish
  • 459
  • 2
  • 10