CSC/ECE 517 Fall 2014/ch1a 8 os: Difference between revisions
Line 85: | Line 85: | ||
===Cache framework=== | ===Cache framework=== | ||
The power of Django lies in the way it performs in case of dynamic websites. In case of dynamic websites the server makes lot of complex calculations and database queries and performs some business logic for very request. This reduces the turn around time as the request not only affects the web server but also the application server and database server at times. For websites which handle heavy to very-heavy traffic loads this processing-overhead becomes a matter of concern. Caching is thus used to prevent repeating the same calculations again-and-again for every request. Caching preserves the result of a calculation the first time it is performed and returns the cached result for the subsequent requests unless the variables change and a new calculation is required. Following pseudocode explains the concept of caching for a dynamicaly generated web page | |||
<pre> | |||
given a URL, try finding that page in the cache | |||
if the page is in the cache: | |||
return the cached page | |||
else: | |||
generate the page | |||
save the generated page in the cache (for next time) | |||
return the generated page | |||
</pre> | |||
== Comparison with other Web Frameworks == | == Comparison with other Web Frameworks == | ||
== References == | == References == |
Revision as of 17:23, 14 September 2014
Django
Background
Architecture
Features
Object- Relational Mapper
Django has its own object relational mapper which eases database access. It facilitates writing of model classes directly in python. Model classes can be defined by sub-classing django.db.models.Model. Each attribute of this class corresponds to a database field. This makes it possible to use an automatically generated database access API.
from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
The database columns first_name and last_name are specified as class attributes and an id field is automatically added to the table.
Automatic admin interface
Generating admin sites for the staff to add,change or delete content is made easy by Django by automating the creation of admin interfaces for models. Creating an admin user is as easy as follows:
1. Create a user
$ python manage.py createsuperuser
2. Enter the username
Username: admin
3. Enter email address
Email address: admin@example.com
4. Final step is to setup a password as follows and the admin site gets created and is activated by default.
Password: ********** Password (again): ********* Superuser created successfully
Elegant URL Design
URLs can be designed using a module called URLconf. This module has simple python code in it which maps URL patterns to python functions(views) using regular expression based pattern matching. This mapping can reference other mappings. Since it is pure python code this mapping can also be gennerated dynamically.
How Django processes a request [1]
When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute:
- Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has an attribute called urlconf (set by middleware request processing), its value will be used in place of the ROOT_URLCONF setting.
- Django loads that Python module and looks for the variable urlpatterns. This should be a Python list, in the format returned by the function django.conf.urls.patterns().
- Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
- Once one of the regexes matches, Django imports and calls the given view, which is a simple Python function (or a class based view). The view gets passed the following arguments:
- An instance of HttpRequest.
- If the matched regular expression returned no named groups, then the matches from the regular expression are provided as positional arguments.
- The keyword arguments are made up of any named groups matched by the regular expression, overridden by any arguments specified in the optional kwargs argument to django.conf.urls.url().
- If no regex matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view.
Example:
Here’s a sample URLconf:
from django.conf.urls import patterns, url from . import views urlpatterns = patterns('', url(r'^articles/2003/$', views.special_case_2003), url(r'^articles/(\d{4})/$', views.year_archive), url(r'^articles/(\d{4})/(\d{2})/$', views.month_archive), url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', views.article_detail), )
The Django template language
Django template system is aimed at achieveing a striking a balnce between power and ease of use. The system provides tags which function similar to normal programming constructs. There are tags like the if tag for conditions and the for tag for looping. A template is a text file which can be converted to HTML, CSV or any other text-based format. It has variables and tags. The variables get converted to values when the file is evaluated and the tags control the logic of the template. Django prefers a text-based template over a XML one so as to be able to use it for emails, Javascript and CSV etc. Also editing text-based templates is easier than modifying XML files. Django templates also support inheritance where in you can define a common skeleton template code in parent file and have blocks in it which can be overridden by children files. A comprehensive list of tags supported by the template language can be found here.
Cache framework
The power of Django lies in the way it performs in case of dynamic websites. In case of dynamic websites the server makes lot of complex calculations and database queries and performs some business logic for very request. This reduces the turn around time as the request not only affects the web server but also the application server and database server at times. For websites which handle heavy to very-heavy traffic loads this processing-overhead becomes a matter of concern. Caching is thus used to prevent repeating the same calculations again-and-again for every request. Caching preserves the result of a calculation the first time it is performed and returns the cached result for the subsequent requests unless the variables change and a new calculation is required. Following pseudocode explains the concept of caching for a dynamicaly generated web page
given a URL, try finding that page in the cache if the page is in the cache: return the cached page else: generate the page save the generated page in the cache (for next time) return the generated page