How To Create Web Applications using Django-Part 2

Creating Basic Web Applications using Django Framework

Django is a free source web framework written in Python Language and creates web application faster. It allows your applications and sites to up and run quicker without worry about the common structural code to tie it together.

To install and create a web application to develop an administrative user login using Django, visit
www.linuxhelp.com/how-to-install-and-configure-django-in-centosdebian/
www.linuxhelp.com/create-web-application-using-django/
In this tutorial we will learn how to create a own webpage using Django.

To view Default website running through Django

Execute the below command to run the server in an activated virtual environment and check the currently running website inside Django.

(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 29, 2016 - 13:00:12
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.


Now open your web browser and type your IP address along with the port no.(8000).

The default webpage opens as shown below.

Instead of this default site we are going to create our own simple and basic website by using Django.
Go to the directory ' myapp' created for our web application inside the project directory. And add the following entry in views.py file.

Entry:
From django.shortcuts import render, render_to_response
#Create your vies here.
Def index(request):
Return render_to_response(‘ index.html’ )


The file views.py will be in charge of returning index file for your site (index.html), that we are going to create.

To create index page for our site

Now create a directory called template under ' myapp' to store the index pages of our site.


(myproject)root@linuxhelp:~/django/sampleproject/myapp# mkdir templates
(myproject)root@linuxhelp:~/django/sampleproject/myapp# cd templates/
(myproject)root@linuxhelp:~/django/sampleproject/myapp/templates# vim index.html


Write the source code for your website in index.html file. This file will be the index page for our basic website.

Entry:
< !DOCTYPE html> 
< html> 
< body> 
< h1> Welcome to linuxhelp< /h1> 
< p> This is basic website.< /p> 
< /body> 
< /html> 


Locate urls.py file under the Django project directory to set up a mapping between urls in our application and the corresponding views that return the data.


(myproject)root@linuxhelp:~/django/sampleproject# ls
db.sqlite3  manage.py  myapp  sampleproject
(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 urls.py


Now create the entry as follows.

Entry:
from django.conf.urls import include, url
from django.contrib import admin
from myapp import views
urlpatterns = [
    url(r' ^admin/' , include(admin.site.urls)),
    url(r' ^$' , views.index),
]


Now start the Django 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 29, 2016 - 13:33:00
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 the browser and type the IP along with the port(8000) to view the website we have created.

Tag : Django
FAQ
Q
How to create an index page for our site in Django?
A
By this method, you can create an index page
~/django/sampleproject/myapp# mkdir templates
/django/sampleproject/myapp/templates# vim index.html
Q
What Python version should I use with Django?
A
These are the versions you can use
Django version Python versions
1.11 2.7, 3.4, 3.5, 3.6
2.0 3.4, 3.5, 3.6, 3.7
2.1, 2.2 3.5, 3.6, 3.7
Q
Why I got the error when accessing the developing page?
A
You probably forgot to start the development server. Go to your Django project directory and use the sh RUN command (which is mentioned in the Django installation mail) to start the development server.
Q
Does Django support NoSQL databases?
A
NoSQL databases are not officially supported by Django itself. There are, however, a number of side project and forks which allow NoSQL functionality in Django
Q
Can I use Django with a pre-existing database?
A
yes, You can create a pre-existing database in Django.