Skip to content

Web Application Development with Django – Part 1

Posted in Web Development

What is Django?

Django is an open-source web framework written in Python. The primary goal of Django is to ease the creation of complex, database-driven websites. Django emphasises reusability of components, rapid development and the principle “don’t repeat yourself”. Django provides also an administrative CRUD (create,read,update,delete) interface.

The core Django framework follows MVC architecture.It consists of an object-relational mapper (ORM) that mediates between data models (defined as Python classes) and a database (“Model”), a system for processing HTTP requests with a web tempting system (“View”), and regular-expression-based URL dispatcher (“Controller”).

Django core framework contains also:

  • standalone web server for development and testing
  • form serialisation and validation system the can translate forms and values suitable for storage in the database
  • template system that utilises the concept of inheritance
  • caching framework
  • support for middleware classes
  • internationalisation system
  • serialisation system that can produce and read XML and JSON representations of Django model instances

The main Django distribution package includes also the following apps:

  • authentication system
  • dynamic administrative interface
  • tools for generating RSS and Atom syndication feeds
  • tools for generating Google sitemaps
  • built-in security features for typical web attacks, most of them are turned on by default
  • framework for creating GIS applications

Django extensibility

Django’s configuration system allows third party code to be plugged into a project, if it follows the reusable app conventions. More than 2500 packages are available to extend the framework’s original behaviour, providing solutions to issues like registration, search, API provision and consumption etc.

Django server requirements

Django can be run in conjunction with Apache, NGINX using WSGI, Gunicorn or Cherokee using flup (a Python module). Officially Django supports four database backends: PostgresSQL, MySQL, SQLite and or Oracle. Unofficially Django supports also other database backends like Microsoft SQL Server, IBM DB2, SQL Anywhere and Firebird. Also NoSQL databases (e.g. MongoDB) are supported in a way.


Myfirstdjango app

I started my Django studies at lynda.com with ‘Up and Running with Python and Django’ course few days ago. I have some clue how to create web apps with NodeJS and AngularJS. Now I want to learn another efficient way to create web apps. Here are my notes.

  1. Install Python (no need to do this if you are on Mac, like me). Anyways I installed the latest python ver 3
  2. Install Django: pip install django (at command prompt or terminal)
  3. Create Django project: django-admin start project myfirstdjango
  4. List of available commands, run: python manage.py
  5. Example: run a server: python manage.py runserver (by default server is running at localhost:8000)

Django Project Files

  • myfirsrdjango/__init__.py
    • Tells Django where a project folder is
    • Do NOT edit
  • manage.py
    • Runs commands
    • Do NOT edit
  • myfirstdjango/wsgi.py
    • Provides a hook for web servers
    • Do NOT edit
  •  myfirstdjango/settings.py
    • Configures Django
  • myfirstdjango/urls.py
    • Routes requests based on URL

Django Apps

  • folder with Python files
  • app is a component
  • app fits a specific purpose (e.g. blog, forum, wiki)

Pieces of an App

  • models.py: data layer (structure of database tables)
  • admin.py: administrative interface (database interface for the admin)
  • views.py: control layer (HTTP requests)
  • tests.py: tests the app
  • migrations: database migration files

Creating a new django app

  • run: python manage.py startapp myfirstdjango
  • edit: settings.py: add new app (myfirstdjango) at the end of INSTALLED_APPS tuple

Django Settings

  • INSTALLED_APPS
    • New Django apps
  • TEMPLATES
    • New templates
  • STATICFILES_DIRS
    • New static assets (e.g. CSS and Javascript)
  • DEBUG
    • By default debug is on, when deploying to production change it to false
  • DATABASES
    • Default database is SQLite
    • When deploying to production, database is better to be something else (like PostgresSQL, MySQL)

Database Migrations

  • Adding a Model
  • Adding a Field
  • Removing a Field
  • Changing a Field

Migrations commands

  • python manage.py makemigrations
    • generates migration files
    • uses current models fields and database tables
  • python manage.py migrate
    • runs all migrations that have not run yet

Django admin

  1. Create first superuser: python manage.py createsuperuser
  2. Start local server: python manage.py runserver
  3. Open a browser and enter localhost:8000 to the URL field and hit enter
  4. Login as a superuser
  5. Now you can add items and users to the database

Querying data with Django ORM

  • Run: python manage.py shell
  • Run e.g. the following to check that code works correctly:
    • items = Items.objects.all()
    • item = items[0]
    • item.title
    • item.amount
    • item.id
    • Item.objects.filter(amount=0)[0].title

URLs, Views and Templates

  • URLs: urls.py
  • Views: views.py
  • Templates: templates folder

CSS and Javascript

  • settings.py: Static files setting

Next steps

I will create a REST API, change the database type to PostgresSQL and move code to Heroku and Azure.


Links

 

Recent Posts

Categories