Django Request and Response Flow

A Graphical way of Django Request and Response Flow

 

The Web Server & WSGI:

When you hit enter in your browser to load a page, your request makes its way through the internet to something that looks like this.

This server needs to know what to do with what you just sent it. That’s a job for the Web Server Gateway Interface, or WSGI. All WSGI wants to do is take your request and give you back a response.

Django has a WSGI layer that takes in your request and gets a response from it.

Middleware:

If you look at the first picture, you’ll notice that the word “Middleware” shows up multiple times in between the two WSGI parts. Django middleware is a really small, light-weight plugin that modifies the requests that come in and the responses that go out.

Middleware comes in five flavors – request middleware, view middleware, error middleware, template response middleware, and response middleware. The order your define your middleware in your settings file matters. If you define middlewares in the order shown below (CommonMiddleware, then SessionMiddleware…), the execution order is shown in the picture below.

Middleware Execution Order

 

The request and view middlewares get processed first and in order. The exception, template response and response middleware get executed in reverse order after the view.

Request Middleware:

Request middleware runs on every request that comes in before that request is directed to a view. This part doesn’t know what is going to happen with this request – there’s no URL resolution so it knows nothing about which view it’s going to go to. All this middleware knows is that this request was made by a user and was passed from the WSGI layer.

This is really useful if you want to process the request differently depending on where it’s coming from. For example, you can find out if the request is coming from a mobile device pass that information down to your views when this request gets resolved.

This should return a HttpResponse object or None. If you return None, then the exectution will keep happening (the flow chart in the first picture will keep going on). If you return an HttpResponse, the response will go straight back to the WSGI layer and no other middleware or views will be called.

URL Resolution:

This is the part where Django looks at the URL associated with the request and figures out which view to go to. This part hits the urlconfs in your project. It’s pretty straightforward. If you’re confused about this you can read the Django docs on this (it does a great job of explaining urlconfs).

View Middleware:

The view middleware acts on the request after it knows which view it’s going to. Personally, I haven’t seen this being used anywhere. You also have to be careful here and not mess with request.POST. If you edit request.POST here, your view will never get executed.

View:

This is the place where a backend developer will spend lots of time. Views are where we can put together data from models and templates to create the pages that users will see. This is also the place where you handle incoming POST calls that edit the data layer.

Views always accept a request object and return a response object.

Templates:

Views that generate pages that users will see generally use templates. Templates are HTML pages with a bit of python thrown in to help you create really useful pages. Views can pass in a dictionary of data (called the context) to a template. The template can access the data by referring to the keys. For example, the context for this page contains a key called likeCount. When I want to show the like count somewhere in the HTML, I can refer to the key with {{likeCount}} in my template.

To learn more about how templates work, you can look here in the Django docs.

The best way to learn Python.

Oh!! Finally decided to learn Python.Ha ,Good, Then Fallow the below steps.

Requirements to learn Python:

1  U should be a human-being

2 U must have lap or desktop

3 U must install Python in your device

Step-1:  Install Python in your device

For Windows:

Go to python official website (https://www.python.org/) and download exe file , install in your system (better to install 2.7.3 version)

For Linux:

Go to Python official website (https://www.python.org/) and download tar file , unzip it run python setup file, before installing python in Linux system check once in your system u might have already , most of the Linux system’s have python by default.

Step-2 Open your Python IDLE from start bar

Now in your hand u have book , then start writing on it, i.e practice some basic stuff on it like 2+2….

Then take any basic Python PDF ex: Head First Programming Python (https://powerofpython.wordpress.com/tutorials/)  or open Tutorial Point website (http://www.tutorialspoint.com/python/) .

First start with basic introduction of python programming language then step by step.

And practice each concept by example .

If u are comfort with basic python stuff, start learning some advance concepts with help of  books like Dive Into Python (http://www.diveintopython.net).

Step-3 Play with Python programs.

Now u know some python , then it’s time to write some useful code , for this do goggling find some basic application level code , and practice it.

Step-4 Take step for useful.

Now you are Jim in Python , don’t feel that you are ready for industry , there is one more step , i.e need to learn combination of technologies.

Now  decide which side need to go , to become hero in industry , i.e in Web development or GUI development or Automation or some other.

Step-5 Learn Django for web development.

Now in your hand have Python, open Django official website (https://www.djangoproject.com/) find some example app , and practice it.

Finally!!!!!! Ha….Become…Pythonist…Congrats ..All The Best For Ur Future….

Want to Start New Django Project Fallow These Steps (Django Workflow).

Django Workflow

  1. Create a new project and a new app called mainapp
  2. Edit settings.py. Fix the following:
    1. Database settings
    2. Installed apps
    3. Media and media admin paths
    4. Most recently, to add CSS and JS paths
  3. Edit urls.py in the project folder and
    1. Uncomment the admin paths
    2. Include mainapp.urls.py
  4. Create a new urls.py in the mainapp folder
    1. Create a path that display a view called home
  5. Create a new templates folder
  6. Create a base.html file in the templates folder
    1. Create a place-holder variable within base.html
  7. Edit the views.py file. Create a new view called home and return ‘hello world’ using the base.html template and a place-holder variable
  8. Create a media folder the project folder (1 level up from the mainapp folder)
  9. Edit the models.py file
  10. Run syncdb
  11. At this point, you should be able to access both your admin page and your homepage that outputs ‘hello world’ by running the development server

Difference between Python 2 and Python 3:

If have any question in front of you like which one I need to select either Python 3 or Python 2!

See it completely depends on what you required and what you have to do with your darling(Python).

Here going to discuss, merits of Python 2, and the merits of Python 3, comparison between both, suggests which one to choose

Merits and Advantages of Python 2 or Why you want to use only Python 2:

  • Firstly, if you’re deploying to an environment you don’t control, that may impose a specific version, rather than allowing you a free selection from the available versions.
  • Secondly, if you want to use a specific third-party package or utility that doesn’t yet have a released version that is compatible with Python 3, and porting that package is a non-trivial task, you may choose to use Python 2 in order to retain access to that package.
  • Python 2 has been ruling the world of computers for almost 20 years, so there are a lot of projects and modules are implemented which may solve your problem without writing from scratch.
  • Python 2 Had good materials and books, sites throughout the world.
  • A lot of code in the world so you may come out easily from your issue

Merits and Advantages of Python 3:

  • Strings are Unicode by default
  • Non-local variable declarations
  • Function annotations
  • The syntax for keyword-only arguments
  • Including using new-style classes
  • Not using ancient deprecated arcane incantations of print, using lazy iterators were available, etc

Module Supports:

  • Some of the modules are not yet compatible with Python 3 (Twisted (for networking and other applications),gevent (a network library like Twisted, but using micro-threads rather than an explicitly asynchronous style)).
  • Many other major packages have been ported to Python 3 (numpy (for number crunching) , Django, Flask, CherryPy, and Pyramid (for Web sites) ).

Interested in Python 3 but stopping you afraid of Python 3 support in the market, and module supports.

For this, you have a tool that will convert your Python 2 module code into Python 3 code (2to3 tool that allows 3.x code to be generated from 2.x). Have a detailed look at Python official language site.

Suggestion:

If you really a person who is always interested in doing experiments with code, sure you are welcome by Python 3.

No, I am not like that:

Then Better to go Python 2 because the world exists a lot of code it