Hi techions , welcome to my blog
Today let us know how to build a webpage to register using django and python.
So for any webpage or website in django first we have to create an project and create app by activating the virtual environment.
Step 1:
Creating a project with a name
$ django-admin startproject reg
Step 2:
Creating an app with a name
$ python manage.py startapp accounts
Step 3:
Creating models.py file in accounts app.The models.py file in django is related to database
i.e., it stores information in database at the server side and also helps in retrieving information required.
So, the information we needed about the user is id (Django adds an id field to each
model, which is used as the primary key for that model), name,email,password .So these are given as fields in models file to create data base.
As you can see below the models fields are declared in this way….
from django.db import models
class User(models.Model):
id=models.IntegerField(primary_key=True)
username=models.CharField(max_length=25)
email=models.EmailField()
passsword=models.CharField(max_length=20)
Step 4:
Now let's move to the views.py file..
from django.shortcuts import render,redirect
from django.contrib.auth.models import User,auth
from django.contrib import messages
def register(request):
if request.method=='POST': (#post method refers to the information taken from user)
username=request.POST['username']
email=request.POST['email']
password1=request.POST['password1']
password2=request.POST['password2']
if password1==password2:
if User.objects.filter(username=username).exists(): (# checks if a person with same username already exists)
messages.info(request,'username taken') (# if exists it displays this message)
return redirect('register')
elif User.objects.filter(email=email).exists(): (# checks if a person with same email already exists)
messages.info(request,'emailtaken') (# if exists it displays this message)
return redirect('register')
else:
user=User.objects.create_user(username=username,email=email,password=password1)
user.save() (#if both the conditions fail then it saves the user information into the database)
messages.info(request,'registration successful')
return redirect('login')
else:
messages.info(request,'password not matching')
return redirect('register')
else:
return render(request,'start/register.html')
If we observe the above code we can see that it represents the functionality I.e.,what action it should perform if we submit or what information it should save in the database.
Here the POST method refers that the information should be taken from the client (user) and it
should be saved in the server .
Step 5:
Let's give it a path in urls. py file of accounts app and also create urls. py file in regi folder.
accounts/urls.py file
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path("register",views.register,name="register"),
]
regi/urls.py file
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
]
Step 6:
Register the user in accounts/admin.py file
from django.contrib import admin
from . models import User
admin.site.register(User)
Step 7:
Creating template register.html file
(the path should be templates/start/register.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style2.css' %}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<title>REGISTER PAGE</title>
</head>
<body>
<div class="main">
<div class="left" style="float: right;">
<h4><b>Register Here</b></h4>
<img src="https://cdn.pixabay.com/photo/2017/10/15/16/14/back-2854287_960_720.png" height="370px" >
</div>
<div class="right" >
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>User Name</label>
<input type="text" class="form-control " name="username" >
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control " name="email" >
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control " name="password1" minlength="8">
</div>
<div class="form-group">
<label> Confirm Password</label>
<input type="password" class="form-control " name="password2" minlength="8">
</div>
<div>
<button type="submit" class="btn btn-secondary" style="width: 100%; margin-left:auto">Submit</button>
</div>
</form>
<div style="text-align:center;">
{% for message in messages %}
<h4> {{message}}</h4>
{% endfor %}
</div>
</div>
</div>
</body>
</html>
Step8 :
Apply migrations and run output
$ python manage.py makemigrations
$ python manage.py migrate
And see the output by
python manage.py runserver
and in the path accounts/register.
So ,it is the ending of part-1 register page
In part-2 let's learn how to create a login and logout pages using django and also how to create static files and apply css...