web development with python - django
TRANSCRIPT
-
8/2/2019 Web Development With Python - Django
1/31
Web developement withPython: Django framework +
DeploymentDavid Jeli (FOI OSS)"
RV FOI
-
8/2/2019 Web Development With Python - Django
2/31
virtualenv
virtualenvwrapper
pip
Django
Django deployment
-
8/2/2019 Web Development With Python - Django
3/31
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
-
8/2/2019 Web Development With Python - Django
4/31
-
8/2/2019 Web Development With Python - Django
5/31
virtualenv
$sourceENV/bin/activate
(ENV)$python-c"importsys;print
sys.path
['','/Users/djelic/Development/ENV/lib/python2.7/site-packages/setuptools-0.6c11-
py2.7.egg,]$deactivate
$
-
8/2/2019 Web Development With Python - Django
6/31
virtualenv
use --no-site-packages (default)
use --system-site-packages to inherit globalpackages
relocatable environments (experimental) relocatable
your code doesnt need to be located inside virtualenvironment path!
-
8/2/2019 Web Development With Python - Django
7/31
virtualenv
Demo
-
8/2/2019 Web Development With Python - Django
8/31
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 projectdependencies
virtualenvwrapper depends on virtualenv
-
8/2/2019 Web Development With Python - Django
9/31
virtualenvwrapper
$pipinstallvirtualenvwrapper
exportWORKON_HOME=$HOME/.virtualenvs
source/usr/local/bin/virtualenvwrapper.sh
-
8/2/2019 Web Development With Python - Django
10/31
virtualenvwrapper
switching between environments
user-configurable hooks!
tab completion
import libraries from global Python path
-
8/2/2019 Web Development With Python - Django
11/31
virtualenvwrapper
transparent workvirtualenv
mkvirtualenv rmvirtualenv workon add2virtualenv
cdsitepackages cdvirtualenv deactivate
user-configurable hooks
postmkvirtualenv
prermvirtualenv postrmvirtualenv
postactivate predeactivate
postdeactivate
-
8/2/2019 Web Development With Python - Django
12/31
virtualenvwrapper
Demo
-
8/2/2019 Web Development With Python - Django
13/31
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
-
8/2/2019 Web Development With Python - Django
14/31
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
-
8/2/2019 Web Development With Python - Django
15/31
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 theirsetup.py files
-
8/2/2019 Web Development With Python - Django
16/31
pip
install packages
$pipinstallsimplejson[...progressreport...]Successfullyinstalledsimplejson
uninstall packages
$pipuninstallsimplejsonUninstallingsimplejson:
/home/me/env/lib/python2.7/site-packages/simplejson/home/me/env/lib/python2.7/site-packages/simplejson-2.2.1-py2.7.egg-infoProceed(y/n)?ySuccessfullyuninstalledsimplejson
-
8/2/2019 Web Development With Python - Django
17/31
pip
upgrading packages
$pipinstall--upgradesimplejson[...progressreport...]Successfullyinstalledsimplejson
searching for packages (PyPI)
$pipsearchsimplejsonsimplejson-Simple,fast,extensibleJSONencoder/decoderforPython
-
8/2/2019 Web Development With Python - Django
18/31
pip
bundles
requirements file
MyAppDjango==1.4.0wsgiref==0.1.2-esvn+http://myrepo/svn/MyApp#egg=MyApp
freeze$pipfreeze>requirements.txt
install$pipinstall-r/path/to/requirements.txt
-
8/2/2019 Web Development With Python - Django
19/31
Django
open source web application framework
written in Python
MVC architectural pattern
originaly developed for news-oriented sites for TheWorld Company
Named after guitarist Django Reinhardt
-
8/2/2019 Web Development With Python - Django
20/31
Django
The Web framework for perfectionists withdeadlines
emphasis on simplicity, DRY and reuse
pluggable applications majority of the features commuity contributed
-
8/2/2019 Web Development With Python - Django
21/31
Django
database driven web application
MVC
object-relational mapper (ORM)
Model: relational database
View: processing requests with web templatingsystem
Controller: regular-expression based URL dispatcher
-
8/2/2019 Web Development With Python - Django
22/31
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
-
8/2/2019 Web Development With Python - Django
23/31
django.contrib
authentication system
dynamic administrative interface
tools for generating RSS and Atom feeds
sites framework
commenting system
-
8/2/2019 Web Development With Python - Django
24/31
Model Syntax
fromdjango.dbimportmodelsclassAnketa(models.Model):
pitanje=models.CharField(max_length=200)pub_date=models.DateTimeField('datepublished')def__unicode__(self):returnself.pitanjeclassOdgovor(models.Model):pitanje=models.ForeignKey(Anketa)odgovor=models.CharField(max_length=200)glasova=models.IntegerField()def__unicode__(self):returnself.odgovor
-
8/2/2019 Web Development With Python - Django
25/31
Interactive shell
$pythonmanage.pyshell>>>fromanketa.modelsimportAnketa,Odgovor>>>Anketa.objects.all()
[]>>>a=Anketa.objects.get(pk=1)>>>a>>>a.odgovor_set.all()[]>>>o=a.odgovor_set.all()[0]>>>o.glasova1
-
8/2/2019 Web Development With Python - Django
26/31
Auto-generated admin
-
8/2/2019 Web Development With Python - Django
27/31
Admin Syntax
fromanketa.modelsimportAnketa,Odgovorfromdjango.contribimportadminclassOdgovorInline(admin.TabularInline):
model=Odgovorextra=3classAnketaAdmin(admin.ModelAdmin):fieldsets=[(None,{'fields':['pitanje']}),('Dateinformation',{'fields':['pub_date'],
'classes':['collapse']}),]inlines=[OdgovorInline]list_display=('pitanje','pub_date')admin.site.register(Anketa,AnketaAdmin)
-
8/2/2019 Web Development With Python - Django
28/31
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)),)
-
8/2/2019 Web Development With Python - Django
29/31
View Syntax
fromanketa.modelsimportAnketafromdjango.shortcutsimportrender_to_response,get_object_or_404defindex(request):anketa_list=Anketa.objects.all()
returnrender_to_response('anketa/index.html',{'anketa_list':anketa_list})
-
8/2/2019 Web Development With Python - Django
30/31
Template
{%ifanketa_list%}
{%foranketainanketa_list%}{{anketa.pitanje}}{%endfor%}{%else%}
Nemaanketa.
{%endif%} -
8/2/2019 Web Development With Python - Django
31/31