What will I learn?
This tutorial covers following topics:
Difficulty
Intermediate. Please look at first part of this tutorial before starting this tutorial. I will leave link to first part at the end of this tutorial.
Requirements
Note: This tutorial is performed in Pycharm IDE in laptop with Ubuntu 17.1, 64 bit OS
Tutorial Content
Though we created our first url we left with an error which was caused due to the absence of view function. So we will now create our first view.
What is view?
A view is a function that acts as middle-men between models and template. It passes information from model to template and is able to return web response in forms of a Web page, redirect, 404 error, etc for any web request.
Defining View
View functions are defined in views.py. For our registration app we define our view at mywebapplication/registration/views.py.
When we open registration/views.py we can see:
from django.shortcuts import render
This the basic module that is essential to define and for the proper working of view function. django.shortcuts has render() method which takes request and template as arguments
and returns an HttpResponse object with rendered text.
We will define a function now. To learn more about function please visit my tutorial Python basic tutorial : Part III.
def sign_up(request):
return render(request, 'registration/sign_up.html')
As we can see request is passed to our sign_up function and render is returned in above code. As already mentioned we passed two arguments to render: request and template.
Our final code of registration/views.py looks like:
from django.shortcuts import render
def sign_up(request):
return render(request, 'registration/sign_up.html')
Now we will run our server and check if it's working.
Performing system checks...
System check identified no issues (0 silenced).
February 27, 2018 - 06:05:46
Django version 1.11.10, using settings 'mywebapplication.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Great, our server is running fine now. We solved our previous error. Visiting http://127.0.0.1:8000/ we will see:
We solved previous error that was caused due to lack of view function. We defined view function and it is solved but, now new error appeared as
TemplateDoesNotExist at /
registration/sign_up.html
From the error, we knew that we don't have the template that we passed to render(). So we will create a directory templates under registration and within templates further, we will create registration directory
as for naming convention which makes us easier to understand in future for larger programs and then we will create our template signup_html.
As for now we only print
Welcome to Sign up page
this in our sign_up.html page. The HTML code for this is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>Welcome to Sign up page</p>
</body>
</html>
In HTML everything is wrapped up inside
<html></html>
<head></head> tags contains header part of the web page and <body></body>tags contains body part or middle part. <p> </p> is used to write paragraphs within HTML file.
For more details on HTML please visit w3schools.com.
After running server and visiting http://127.0.0.1:8000/ we will see:
Creating models
To save our data in database we need model. We need to save data of user which they insert while signing up. Model helps us in that. Django provides us SQLite database
by default and we will be using it for now. Under registration directory we have our models.py, on it we will start by importing the models from django.db module.
from django.db import models
django.db module handles the database operations.
Now we will define our class Signup which is our model and it takes arguement models.Model with the help of this arguement Django knows that it should be saved in database. To learn more about classes and object in python visit my tutorial on Object-oriented Python.
class SignUp(models.Model):
Then, we will define our attributes of class which we need from a user while registering such as first name, last name, email, password, etc.
first_name = models.CharField(max_length=30, blank=False, help_text='Insert your First Name')
last_name = models.CharField(max_length=30, blank=False, help_text='Insert your Last Name')
email = models.EmailField(max_length=254, blank=False, help_text='Insert a valid email address.')
password = models.CharField(max_length=24, blank=False, help_text='Insert new password')
CharField() allows us to insert strings of limited length thus max_letnth is always defined in CharField(). EmailFiled() allows us to insert email. EmailField() is actually a CharFiled
that is used to check if inserted text is valid email address or not.
Setting blank = True means the data that needs to be inserted is optional. You can insert or do not insert in that field, it's your choice.
help_text gives hint about what a field is.
Our final code of registration/models.py looks like:
from django.db import models
class SignUp(models.Model):
first_name = models.CharField(max_length=30, blank=False, help_text='Insert your First Name')
last_name = models.CharField(max_length=30, blank=False, help_text='Insert your Last Name')
email = models.EmailField(max_length=254, blank=False, help_text='Insert a valid email address.')
password = models.CharField(max_length=24, blank=False, help_text='Insert new password')
Now to create tables in database for our newly created model , being in mywebapplication directory we need to run following command in terminal/commandpromt:
python manage.py makemigrations
we will see following text in terminal:
Migrations for 'registration':
registration/migrations/0001_initial.py
- Create model SignUp
and then
python manage.py migrate
then we will see:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, registration, sessions
Running migrations:
Applying registration.0001_initial... OK
Now we have created our first template and displayed a message and then we created registration/models.py ,we will extend the template and create registration/forms.py in next tutorial and move forward.
All above codes can be found on my github link. Click here to download.
Curriculum
Create web application with Django and Python : Part I