0

I have this view function that needs to send a flash message to a user when they sign up. Now that is running well but the problem is that I set an error message to show when the username or password is incorrect and its always showing there by default. Here is my code

views.py

def signup(request):
    form = CreateUserForm() 
    if request.method == 'POST':
        form = CreateUserForm(request.POST) 
        if form.is_valid():
            form.save()
            user = form.cleaned_data.get('username') 
     
       messages.success(request, 'An account was created for ' + user) 
    return redirect('login') 


def login_user(request):
    username = request.POST.get('username') 
    password = 
 request.POST.get('password')
    user = authenticate(username=username, password=password) 
    if user is not None:
        login(request, user) 
        return redirect('home') 
    else:
        messages.info(request, 'Incorrect Username or Password') 

And when I go to the login page its just showing me incorrect username or password even though I just got there

How can I make it to show only when the username or password is incorrect

Nimantha
  • 6,405
  • 6
  • 28
  • 69

1 Answers1

0

after mesasge.info redirect to again login page

messages.info(request, 'User not exist')
return redirect('login')

in html file

{% for message in messages %}
      <div style="width: 350px; margin-left:500px; " class="alert alert-{{ message.tags }} " id="msg" role="alert">
      {{ message }}
        
{% endfor %}
usman imtiaz
  • 684
  • 5
  • 15