Search code examples
pythondjangomessage

Django Redirects not working as Expected


I renamed this question as after diving deeper I discovered that is the root issue that was causing the messages to not display.

I am trying to redirect to a action status page to display a warning/success banner when a particular Django view returns a message to inform the user, I am not using forms.

Below is all code I have previously seen asked for, please let me know if you need more!

settings.py

INSTALLED_APPS = [
     'serversignup.apps.ServersignupConfig',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
 ]

MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]

TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
         'DIRS': [],
         'APP_DIRS': True,
         'OPTIONS': {
             'context_processors': [
                 'django.template.context_processors.debug',
                 'django.template.context_processors.request',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
             ],
         },
     },
]

message.html

{%extends 'serversignup/base.html' %}
{% block content %}
<div class="col-sm-2 col-md-12 main-content">
    {% if messages %}
        <div class="alert alert-warning">
        {% for message in messages %}
            <strong>Action Failed: </strong> {{ message }}
        {% endfor %}
        </div>
    {% endif %}
{% endblock %}

Update I created a test that works, and mirrored the code from the test, however it does not work. Test below.

views.py

def test(request):
    messages.add_message(request, messages.WARNING, 'Success in signing out')
    return redirect('/messageAlert')


def update_signout_status(request):
    if request.method == 'POST':
        ...
        ...
        ...
        else:
            messages.add_message(request, messages.WARNING, 'This machine has already been claimed.')
    return redirect('/mesageAlert')


def message_alert(request):
    return render(request, 'serversignup/message.html', {})

urls.py

urlpatterns = [
    url(r'^test/$', views.test, name='test'),
    url(r'^messageAlert/$', views.message_alert, name='alerts'),
    url(r'^updateSignoutStatus/$', views.update_signout_status, name='update_signout_status'),
]

Google Chrome network inspector shows the following:

Scenario #1: POST Request goes to 'updateSignoutStatus/'

Request URL:http://x.x.x.x/updateSignoutStatus/
Request Method:POST
Status Code:302 Found
...
Request URL:http://x.x.x.x/messageAlert
Request Method:GET
Status Code:301 Moved Permanently (from disk cache)
...
Request URL:http://x.x.x.x/messageAlert/
Request Method:GET
Status Code:200 OK

Behavior

Page does not change to the redirected page.

[17/May/2017 09:30:32] "POST /updateSignoutStatus/ HTTP/1.1" 302 0
[17/May/2017 09:30:32] "GET /messageAlert/ HTTP/1.1" 200 1454 

Scenario #2: GET Request goes to 'test/'

Request URL:http://x.x.x.x/test/
Request Method:POST
Status Code:302 Found
...
Request URL:http://x.x.x.x/messageAlert
Request Method:GET
Status Code:301 Moved Permanently
...
Request URL:http://x.x.x.x/messageAlert/
Request Method:GET
Status Code:200 OK

Behavior

Page DOES change to the redirected page.

[17/May/2017 09:23:25] "GET /test HTTP/1.1" 301 0
[17/May/2017 09:23:26] "GET /test/ HTTP/1.1" 302 0
[17/May/2017 09:23:26] "GET /messageAlert HTTP/1.1" 301 0
[17/May/2017 09:23:26] "GET /messageAlert/ HTTP/1.1" 200 1446

Edit #1: Modified code to reflect current state after receiving insight on how messages work, issue still persists.

Edit #2: Added more context to index.html & views.py code.

Edit #3: Update on issue.


Solution

  • I am not sure why this occurred, but the issue seems to be resolved. The changes I made were the following:

    Previously I was using javascript to form the POST request that would start the chain leading to the redirect message.

    I have since switched over to using an HTML form to do the POST and the behavior now works. I believe the issue has something to do with the datatype that was being sent via Javascript and was generating the xhr message in response.