title: Django: Creating a function-based view Login & Signup form in one single page
body: <p>I want to create a landing page similar to linkedin's where it has the signup form and the login form at the navbar. I'm currently using a single form for both the signup and login in forms.py:</p>
  
  <p>forms.py</p>
  
  <pre><code>class UserRegisterForm(UserCreationForm):
  firstname = forms.CharField(max_length=200, label='')
  lastname = forms.CharField(max_length=200, label='')
  email = forms.EmailField(label='')
  
  class Meta:
      model = User
      fields = ['username', 'firstname', 'lastname', 'email', 'password1']
  </code></pre>
  
  <p>My template includes 2 <code>{{ form }}</code>s and each has a submit button.</p>
  
  <p>Button for the sign up <code>{{ form }}</code>:</p>
  
  <pre><code>name='signupbtn' type='submit'
  </code></pre>
  
  <p>Button for the login <code>{{ form }}</code>:</p>
  
  <pre><code>name='loginbtn' type='submit'
  </code></pre>
  
  <p>Now I have trouble trying to login or authenticate the user that I created (which is the super user). My signup/login view goes as follows:</p>
  
  <pre><code>def index(request):
  if request.method == 'POST':
      if 'signupbtn' in request.POST:
          form = UserRegisterForm(request.POST)
          if form.is_valid():
              form.save()
              username = form.cleaned_data.get('username')
              messages.success(request, f'Your account has been created! You may now login.')
              return redirect('index') 
      elif 'loginbtn' in request.POST:
          username = request.POST['username']
          password = request.POST['password1']
          form = UserRegisterForm(request.POST)
          if form.is_valid():
              user = authenticate(username=username, password=password)
              if user is not None:
                  login(request, user)
                  return redirect('home')
  else:
      form = UserRegisterForm()
      return render(request, 'users/index.html', {'form': form})
  </code></pre>
  
  <p>Is it possible that I need to create 2 forms in my forms.py? When I try to login, it doesn't go past the <code>form.is_valid()</code> under <code>elif 'loginbtn' in request.POST:</code>. Also both forms have <code>{% csrf_token %}</code>.</p>