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.
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
~/django/sampleproject/myapp# mkdir templates
/django/sampleproject/myapp/templates# vim index.html