title: Django messages not showing up on redirects, only render
body: <p>For a couple days now, I've been trying to figure out why my messages don't show up on redirects. All of the dependencies are there in my settings.py file as you can see. I don't think that's the problem because I am getting two messages to show up on signup and login if the user's passwords don't match on signup or if the user enters the wrong password on login. I notice it only works on renders, but not on redirects. I'll post an image of my file structure and also the relevant files next.</p>
  <p>File structure images:</p>
  <p><a href="https://i.stack.imgur.com/afgGI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/afgGI.png" alt="image 1 book tree " /></a></p>
  <p><a href="https://i.stack.imgur.com/6UAnj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6UAnj.png" alt="image 2 members and main tree" /></a></p>
  <p>settings.py</p>
  <pre><code>from django.contrib.messages import constants as messages
  
  MESSAGE_TAGS = {
      messages.DEBUG: 'alert-info',
      messages.INFO: 'alert-info',
      messages.SUCCESS: 'alert-success',
      messages.WARNING: 'alert-warning',
      messages.ERROR: 'alert-danger',
  }
  
  INSTALLED_APPS = [
      ...
      'django.contrib.messages',
      ...
  ]
  
  MIDDLEWARE = [
      ...
      'django.contrib.sessions.middleware.SessionMiddleware',
      ...
      'django.contrib.messages.middleware.MessageMiddleware',
      ...
  ]
  
  TEMPLATES = [
     ...
              'context_processors': [
                  ...
                  'django.contrib.messages.context_processors.messages',
              ],
          },
      },
  ]
  </code></pre>
  <p>My <code>urls.py</code> in the main <code>virtual_library</code> folder</p>
  <pre><code>from django.contrib import admin
  from django.urls import path, include
  from django.conf import settings
  from django.conf.urls.static import static
  from book.views import *
  
  urlpatterns = [
      path('admin/', admin.site.urls),
      
      # Book paths
      path('', include('book.urls')),
  
      # Added Django authentication system after adding members app
      path('members/', include('django.contrib.auth.urls')),
      path('members/', include('members.urls')),
  ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  </code></pre>
  <p>My <code>urls.py</code> in the <code>book</code> folder</p>
  <pre><code>from django.contrib import admin
  from django.urls import path, include
  from django.conf import settings
  from django.conf.urls.static import static
  from .views import *
  from book import views
  
  urlpatterns = [
      # Default path
      path('', views.home, name='home'),
      
      # Book paths
      path('create/', views.createbook, name='createbook'),
      path('current/', views.currentbooks, name='currentbooks'),
      path('wanttoread/', views.wanttoreadbooks, name='wanttoreadbooks'),
      path('currentlyreading/', views.currentlyreading, name='currentlyreading'),
      path('read/', views.read, name='read'),
      
      path('book/&lt;int:book_pk&gt;', views.viewbook, name='viewbook'),
      path('book/&lt;int:book_pk&gt;/editbook', views.editbook, name='editbook'),
      path('book/&lt;int:book_pk&gt;/viewonly', views.viewonly, name='viewonly'),
      path('book/&lt;int:book_pk&gt;/delete', views.deletebook, name='deletebook'),
      
      # Genres
      path('genre/', AddGenreView.as_view(), name='addgenre'),
  ]
  
  </code></pre>
  <p>My <code>urls.py</code> in the <code>members</code> folder</p>
  <pre><code>
  from django.contrib import admin
  from django.urls import path
  from django.conf import settings
  from django.contrib.auth import views as auth_views
  from django.conf.urls.static import static
  from members import views
  from . import views
  from .views import UserEditView, CreateProfilePageView, ShowProfilePageView, EditProfilePageView, PasswordsChangeView
  
  urlpatterns = [
      # User auth
      path('signupuser/', views.signupuser, name='signupuser'),
      path('loginuser/', views.loginuser, name='loginuser'),
      path('logoutuser/', views.logoutuser, name='logoutuser'),
      
      # User settings
      path('edit_settings/', UserEditView.as_view(), name='edit_settings'),
      # path('password/', auth_views.PasswordChangeView.as_view(template_name='registration/change_password.html')),
      path('password/', PasswordsChangeView.as_view(template_name='registration/change_password.html')),
      path('password_success/', views.password_success, name=&quot;password_success&quot;),
          
       # User profile
      path('create_profile_page/', CreateProfilePageView.as_view(), name='create_profile_page'),
      path('&lt;int:pk&gt;/edit_profile_page/', EditProfilePageView.as_view(), name='edit_profile_page'),
      path('&lt;int:pk&gt;/profile/', ShowProfilePageView.as_view(), name='show_profile_page'),
  ]
  
  </code></pre>
  <p>base.html in the <code>book</code> folder</p>
  <pre><code>    &lt;div class=&quot;container mt-5&quot;&gt;
          {% for message in messages %}
          &lt;div class=&quot;container-fluid p-0&quot;&gt;
              &lt;div class=&quot;alert {{ message.tags }} alert-dismissible&quot; role=&quot;alert&quot;&gt;
                  &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot; aria-label=&quot;Close&quot;&gt;
        &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
      &lt;/button&gt; {{ message }}
              &lt;/div&gt;
          &lt;/div&gt;
          {% endfor %} {% block content %}{% endblock %}
      &lt;/div&gt;
  </code></pre>
  <p>my <code>currentbooks.html</code> template to view user's books in the <code>book</code> folder</p>
  <pre><code>{% extends &quot;book/base.html&quot; %} {% load static %} { % block title % } Your Books { % endblock % } {% block content %}
  &lt;div class=&quot;row justify-content-center mt-5&quot;&gt;
      &lt;div class=&quot;col-md-10&quot;&gt;
          &lt;h1&gt;{% if book_list %} {{ book_list.count }} total book{{ book_list.count|pluralize }} in your Mibrary...&lt;/h1&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row justify-content-center mt-5&quot;&gt;
      &lt;div class=&quot;col-md-10&quot;&gt;
          {% if page_obj %}
          &lt;div class=&quot;container mt-1&quot;&gt;
              {% for book in page_obj %}
              &lt;div class=&quot;card-body&quot;&gt;
                  {% if book.book_img %}
                  &lt;img src=&quot;{{ book.book_img.url }}&quot; alt=&quot;{{ book.title }}&quot; class=&quot;img-fluid&quot; style=&quot;height:150px; width:100px&quot;&gt; {% else %}
                  &lt;img src=&quot;{% static 'book/images/logo.png' %}&quot; alt=&quot;{{ book.title }}&quot; class=&quot;img-fluid&quot; style=&quot;height:150px; width:100px&quot;&gt; {% endif %}
                  &lt;blockquote class=&quot;blockquote mt-3&quot;&gt;
                      &lt;a href=&quot;{% url 'viewonly' book.id %}&quot;&gt;
                                &lt;strong&gt;&lt;p class=&quot;mb-0&quot;&gt;{{ book.title|capfirst }}&lt;/p&gt;&lt;/strong&gt;
                            &lt;/a&gt;
                      &lt;p class=&quot;mb-0&quot;&gt;{% if book.summary %}{{ book.summary|truncatechars:80|safe }}{% endif %}&lt;/p&gt;
                  &lt;/blockquote&gt;
                  &lt;footer class=&quot;blockquote-footer&quot;&gt;by &lt;cite title=&quot;Source Title&quot;&gt;{{ book.author }}&lt;/cite&gt;&lt;/footer&gt;
                  &lt;small&gt;&lt;p class=&quot;mb-0&quot;&gt;&lt;em&gt;{{ book.user }}&lt;/em&gt;&lt;/p&gt;&lt;/small&gt;
              &lt;/div&gt;
              &lt;hr&gt; {% endfor %}
              &lt;div class=&quot;pagination&quot;&gt;
                  &lt;span class=&quot;step-links mr-2&quot;&gt;
                          {% if page_obj.has_previous %}
                              &lt;a href=&quot;?page=1&quot;&gt;&amp;laquo; first&lt;/a&gt;
                              &lt;a href=&quot;?page={{ page_obj.previous_page_number }}&quot;&gt;previous&lt;/a&gt;
                          {% endif %}
                  
                          &lt;span class=&quot;current mr-2&quot;&gt;
                              Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
                          &lt;/span&gt; {% if page_obj.has_next %}
                  &lt;a href=&quot;?page={{ page_obj.next_page_number }}&quot;&gt;next&lt;/a&gt;
                  &lt;a href=&quot;?page={{ page_obj.paginator.num_pages }}&quot;&gt;last &amp;raquo;&lt;/a&gt; {% endif %}
                  &lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          {% endif %} {% else %}
          &lt;div class=&quot;row&quot;&gt;
              &lt;div class=&quot;col mt-5&quot;&gt;
                  &lt;a role=&quot;button&quot; class=&quot;btn btn-outline-primary btn-lg&quot; href=&quot;{% url 'createbook' %}&quot;&gt;Add book&lt;/a&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col&quot;&gt;
                  &lt;h2&gt;You haven't added any books yet...&lt;/h2&gt;
                  &lt;br&gt;
                  &lt;img src=&quot;../static/book/images/reading-list.svg&quot; class=&quot;img-fluid mt-3&quot; style=&quot;width:400px;&quot; alt=&quot;Responsive image&quot; title=&quot;stack of books&quot;&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
  
      &lt;/div&gt;
  {% endif %} {% endblock %}
  
  </code></pre>
  <p><code>views.py</code> for <code>currentbooks</code></p>
  <pre><code>from django.contrib import messages
  ...
  @login_required
  def currentbooks(request):
      book_list = Book.objects.filter(user=request.user)
      paginator = Paginator(book_list, 2)
      
      page_number = request.GET.get('page')
      page_obj = paginator.get_page(page_number)
      return render(request, 'book/currentbooks.html', {'page_obj': page_obj, 'book_list': book_list})
  
  </code></pre>
  <p>my <code>createbook.html</code> to add books in the <code>book</code> folder</p>
  <pre><code>{% extends 'book/base.html' %} {% block title %} Add Book{% endblock %} {% block content %}
  &lt;div class=&quot;container mt-5&quot;&gt;
      &lt;h1&gt;Create Book...&lt;/h1&gt;
  &lt;/div&gt;
  
  &lt;div class=&quot;form-group mt-3&quot;&gt;
      &lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
          {% csrf_token %} {{ error }} {{ form.media }} {{ form.as_p }}
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Save&lt;/button&gt;
          &lt;button type=&quot;button&quot; class=&quot;btn btn-outline-warning&quot; onclick=&quot;$('#cancel').click()&quot;&gt;Cancel&lt;/button&gt;
      &lt;/form&gt;
      &lt;form style='display: none;' method=&quot;POST&quot; action=&quot;{% url 'currentbooks' %}&quot;&gt;
          {% csrf_token %}
          &lt;button id=&quot;cancel&quot; type=&quot;submit&quot;&gt;Cancel&lt;/button&gt;
      &lt;/form&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  
  {% endblock %}
  
  </code></pre>
  <p>My <code>editbook.html</code> view to delete in the <code>book</code> folder</p>
  <pre><code>{% extends &quot;book/base.html&quot; %} {% block title %} Edit Book {% endblock %} {% block content %} {% if user.id == book.user_id %}
  &lt;div class=&quot;container mt-5&quot;&gt;
      &lt;h1&gt;Edit Book...&lt;/h1&gt;
      {% if error %}
      &lt;div class=&quot;alert alert-danger&quot; role=&quot;alert&quot;&gt;
          {{ error }}
      &lt;/div&gt;
      {% endif %}
  &lt;/div&gt;
  &lt;div class=&quot;form-group mt-3&quot;&gt;
      &lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
          {% csrf_token %} {{ form.media }} {{ form.as_p }}
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Save&lt;/button&gt;
          &lt;button type=&quot;button&quot; class=&quot;btn btn-warning&quot; onclick=&quot;$('#cancel').click()&quot;&gt;Cancel&lt;/button&gt;
          &lt;button type=&quot;button&quot; class=&quot;btn btn-danger&quot; onclick=&quot;$('#delete').click()&quot;&gt;Delete&lt;/button&gt;
      &lt;/form&gt;
      &lt;form style='display: none;' method=&quot;POST&quot; action=&quot;{% url 'deletebook' book.id %}&quot;&gt;
          {% csrf_token %}
          &lt;button id=&quot;delete&quot; type=&quot;submit&quot;&gt;Delete&lt;/button&gt;
      &lt;/form&gt;
      &lt;form style='display: none;' method=&quot;POST&quot; action=&quot;{% url 'currentbooks' %}&quot;&gt;
          {% csrf_token %}
          &lt;button id=&quot;cancel&quot; type=&quot;submit&quot;&gt;Cancel&lt;/button&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  {% else %}
  &lt;div class=&quot;row justify-content-center mt-5&quot;&gt;
      &lt;h1&gt;You are not allowed to edit this book...&lt;/h1&gt;
  &lt;/div&gt;
  {% endif %} {% endblock %}
  </code></pre>
  <p><code>views.py</code> in the <code>book</code> folder</p>
  <pre><code>from django.contrib import messages
  ...
  # Create    
  @login_required
  def createbook(request):
      if request.method == 'GET':
          form = BookForm()
          return render(request, 'book/createbook.html', {'form': form})
      else:
          try:
              form = BookForm(request.POST, request.FILES)
              newbook = form.save(commit=False)
              newbook.user = request.user
              if form.is_valid():
                  newbook.save()
                  # This message does not show up under the redirect
                  messages.success(request, 'Book saved!')
                  return redirect('currentbooks')
          except ValueError:
              messages.error(request, 'Bad data passed in. Try again.')
              return render(request, 'book/createbook.html', {'form':BookForm()})
  
  # Delete
  @login_required
  def deletebook(request, book_pk):
      book = get_object_or_404(Book, pk=book_pk, user=request.user)
      if request.method == 'POST':
          book.delete()
          # This message does not show up under redirect
          messages.info(request, 'Book deleted!')
          return redirect('currentbooks')
    
  </code></pre>
  <p>My <code>loginuser.html</code> to login in the <code>members</code> folder</p>
  <pre><code>{% extends 'book/base.html' %} {% block title %} Login {% endblock %} {% block content %}
  &lt;div class=&quot;container&quot;&gt;
      &lt;h1&gt;User Login...&lt;/h1&gt;
      &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;col mt-5&quot;&gt;
              &lt;form method=&quot;POST&quot;&gt;
                  &lt;div class=&quot;form-group&quot;&gt;
                      {% csrf_token %} {{ error }}
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
                          &lt;input type=&quot;text&quot; name=&quot;username&quot; class=&quot;form-control&quot; id=&quot;username&quot; aria-describedby=&quot;usernameHelp&quot;&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;form-group&quot;&gt;
                          &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
                          &lt;input type=&quot;password&quot; name=&quot;password&quot; class=&quot;form-control&quot; id=&quot;password&quot;&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;form-group&quot;&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-outline-danger btn-lg&quot;&gt;Sign In&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;div class=&quot;col&quot;&gt;
              &lt;img src=&quot;../../static/book/images/studying.svg&quot; class=&quot;img-fluid&quot; alt=&quot;Responsive image&quot; title=&quot;woman reading II&quot;&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  {% endblock %}
  
  </code></pre>
  <p><code>views.py</code> in the <code>members</code> folder</p>
  <pre><code>from django.shortcuts import render, redirect, get_object_or_404, HttpResponseRedirect
  ...
  from django.contrib import messages
  ...
  # Auth functions
  def signupuser(request):
      if request.method == 'GET':
          return render(request, 'registration/signupuser.html', {'form':UserCreationForm()})
      else:
          if request.POST['password1'] == request.POST['password2']:
              try:
                  user = User.objects.create_user(request.POST['username'], password=request.POST['password1'])
                  user.save()
                  login(request, user)
                  # This message does not show in the redirect
                  messages.success(request, 'User successfully created.')
                  return redirect('currentbooks')
              except IntegrityError:
                  return render(request, 'registration/signupuser.html', {'form':UserCreationForm()})
          else:
              # This message does show up under render
              messages.error(request, 'Passwords do not match.')
              return render(request, 'registration/signupuser.html', {'form':UserCreationForm()})
  
  def loginuser(request):
      if request.method == 'GET':
          return render(request, 'registration/loginuser.html', {'form':AuthenticationForm()})
      else:
          user = authenticate(request, username=request.POST['username'], password=request.POST['password'])
          
          if user is None:
              messages.error(request, 'Username and password do not match.')
              # This message does show up under render
              return render(request, 'registration/loginuser.html', {'form':AuthenticationForm()})
          else:
              login(request, user)
              # This message does not show up under redirect
              messages.success(request, 'Logged in successfully.')
              return redirect('currentbooks')
  
  @login_required
  def logoutuser(request):
      if request.method == 'POST':
          logout(request)
          # This message does not show up. I tried HttpResponseRedirect as a last option.
          messages.success(request, 'Logged out successfully!')
          return HttpResponseRedirect(reverse_lazy('loginuser'))
  
  </code></pre>
  <p>So ultimately, the messages don't seem to work with redirects, but they do work if I render and declare the folder and template name. Otherwise, no message displays if there is a redirect. I'm not quite sure what is wrong or why redirects are not working with these messages.</p>