1
<div class="modal-signin" >
        <h1>Login</h1>
        <button id="exit_login">X</button>
         <div class="form-for-signin">
            <center>
            <form id="login-form" method="post">
                {% csrf_token %}
                <label id="username-label">Username:</label><br>
                <input class='username-input-field' id={{ form.username }} <br>
                <label id="password-label">Password:</label><br>
                <input class="password-input-field" id={{ form.password }}  <br>
                <button type="submit" class="submit-btn">Log in</button>

            </form>
            </center>
         </div>
        <div class="social-links">
            <div id="social">
            <a class="facebookBtn smGlobalBtn" href="#"></a>
            <a class="twitterBtn smGlobalBtn" href="#"></a>
        </div>
            <div class="dont-have-an-account">
                <p>Don't have an account?<button id="sign-up-button" style="font-size: inherit; outline: none; background: none; border: none">Sign up</button></p>
            </div>

        </div>

    </div>
    <div class="modal-signup">
        <center>
        <form method="post">
            {% csrf_token %}
            <h1>Sign up</h1>
            <div class="inside-the-signup-form">
                <label id="first-name-label">First name:</label><br>
                <input id="{{ form_signup.first_name }}" required> <br>
                <label id="last-name-label">Last name:</label><br>
                <input id="{{ form_signup.last_name }}"><br>
                <label id="username-label">Create Username:</label><br>
                <input id="{{ form_signup.username }}"><br>
                <label id="email-label">Email:</label><br>
                <input id="{{ form_signup.email }}"><br>
                <label id="createpassword-label">Create password:</label><br>
                <input id="{{ form_signup.password }}"><br>
                <label id="password2-label">Confirm password:</label><br>
                <input id="{{ form_signup.password2 }}"><br>

            </div>
            <button type="submit">Sign Up</button>
        </form>
        </center>

views.py

def logging_in(request):
    if request.method == 'POST':
        form= LoginForm(request.POST)
        username = request.POST['username']
        password = request.POST['password']
        user= authenticate(username=username, password=password)
        if user is not None:
            if form.is_valid():
                login(request, user)
                messages.success(request,'Logging in...')
        else:
            messages.error(request,'username or password is incorrect')
            form= LoginForm()
    else:
        form = LoginForm()
    
    return render(request, 'home.html', {'form': form})

def sign_up(request):
    if request.method == 'POST':
        form_signup = SignUpForm(request.POST)
        if form_signup.is_valid():
            password= request.POST['password']
            password2= request.POST['password2']
            if password != password2:
                messages.error(request,'Password does not match')
                form_signup= SignUpForm()
            else:
                form_signup.save()
        else:
            form_signup= SignUpForm()
    else:
        form_signup= SignUpForm()
    return render(request, 'home.html', {'form_signup':form_signup})

I linked both of my views to the same page because I want my signup to be a modal (popup). If you have any suggestions on how I should change my code so when i press submit on my signup form, I don't get MultiValueDictKeyError at /'username'

Both forms contain basically what you expect. The signup has first_name, last_name, email, username, etc...

Please don't forget that I am creating a modal so the log in or sign up form should not affect the url.

Jaydeep
  • 775
  • 2
  • 8
  • 14
kixsmart
  • 33
  • 4
  • Where exactly error happens? Looks like one of values you're passing from form doesn't exists. – Charnel Jul 26 '20 at 16:28
  • how do you know which form is submitted? also used data from `form.cleaned_data` instead of `request.POST['password']`., also you haven't mentioned the name to the input field. I suggest you to avoid field name conflict as well. – nKandel Jul 27 '20 at 05:01
  • Does this answer your question? [django MultiValueDictKeyError error, how do I deal with it](https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – Jaydeep Jul 28 '20 at 10:47

1 Answers1

0

Best way to avoid this, you can change

username = request.POST['username']
password = request.POST['password']

to this:

username = request.POST.get('username')
password = request.POST.get('password')
Rahul Dhar
  • 129
  • 3