Rapid Web Application Development With Python/Django – Python – PyPi



Rapid Web Application Development With Python/Django – Python – PyPi

0 1


rad-django


On Github chamindu / rad-django

Rapid Web Application Development With Python/Django

Chamindu R. Munasinghe chamindu@calcey.com@chamindum

Agenda

  • Python
  • PyPi & pip
  • virtualenv
  • Django
  • Demo

Python

  • Open source
  • Cross platform
  • General purpose
  • Dynamic

Python

  • Object oriented
  • Procedural
  • Functional

"Batteries Included"

Python 2.7 vs Python 3

PyPi

Python package index

A repository of around 51k packages

pip

Package manager for Python

              $ pip install django
              $ pip install -r requirements.txt
            

virtualenv

Isolated working environments

One global python environment

Cannot run multiple python versions

All apps share same packages and versions

Using virtuanenv

virtualenvwrapper

A command line tool to easily manage virtualenv's in development

Django

"The Web framework for perfectionists with deadlines"

Used by

Disqus, Instagram, Pinterest, Bitbucket and many more

Modular

  • A "project" comprises of many "apps"
  • Third party apps that can be plugged into a Django project
    • Blogs
    • Calendars
    • CMS's
  • http://www.djangopackages.com

Architecture

  • MVT - Model, View, Template
  • Views are python functions
  • Built in ORM
  • Regex based URL patterns
  • Middleware

Demo

Create a simple "polls" app

Creating the project

$ django-admin.py startproject mysite

Database setup

The app is already configured to use sqlite

Create "polls" app

$ python manage.py startapp polls

Add it to the INSTALLED_APPS

Creating the Models

            class Question(models.Model):
                question_text = models.CharField(max_length=200)
                pub_date = models.DateTimeField('date published')

            class Choice(models.Model):
                question = models.ForeignKey(Question)
                choice_text = models.CharField(max_length=200)
                votes = models.IntegerField(default=0)
            

Create and apply Migrations

              $ python manage.py makemigrations polls
              $ python manage.py migrate
            

Exploring the admin app

Basic CRUD operations with zero code

            from django.contrib import admin
            from polls.models import Question
 
            admin.site.register(Question)
            

Adding "Choices" to the admin

            from django.contrib import admin
            from polls.models import Question, Choice

            class ChoiceInline(admin.StackedInline):
                model = Choice
                extra = 3

            class QuestionAdmin(admin.ModelAdmin):
                inlines = [ChoiceInline]

            admin.site.register(Question, QuestionAdmin)
            

Creating our first view

            from django.http import HttpResponse

            def index(request):
                return HttpResponse("Hello, world. You're at the polls index.")
            

URL mapping

            # polls/urls.py
            from django.conf.urls import patterns, url
            from polls import views

            urlpatterns = patterns('', 
                url(r'^$', views.index, name='index'),
            )
            

            # urls.py
            from django.conf.urls import patterns, include, url
            from django.contrib import admin

            urlpatterns = patterns('',
                url(r'^polls/', include('polls.urls')),
                url(r'^admin/', include(admin.site.urls)),
            )           
            

Using generic views

Generic ListView

            class IndexView(generic.ListView):
                model = Question
            

Creating a template

            <html>
              <head></head>
              <body>
                <ul>
                  {% for question in object_list %}
                    <li><a href="{% url 'detail' question.id %}">
                    {{ question.question_text }}
                    </a></li>
                  {% empty %}
                    <li>No polls</li>
                  {% endfor %}
                </ul>
              </body>       
            </html>
            

What we did not cover

  • More generic views CreateView, UpdateView, DeleteView
  • Forms
  • Authentication & Authorization
  • Middleware
  • Templates in depth
  • Caching
  • Management commands

Hosting Django

nginx and uWSGI recommended

Some interesting tools

  • django-rest-framework - Easy REST API's
  • django-crispy-forms - Beautifull looking forms
  • django-oauth-toolkit - OAuth support
  • django-debug-toolbar - Debugging tool
  • django-reversion - Version control for Models
  • django-guardian - Record level permissions
  • django-cms - Content management system
  • django-extentions - Lot of usefull extensions
  • Celery - Async, distributed job queues

Questions?

Thank You