You are on page 1of 31

Web developement with Python: Django framework + Deployment

David Jeli (FOI OSS)" RV FOI

virtualenv virtualenvwrapper pip Django Django deployment

virtualenv
created by Ian Bicking tool to create isolated Python environments solution for:
dependencies and version problem inaccessible global site-packages install application and leave it be?

environments isolated from each other


has its own installation directories doesnt share libraries between envs doesnt access globally installed libraries

virtualenv
$ virtualenv ENV $ tree -L 2 ENV/ ENV/ bin activate activate.csh activate.fish activate_this.py easy_install easy_install-2.7 pip pip-2.7 python python2.7 -> python include python2.7 -> /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 lib python2.7 5 directories, 10 files

virtualenv
$ source ENV/bin/activate (ENV) $ python -c "import sys; print sys.path ['', '/Users/djelic/Development/ENV/lib/ python2.7/site-packages/setuptools-0.6c11- py2.7.egg, ] $ deactivate $

virtualenv
use --no-site-packages (default) use --system-site-packages to inherit global packages relocatable environments (experimental) relocatable your code doesnt need to be located inside virtual environment path!

virtualenv

Demo

virtualenvwrapper
set of extensions for virtualenv wrappers for creating and deleting virtualenvs managing development workflow easier to work on more than one project working on projects without conflicts in project dependencies virtualenvwrapper depends on virtualenv

virtualenvwrapper

$ pip install virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh

virtualenvwrapper

switching between environments user-configurable hooks! tab completion import libraries from global Python path

virtualenvwrapper
transparent work virtualenv
mkvirtualenv rmvirtualenv workon add2virtualenv cdsitepackages cdvirtualenv deactivate

user-configurable hooks
postmkvirtualenv prermvirtualenv postrmvirtualenv postactivate predeactivate postdeactivate

virtualenvwrapper

Demo

pip
another tool created by Ian Bicking tool for installing and managing Python packages Python Package Index (http://pypi.python.org/pypi) 20410 packages easy_install replacement support for VCS most nutritious when used with virtualenv

pip vs. easy_install


all packages are downloaded before installation produces useful output keeps track of why some package is required easier to use programmatically native support for VCS (Git, Mercurial, Baazar) uninstalation of packages requirements file

pip vs. easy_install

pip cannot install from eggs, only from source pip doesnt understand setuptools extras pip is incompatible with some packages that extensively customize distutils or setuptools in their setup.py files

pip
install packages
$ pip install simplejson [... progress report ...] Successfully installed simplejson

uninstall packages
$ pip uninstall simplejson Uninstalling simplejson: /home/me/env/lib/python2.7/site-packages/simplejson /home/me/env/lib/python2.7/site-packages/ simplejson-2.2.1-py2.7.egg-info Proceed (y/n)? y Successfully uninstalled simplejson

pip
upgrading packages
$ pip install --upgrade simplejson [... progress report ...] Successfully installed simplejson

searching for packages (PyPI)


$ pip search simplejson simplejson - Simple, fast, extensible JSON encoder/decoder for Python

pip
bundles requirements file
MyApp Django==1.4.0 wsgiref==0.1.2 -e svn+http://myrepo/svn/MyApp#egg=MyApp

freeze
$ pip freeze > requirements.txt

install
$ pip install -r /path/to/requirements.txt

Django
open source web application framework written in Python MVC architectural pattern originaly developed for news-oriented sites for The World Company Named after guitarist Django Reinhardt

Django

The Web framework for perfectionists with deadlines emphasis on simplicity, DRY and reuse pluggable applications majority of the features commuity contributed

Django
database driven web application MVC
object-relational mapper (ORM) Model: relational database View: processing requests with web templating system Controller: regular-expression based URL dispatcher

django.core
core Django framework
standalone development web server form serialization and validation middleware classes internationalization system caching system for extending template engine interface to unit test framework internal dispatcher system

django.contrib

authentication system dynamic administrative interface tools for generating RSS and Atom feeds sites framework commenting system

Model Syntax
from django.db import models class Anketa(models.Model): pitanje = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.pitanje class Odgovor(models.Model): pitanje = models.ForeignKey(Anketa) odgovor = models.CharField(max_length=200) glasova = models.IntegerField() def __unicode__(self): return self.odgovor

Interactive shell
$ python manage.py shell >>> from anketa.models import Anketa, Odgovor >>> Anketa.objects.all() [<Anketa: Razmjena vjetina rules>] >>> a = Anketa.objects.get(pk=1) >>> a <Anketa: Razmjena vjetina rules> >>> a.odgovor_set.all() [<Odgovor: test>] >>> o = a.odgovor_set.all()[0] >>> o.glasova 1

Auto-generated admin

Admin Syntax
from anketa.models import Anketa, Odgovor from django.contrib import admin class OdgovorInline(admin.TabularInline): model = Odgovor extra = 3 class AnketaAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['pitanje']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [OdgovorInline] list_display = ('pitanje', 'pub_date') admin.site.register(Anketa, AnketaAdmin)

URL pattern

urlpatterns = patterns('', url(r'^$', 'rvdemo.views.home', name='home'), url(r'^rvdemo/', include('rvdemo.anketa.urls')), # OR # url(r'^anketa/$', 'anketa.views.index'), url(r'^admin/', include(admin.site.urls)), )

View Syntax

from anketa.models import Anketa from django.shortcuts import render_to_response, get_object_or_404 def index(request): anketa_list = Anketa.objects.all() return render_to_response('anketa/index.html', {'anketa_list': anketa_list})

Template
{% if anketa_list %} <ul> {% for anketa in anketa_list %} <li><a href="/anketa/ {{ anketa.id }}/">{{ anketa.pitanje }}</a></li> {% endfor %} </ul> {% else %} <p>Nema anketa.</p> {% endif %}

Pitanja?

You might also like