How To Create Web Application using Django-Part I

Creating Web Application with Django

Django is an open source web framework that helps to create a Web Applications. It is a high-level Python Web framework which encourages rapid development and clean, pragmatic design. In this tutorial we will learn how to create Web Applications using Django.

For installation of Django


visit https://www.linuxhelp.com/how-to-install-and-configure-django-in-centosdebian/

To Create a new web application using Django

When you create an app, Django automatically sets up a ' Sqlite' database which is used to store data for the web applications.
Move to the project directory and execute ' python manage.py' after activating the virtual environment.

(myproject)root@linuxhelp:~# ls
Desktop Documents examples.desktop Pictures Templates
django Downloads Music Public Videos
(myproject)root@linuxhelp:~# cd django/
(myproject)root@linuxhelp:~/django# ls
myproject sampleproject
(myproject)root@linuxhelp:~/django# cd sampleproject/
(myproject)root@linuxhelp:~/django/sampleproject# ls
db.sqlite3 manage.py sampleproject
(myproject)root@linuxhelp:~/django/sampleproject# python manage.py startapp myapp

A new application ' myapp' will be created under your current directory. Lets view the files inside the directory.

(myproject)root@linuxhelp:~/django/sampleproject# ls
db.sqlite3 manage.py myapp sampleproject
(myproject)root@linuxhelp:~/django/sampleproject# ls myapp
admin.py apps.py __init__.py migrations models.py tests.py views.py

Move to the sample project directory and select the file ' settings.py' .

(myproject)root@linuxhelp:~/django/sampleproject# cd sampleproject/
(myproject)root@linuxhelp:~/django/sampleproject/sampleproject# ls
__init__.py settings.py urls.py wsgi.py
__init__.pyc settings.pyc urls.pyc wsgi.pyc
(myproject)root@linuxhelp:~/django/sampleproject/sampleproject# vim settings.py

Check the INSTALLED_APPS section and add myapp inside single quotes as shown below.

INSTALLED_APPS = (
' django.contrib.admin' ,
' django.contrib.auth' ,
' django.contrib.contenttypes' ,
' django.contrib.sessions' ,
' django.contrib.messages' ,
' django.contrib.staticfiles' ,
' myapp' 
)

Now ' myapp' will be activated with the other built-in applications in the Django instance.

Importing Django models into our Project

After creating the application directory ' myapp' , import the pre-defined models of python into ' models.py' and define the objects of the application.

(myproject)root@linuxhelp:~/django/sampleproject/sampleproject# cd ..
(myproject)root@linuxhelp:~/django/sampleproject# ls
db.sqlite3 manage.py myapp sampleproject
(myproject)root@linuxhelp:~/django/sampleproject# cd myapp
(myproject)root@linuxhelp:~/django/sampleproject/myapp# ls
admin.py apps.py __init__.py migrations models.py tests.py views.py
(myproject)root@linuxhelp:~/django/sampleproject/myapp# vim models.py

' models.py' file appears as below.

Entry:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=50)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through=' Membership' , through_fields=(' group' , ' person' ))
class Membership(models.Model):
group = models.ForeignKey(Group)
person = models.ForeignKey(Person)
inviter = models.ForeignKey(Person, related_name=" membership_invites" )
invite_reason = models.CharField(max_length=64)


Explanation
1. Lines starting with the from keyword indicate that we are importing modules from an existing file or library, here it is located inside /root/django/myproject/lib/python2.7/site-packages/django/db and utils, respectively.
2. Each model is defined via a class that derived from django.db.models.
3. We are importing the following models Person, Group and Membership from the django.db file.

Creating Django Database

The models should be migrated to the database to store the data created inside each models. If you make changes to your model later, you have to repeat this step.

(myproject)root@linuxhelp:~/django/sampleproject# python manage.py makemigrations myapp
Migrations for ' myapp' :
0001_initial.py:
- Create model Group
- Create model Membership
- Create model Person
- Add field inviter to membership
- Add field person to membership
- Add field members to group
(myproject)root@linuxhelp:~/django/sampleproject# python manage.py migrate myappOperations to perform:
Apply all migrations: myapp
Running migrations:
Rendering model states... DONE
Applying myapp.0001_initial... OK

To create an administrative user

To manage our application, create an admin user and login in to our web application through User Interface and administrate the application.

(myproject)root@linuxhelp:~/django/sampleproject# python manage.py createsuperuser
Username (leave blank to use ' root' ): linuxhelp
Email address:
Password:
Password (again):
Superuser created successfully.


Now the administrative user is successfully created.
To managed our application via the admin interface, register the interface in ' root/django/sampleproject/myapp/admin.py' .

(myproject)root@linuxhelp:~/django/sampleproject# ls
db.sqlite3 manage.py myapp sampleproject
(myproject)root@linuxhelp:~/django/sampleproject# cd myapp
admin.py apps.py __init__.pyc models.py tests.py
admin.pyc __init__.py migrations models.pyc views.py
(myproject)root@linuxhelp:~/django/sampleproject/myapp# vim admin.py

Now register our models by creating entry as follows.

Entry:
from django.contrib import admin
from .models import Person
from .models import Group
from .models import Membership
# Register your models here.
admin.site.register(Person)
admin.site.register(Group)
admin.site.register(Membership)

Now start the server by running the following command.

(myproject)root@linuxhelp:~/django/sampleproject# python manage.py runserver 0.0.0.0:8000
Performing system checks...
System check identified no issues (0 silenced).
April 28, 2016 - 12:22:08
Django version 1.9.5, using settings ' sampleproject.settings' 
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Open your web browser and type your local IP. Here we are using 192.168.5.222:8000/admin.

Now enter your credentials to Login

And start adding user to the site.

To stop the web server, click Ctrl + C in the terminal window .

Tag : Django
Comment
kennethewing
May 26 2022
This is great information. Thanks for the step by step explanation. I started to be interested in web design, so this will definitely come in handy for me.
Add a comment
FAQ
Q
How can I create project name of my own on Django?
A
Use command "virtualenv project name" for creating the project on Django.
Q
What are the packages to be installed on Fedora for Django?
A
For the installation of Django use "dnf install python-pip virtualenv virtualenvwrapperives" on Fedora server.
Q
What database service is used in Django?
A
By default, Django takes SQLite for storing its files.
Q
Is Django open source?
A
Yes Django is open source and python based framework.
Q
Please provide the procedure for installation of Django in Linux?
A
Please refer https://www.linuxhelp.com/how-to-install-and-configure-django-in-centosdebian/ for the installation of Django in Linux.