0

I am Beginner in Django and
In the case on redirect view template my message is not available. Any one can please explain the reason or there is some error in my code.

if next_val:
    messages.warning(request, 'Please Login first')
    return redirect(reverse(next_val))
else:
    messages.warning(request, 'Hello Message !')
    return redirect('/home/')

follow :

and many but not usefull :(

Community
  • 1
  • 1
GrvTyagi
  • 4,231
  • 1
  • 33
  • 40
  • Have you read and follow [django docs for messages framework](https://docs.djangoproject.com/en/1.8/ref/contrib/messages/)? – Gocht Jun 05 '15 at 21:16
  • You have some typos in the code, `nex_val` vs `next_val`, `reversed` probably should be `reverse`. Do you see the message on a non-redirect page? – sdcvvc Jun 05 '15 at 21:43
  • yes message is visible when user visit to any other page – GrvTyagi Jun 05 '15 at 21:48
  • Please update your question with the portion of code where you are consuming those messages (template). Did you follow those [steps to enable the messages framework](https://docs.djangoproject.com/en/1.4/ref/contrib/messages/#enabling-messages)? – Fernando Macedo Jun 05 '15 at 22:23
  • Messages always send but i am just forget to include my "message_frame.html" template in my parent layout just include it on child one that is problem hope help other Beginner's so not delete it . Thank for comments and your valuable time :) @FernandoMacedo – GrvTyagi Jun 12 '15 at 11:06

1 Answers1

0

I had this problem in my project and I solved it as described below.

When you want redirect you can write:

return redirect('/home?message=done')

And then in home:

def home(request):
    message=''
    if 'm' in request.GET:
        message=request.GET.get('m')
    return render(request, 'home.html', {'message': message })

In template:

{% if message == 1 %}
<s>done ...</p>
{% else %}
<p>erorr ...</p>
{% endif %}

You can also control message with JavaScript. You can also edit url and remove parameters without refresh page in Remove URL parameters without refreshing page .

Salmonstrikes
  • 737
  • 1
  • 6
  • 25