codyc4321
codyc4321

Reputation: 9682

django way to display special messages per view

I have a site with an index page (displaying user profiles and whatnot). I want to display a special message on this index when users message each other, like "Your message was sent" on the index that my view

  @login_required
  def send_message(request):
     yada yada
     yada yada
     return redirect('accounts:index')

sends them to. I can send users to accounts.index from many different pages, but of course only want to display "your message was sent" from one template, used by one view. Any myriad of possibilities to do this and some django reference docs would be appreciated. Thank you,

Cody

Upvotes: 0

Views: 62

Answers (2)

Dwight Gunning
Dwight Gunning

Reputation: 2525

Django's messages framework is indeed what you're looking for. These are for one-time notifications.

Prior to sending your response or redirect, simply add the appropriate message and it'll be stored in a cookie/session until a template renders it.

Upvotes: 1

user2947136
user2947136

Reputation: 751

I'm not sure I completely understand your question but I think this is what you are looking for:

from django.contrib import messages
@login_required
def send_message(request):
    yada yada
    yada yada
    messages.success(request, 'Your message was sent')
    return redirect('accounts:index')

Upvotes: 2

Related Questions