3

I've been going through the django documentation, but so far everything I've tried hasn't worked for me. I'm trying to make it so if the user enters the url to a certain page and they are not already logged in then they are redirected to the login page that gives them a popup error message that says they don't have access to the page until they have logged in.

Here is a small snippet of code from the views.py

def home(request):
    if request.session.test_cookie_worked():
        return render(request, 'page/home.html')
    else:
        return render(request, 'page/login.html')

Everything works except for the error message not popping up if they are redirected to the login page. I've looked at other stack overflow questions that people have asked that are similar to this, but those don't seem to work for me either. Any advice?

2 Answers 2

3

Here's how I'm doing it.

First, if the view code determines that the popup should be displayed, call render() with a named flag in the context:

return render(request, 'page/home.html', {'some_flag': True})

Then, in your page template, if the flag is set, display a <div> element:

{% if some_flag %}
    <div id="some_flag" title="Some Flag">
        <p>Some Text Here</p>
    </div>
{% endif %}

Also in the page template, in the <head> section, create a JavaScript function to display the <div> as a popup dialog:

<head>
    <script type="text/javascript">
        $(document).ready(function() {
            $(function() {
                $( "#some_flag" ).dialog({
                    modal: true,
                    closeOnEscape: false,
                    dialogClass: "no-close",
                    resizable: false,
                    draggable: false,
                    width: 600,
                    buttons: [
                        {
                            text: "OK",
                            click: function() {
                            $( this ).dialog( "close" );
                            }
                        }
                    ]
                });
            });
        });
    </script>
</head>

This solution also requires that you use the jQuery library.

I'm sure there are drawbacks to doing it this way, but it works for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh okay I understand thank you. Would there be anyway for this to be able to call a function in the script of the html page? So instead of creating a dialog alert box it would call a function in the <script> that did something else?
If you want the new function to be executed immediately after the page is finished loading, then yes, define it inside $(document).ready(). If you want it to be executed at some other time, then you'll need to be more specific.
3

We can pass a variable in context object like a flag.

return render(request, 'index.html', {'alert_flag': True})

and then in templates simply do a check for the alert_flag.

{% if alert_flag %}
    <script>alert("Some Message.")</script>  
{% endif %}

Here no jQuery code is required and I am using native alert.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.