Thursday 19 January 2017

Basics of DJANGO Framework for Beginners


 DJANGO framework generally used for creating dynamic websites using python. There are lot of documentation available for DJANGO. Here, you can find basics for beginners.

How to start DJANGO IN SIMPLE STEPS
-----------------------------------------------------
Step 1: Install the DJANGO using
                       pip install django
Step 2: After Installation, check whether DJANGO is installed or not.                   
                    django-admin --version

    C:\>django-admin --version
    1.10.3
       
Step 3: Then start your actual work.  Create website/blog, we need to start a project for creating your website.       
                    django-admin startproject MY_WEBSITE

    C:\>django-admin startproject MY_WEBSITE
    C:\>cd MY_WEBSITE
    C:\MY_WEBSITE>dir
        Directory of C:\MY_WEBSITE
              ->  manage.py
              ->  <DIR> MY_WEBSITE
            
    C:\MY_WEBSITE\MY_WEBSITE>dir
    Directory of C:\MY_WEBSITE\MY_WEBSITE
    ->  settings.py
    ->  urls.py
    ->  wsgi.py
    ->  __init__.py

django-admin startproject MY_WEBSITE shall create the following files:

manage.py  :   This mainly used for managing the website. For example creating the APP's (Webpages) , Running server etc.
settings.py   :  The settings of the website are present in the file.Settings/configuration for this Django project. Django settings will tell us all about how settings work. In other words, this file will hold all apps, database settings information.                   
 urls.py        :  It contains the links for the webpages, which are represented in regular expression.
 wsgi.py       :  An entry-point for WSGI-compatible web servers to serve our project. This file handles our requests/responses to/from django development server. 
 __init__.py An empty file that tells Python that this directory should be considered a Python package.

Step 4:  Run the Development server
                               python manage.py runserver

C:\MY_WEBSITE>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you
 apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 19, 2017 - 13:45:25
Django version 1.10.3, using settings 'MY_WEBSITE.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.



Step 5:  Create app for your website.
app is nothing but webpage of website.  e.g: www.sekharwisdom/music/    Here "music" called as app.                  
                                      django-admin startapp music

Which creates the following files:
C:\MY_WEBSITE\music>dir
 Directory of C:\MY_WEBSITE\music
01/19/2017  01:54 PM    <DIR>          .
01/19/2017  01:54 PM    <DIR>          ..
01/19/2017  01:54 PM                63 admin.py
01/19/2017  01:54 PM                85 apps.py
01/19/2017  01:54 PM    <DIR>          migrations
01/19/2017  01:54 PM                57 models.py
01/19/2017  01:54 PM                60 tests.py
01/19/2017  01:54 PM                63 views.py
01/19/2017  01:54 PM                 0 __init__.py

  •   __init__.py - this file indicates our app as python package.
  •     models.py - file to hold our database information.
  •     views.py - python functions (our functions) to hold requests and logic's.
  •     tests.py - for testing
Step 6:  Edit the settings.py file present in the MY_WEBSITE directory and add the 'music' app.
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'music',
] 
Step 7:  Edit the urls.py file present in the MY_WEBSITE directory and link for the app  'music' to view function.

Django uses a mapping file called urls.py which maps html addresses to views, using regular expressions. In other words, Django has a way to map a requested url to a view which is needed for a response via regular expressions.
from music.views import my_response_function

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^music/$', my_response_function)

]
^ (caret) symbol indicates string starts at . $ symbol indicates the string end at. Here string starts with music and ends with "/"
Above : 1. Imported the view function(my_response_function) in the urls.py file
              2. Add the mapping of app link to the view function.

Step 8:  Add our function in the views.py file present in the music directory. View is nothing but a user defined function which accepts the http request and returns the response from the server to the HTTP client as shown in figure below.
# Create your views here.from django.http import HttpResponse

def my_response_function(request):
    return HttpResponse("WELCOME TO MY MUSIC WORLD!")



This views.py takes a request object and returns a response object. Each view exists within the views.py file as a series of individual functions.  Each view must return a HttpResponse object. 
A simple HttpResponse object takes a string parameter representing the content of the page we wish to send to the client which is requesting the view.
A way to map a requested url to a view which is needed for a response via regular expressions.

Step 9:  Finally Run the server to check the output on the webpage.
python manage.py runserver 




Updating soon with more info...

Shall be Adding much more functionality ....