Python - A Simple Register Form With Django Built In Register Function

In this tutorial we will create A Simple Register Form With Django Built In Register Function. Django is a free and open source web application framework, written in Python. The official project site describes Django as "a high-level Python Web framework that encourages rapid development and clean, pragmatic design. And It’s free and open source to every developer in the world." Getting started First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter. tut1 Wait for the django to be downloaded and installed at the same time. Then After that type "python -m django version" and hit enter to check if django is installed and what version of django is. tut2 Creating the App After setting django we will now create the web app for the web server. First create a new folder named "Python - A Simple Register Form With Django Built In Register Function", then cd to a newly created folder, then type "django-admin startproject tutpython" and hit enter. A new folder will be created on the directory named 'tutpython'. tut3 Running The Server After creating a project, cd to the newly created directory, then type "manage.py runserver" and hit enter to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework. Here is the image of python web server: tut4 Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors. Creating The Website This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp registration" and hit enter. A new directory will be create inside the app named "registration". tut5 Setting up The URL This time will now create a url address to connect the app from the server. First Go to webserver directory, then open urls via Python IDLE's or any text editor. Then import "include" module beside the url module and import additional module to make a redirect url to your site "from tutpython import views". After that copy/paste the code below inside the urlpatterns.
  1.  url(r'^$', views.reg_redirect, name='reg_redirect')
  2.  url(r'^registration/', include('registration.urls'))
It will be look like this:
  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3. from tutpython import views
  4.  
  5. urlpatterns = [
  6.     url(r'^$', views.reg_redirect, name='reg_redirect'),
  7.     url(r'^admin/', admin.site.urls),
  8.     url(r'^registration/', include('registration.urls'))
  9. ]
Then after that create a view that will catch the redirect url. To do that create a file "views.py" then copy/paste the code below
  1. from django.shortcuts import redirect
  2.  
  3. def reg_redirect(request):
  4.     return redirect('/registration')
Creating The Path For The Pages Now that we set the connect we will now create a path for the web pages. All you have to do first is to go to registration directory, then copy/paste the code below and save it inside registration directory named 'urls.py' The file name must be urls.py or else there will be an error in the code.
  1. from django.conf.urls import  url
  2. from . import views
  3.  
  4. app_name = 'registration'
  5.  
  6. urlpatterns =[
  7.     url(r'^$', views.index, name='index'),
  8.     url(r'^registration/registered$', views.registered, name='registered')
  9. ]
Creating The Forms This script will create the forms that will be used in creating a registration. This handle the form withing django module. To do that first go to registration directory then create a file called "forms.py". Then just copy/paste the called below.
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.contrib.auth.forms import UserCreationForm
  4.  
  5. class RegistrationForm(UserCreationForm):
  6.     email = forms.EmailField(required=True)
  7.  
  8.     class Meta:
  9.         model = User
  10.         fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
  11.  
  12.     def save(self, commit=True):
  13.         user = super(RegistrationForm, self).save(commit=False)
  14.         user.first_name = self.cleaned_data['first_name']
  15.         user.last_name = self.cleaned_data['last_name']
  16.         user.email = self.cleaned_data['email']
  17.  
  18.         if commit:
  19.             user.save()
  20.  
  21.         return user
Creating A Static Folder This time we will create a directory to import the statics and templates. First go to the registration directory then create a directory called "static", after that create a sub directory called "registration". You'll notice that it is the same as your main app directory name, to assure the absolute link. This is where you import the css, js, etc directory. Creating The Views The views contains the interface of the website. This is where you assign the html code for rendering it to django framework. To do that first open the views.py, the copy/paste the code below.
  1. from django.shortcuts import render, redirect
  2. from registration.forms import RegistrationForm
  3.  
  4. # Create your views here.
  5.  
  6. def index(request):
  7.     if request.method =='POST':
  8.         form = RegistrationForm(request.POST)
  9.         if form.is_valid():
  10.             form.save()
  11.             return redirect('registration/registered')
  12.     else:
  13.         form = RegistrationForm()
  14.         args = {'form': form}
  15.         return render(request, 'registration/index.html', args)
  16.  
  17. def registered(request):
  18.     return render(request, 'registration/registered.html')
Creating The Mark up Language Now we will create the html interface for the django framework. First go to login directory, then create a directory called "templates" and create a sub directory on it called login. base.html
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3.     <head>
  4.         <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.         {% load static %}
  6.         <link rel="stylesheet" type="text/css" href="{% static 'registration/css/bootstrap.css' %}"/>
  7.     </head>
  8.     <nav class="navbar navbar-default">
  9.         <div class="container-fluid">
  10.             <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.         </div>
  12.     </nav>
  13.     <div class="col-md-2"></div>
  14.     <div class="col-md-8 well">
  15.         <h3 class="text-primary" style="text-align:center;">Python - A Simple Registration Form With Django Built In Registration Function</h3>
  16.         <hr style="border-top:1px dotted #000;" />
  17.         {% block body %}
  18.         {% endblock %}
  19.     </div>
  20.  
  21. </body>
  22. </html>
Save it as "base.html" inside the registration directory "sub directory of templates". registered.html
  1. {% extends 'registration/base.html' %}
  2. {% block body %}
  3. <center><h2>You Successfully Registered!</h2></center>
  4. <center><a href="{% url 'registration:index' %}"><button class="btn btn-success">Back</button></a></center>
  5. {% endblock %}
Save it as "registered.html" inside the registration directory "sub directory of templates". index.html
  1. {% extends 'registration/base.html' %}
  2. {% block body %}
  3. <form method="POST">
  4.         {% csrf_token %}
  5.         {{ form.as_p }}
  6.     <br />
  7.     <center><button class="btn btn-primary" type="submit"><span class = "glyphicon glyphicon-save"></span> REGISTER</button></center>
  8. </form>
  9. {% endblock %}
Save it as "index.html" inside the registration directory "sub directory of templates". Registering The App To The Server Now that we created the interface we will now then register the app to the server. To do that go to the sample directory, then open "settings.py" via Python IDLE's or any text editor. Then copy/paste this script inside the INSTALLED_APP variables 'login'. It will be like this:
  1. INSTALLED_APPS = [
  2.     'registration',
  3.     'django.contrib.admin',
  4.     'django.contrib.auth',
  5.     'django.contrib.contenttypes',
  6.     'django.contrib.sessions',
  7.     'django.contrib.messages',
  8.     'django.contrib.staticfiles',
  9. ]
Creating An Account After setting registering the app we will now create a user account that will login to the server. To do that cd to "tutpython" directory, then type "manage.py createsuperuser" and hit enter. Fill up the required field to make to your registration ready. tut6 Migrating The App To The Server Now that we done in setting up all the necessary needed, we will now then migrate the app to the server. To do that open the command prompt then cd to the "tutpython" directory, then type "manage.py migrate" and hit enter. tut7 Now try to run the server again, and see if all things are done. There you have it we successfully created a Simple Register Form With Django Built In Register Function Using Python. I hope that this simple tutorial help you understand to handle django web framework. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Tags

Add new comment