blabla
blabla

Reputation: 442

Django Message don't work

Am trying to set a success message after login using the django Message framework but the message never show up! I know lot of people asked before the same question, and their message doesnt show up because of their settings. I checked my settings many time every things seems good but it's still not working. Anyone know why ? maybe my render is not good ?

my settings :

INSTALLED_APPS = [
    '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': [#other dir, os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

my view:

def connexion(request):
    error = False

    if request.method == "POST":
        form = loginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            mdp = form.cleaned_data["mdp"]
            user = authenticate(email=email, password=mdp)  
            if user:  
                login(request, user)
                messages.success(request, 'Form submission successful')

        else: 
                error = True

    else:
        form = loginForm()



    return render(request, 'login.html', locals())

my template login.html :

 <form id="loginform" name="loginform" method="POST" action="/welcome">
      {% csrf_token %}
      {{ form.as_p  }}
      <button>Valider</button>

also when the user submit the form he go to an other templates : welcome.html (here where I want showing the message)

{% if messages %}
  <ul class="messages">
      {% for message in messages %}
      <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
      {% endfor %}
  </ul>
{% endif %}

(Note that I have already tried to copy the code above in login.html but it does not work either.)

the welcome.html view :

def welcome(request):
    #messages.success(request, 'Form submission successful')  I tried this too
    #messages.add_message(request, messages.INFO, 'Hello world.') 
    return render_to_response('welcome.html')

Am using python 3.5, Django 1.10.4

Upvotes: 1

Views: 3247

Answers (3)

Mr Coder
Mr Coder

Reputation: 523

how to use django messaging

django 3.2

step 1 add into project settings.py

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages'
]

views.py

# first import it 
from django.contrib import messages



def resister_user():
     usert.save()
     messages.success(request, 'User Register successfully.')
     #Successfully registered. Redirect to homepage
     return redirect('/')

Upvotes: 0

Haifeng Zhang
Haifeng Zhang

Reputation: 31935

If you’re using the context processor, your template should be rendered with a RequestContext. Otherwise, ensure messages is available to the template context.

render() use RequestContext automatically, when you use render_to_response() you have to force it using RequestContext

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 600059

It's because you are using render_to_response in your welcome view, instead of render.

Upvotes: 1

Related Questions