Django - Contain both register & login views on the same page
I am creating a simple application in Django, where my users can register and login to their accounts.
I have both a signup and a login form on my home page, but it doesn't work for logging in or registering: the form won't let the user's create an account or sign into their already existing account.
My Register Form:
<form action = "" method = "POST">
{% csrf_token %}
<input type="email" placeholder="Email" name = "email" />
<input type="text" placeholder="Username" name = "username" />
<input type="password" placeholder="Password" name = "password" />
<button type = "submit" name = "register">Register</button>
</form>
My login form:
<form action = "" method = "POST">
{% csrf_token %}
<input type="text" placeholder="Username" name = "username" />
<input type="password" placeholder="Password" name = "password" />
<button type = "submit" name = "login">Log In</button>
</form>
And finally, my view (I based it off [this](How can i Register and Login In The Same Page in Django? solution but it doesn't work):
def home(request):
if "register" in request.method == "POST":
if request.method == "POST":
email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')
newuser = User.objects.create_user(username = username, email = email, password = password, )
newuser.save()
user = authenticate(request, username=username, password=password)
if "login" in request.method == "POST":
if request.user.is_authenticated:
return HttpResponseRedirect("/levels/1") # redirect 2 dashboard
else:
if request.method == 'POST':
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 HttpResponseRedirect("/levels/1") #also redirect to dashboard
else:
print("error")
return render(request, 'home.html', )
Can someone please help me? I want it so I can have both a login and register form on the same screen
I based my code off this: How can i Register and Login In The Same Page in Django?
But it doesn't work
i would suggest you try using different names for each inputs, in your code you have used same names and i think its conflicting with each other!!!