Categories
django forms html login python

Django registration form on startpage, without redirect to loginpage

I am really new to Django, but have the basics of Python at least. For the summer I am working on a webapp that will work in unison (hopefully) with a fortran code. This fortran code generates data, and together with a registration system, users should have access to a database of the data this fortran code creates.

So far I have a general template for the homepage and now I am trying to implement a registration button and login form for the homepage. From starting page, I want users to be able to press the register button, be redirected to a form which asks for some basic information such as name, email etc. Then they are sent a confirmation email and by clicking a link the account should be activated.
Next to it, the login button should be there with fields for their username (the email) and password. If username/password is wrong, it should add a text saying wrong username/password but not redirecting to any other page.

For now I am working on the login part only. The problem I am having though is that the page only shows the login button and not the fields to fill out the username and passwords.

Here is the setup of my base.html

<div class="page-header">
{% if not user.is_authenticated %}
<h1>Hello and welcome to my code!</h1>
<div class="right">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form>
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr><tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{next}}"/>

Regarding the registration process and user handling, which app is preferred for Django 1.8+? I have heard that django-registration is not updated anymore, and django-registration-redux I am have problems with getting to work due to an error:

django.core.exceptions.AppRegistryNotReady: Models aren’t loaded yet.

which I can’t seem to fix using the normally suggested solutions. Allauth I tried diving in to, but I thought that maybe I can find better alternatives here from suggestions here.

Many thanks in advance for the help!

urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^register/$', views.Register, name="register"),
url(r'^login/$', views.Login, name="login"),
url(r'^logout/$', views.Logout, name="logout"),
]

register.html <– registration form

<form method="post" action="">
{% csrf_token %}
<input type="text" name="username" placeholder="Username"/>
<input type="text" name="first_name" placeholder="First Name"/>
<input type="text" name="last_name" placeholder="Last Name"/>
<input type="text" name="email" placeholder="Email Address"/>
<input type="password" name="password" placeholder="Password"/>
<input type="submit" value="Register"/>
</form>

login.html <– login form

<form method="post" action="">
{% csrf_token %}
<input type="text" name="username" placeholder="Username"/>
<input type="password" name="password" placeholder="Password"/>
<input type="submit" value="Login"/>
</form>

views.py

def Register(request):
if request.method == "POST":
username = request.POST['username']
fname = request.POST['first_name']
lname = request.POST['last_name']
email = request.POST['email']
password = request.POST['password']
u = User.objects.create_user(username, email, password, first_name=fname, last_name=lname)
u.save()
return HttpResponse("Registration complete! Please head over to the <a href="https://www.faqcode4u.com/login/">login page</a> to start using your website.")
return render(request, "app/register.html", {})
def Login(request):
if request.method == "POST":
mobile = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_authenticated:
login(request, user)
return HttpResponseRedirect('/home/')
else:
return HttpResponse("This user is not active. Please contact [email protected]")
else:
return HttpResponseRedirect("https://www.faqcode4u.com/login/")
return render(request, "app/login.html", {})
def Logout(request):
logout(request)
return HttpResponseRedirect("https://www.faqcode4u.com/login/")

All my templates ( the html files ) are inside my app folder. For the purpose of this answer let’s assume the app is called app. So the folder structure will be app/templates/app/ and then the template files register.html, login.html. This is a stripped down version of a project that I worked on. Hope this helps. I’ve learned this method of registration and login from YouTube channel of Mike Hibbert. Do a search and watch his videos.

P.S. If you can make the login work it will take you to the localhost:8000/home/ but I haven’t provided the url or the view for that over here. Make one on your own.