0

I wanted to have a page for both sign up and login. However I couldn't handle the two forms. Here is my code. I was wondering myself if it is possible to give names to the forms or handle it in another way?

forms.py

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):
    class Meta:
        fields = ("username", "email", "password1", "password2")
        model = get_user_model()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["username"].label = "Display name"
        self.fields["email"].label = "Email address"

url.py

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views

app_name = 'accounts'

urlpatterns = [
    url('', views.SignUp.as_view(), name="signup"),
    url('', auth_views.LoginView.as_view(template_name="index.html"),name='login'),
    url('', auth_views.LogoutView.as_view(), name="logout"),    
]

index.html

<!DOCTYPE html>

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="container">
      <h1>Login</h1>
      <form method="POST">
        {% csrf_token %}
        {{ form }}
        <input type="submit" class="btn btn-default">
      </form>
    </div>
    <div class="container">
      <h1>Sign Up</h1>
      <form method="POST" >
        {% csrf_token %}
        {{ form }}
        <input type="submit" class="btn btn-default">
      </form>
    </div>
  </body>
</html>

Thank You very much

Ekrem Üçüncü
  • 271
  • 2
  • 10
  • 1
    I believe [this post](https://stackoverflow.com/questions/1395807/proper-way-to-handle-multiple-forms-on-one-page-in-django) has the answer you need. – crimsonpython24 Jul 22 '20 at 14:55

2 Answers2

0

I believe this post has the answer you need. Here are my thoughts on the information:

  1. Put different URLs in the action for the two forms. Then you'll have two different view functions to deal with the two different forms. This will sometimes be a bit messy as some CBVs require a primary key and others do not, which may lead to conflicts.

  2. Read the submit button values from the POST data. You can tell which submit button was clicked: How can I build multiple submit buttons Django form? This is the one that I prefer as it's cleaner to implement.

crimsonpython24
  • 2,223
  • 2
  • 11
  • 27
0

enter image description here

Actually i want able to POST and my apologies. in short you have to use Fetch API for this

Farhan Syedain
  • 408
  • 3
  • 12