58
Alexander Lyabah: [email protected] ~$ git clone https://github.com/oduvan/happy-evil.git Cloning into happy-evil... [ … ] Resolving deltas: 100% (38/38), done. ~$ git checkout v0.0 git checkout v0.0 Note: checking out 'v0.0'. [ … ] HEAD is now at 8bb7a86... initial django

Intro django

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Intro django

Alexander Lyabah: [email protected]

~$ git clone https://github.com/oduvan/happy-evil.gitCloning into happy-evil...[ … ]Resolving deltas: 100% (38/38), done.

~$ git checkout v0.0git checkout v0.0Note: checking out 'v0.0'. [ … ]HEAD is now at 8bb7a86... initial django

Page 2: Intro django

Что есть в начале?

~$ python --versionPython 2.7.3

~$ lsb_release -aDistributor ID: UbuntuDescription: Ubuntu 12.10Release: 12.10Codename: quantal

Page 3: Intro django

Installation VirtualEnv~$ sudo aptitude install python-virtualenv[sudo] password for oduvan: The following NEW packages will be installed: [ … ] python-pip{a} python-setuptools{a} python-virtualenv 0 packages upgraded, 13 newly installed, 0 to remove and 0 not upgraded.[ … ]Setting up build-essential (11.5ubuntu3) ...

~$ cd envs/~/envs$ virtualenv heNew python executable in he/bin/pythonInstalling distribute................done.Installing pip...............done.

~/envs$ source he/bin/activate(he)~/envs$

Page 4: Intro django

Installation Django

(he)~/envs$ pip install djangoDownloading/unpacking django Downloading Django-1.4.2.tar.gz (7.7Mb): 7.7Mb downloaded Installing collected packages: django Running setup.py install for django changing mode of build/scripts-2.7/django-admin.py from 664 to 775 changing mode of /home/oduvan/envs/he/bin/django-admin.py to 775Successfully installed djangoCleaning up...

Page 5: Intro django

StartProject – создание проекта

(he)~/envs$ mkdir ~/www(he)~/envs$ cd ~/www/(he)~/www$ django-admin.py startproject he(he)~/www$ lshe(he)~/www$ cd he/(he)~/www/he$ lshe manage.py(he)~/www/he$ ls he/__init__.py settings.py urls.py wsgi.py

Page 6: Intro django

Runserver – первый запуск(he)~/www/he$ python manage.py runserver 0:8001Validating models...

0 errors foundDjango version 1.4.2, using settings 'he.settings'Development server is running at http://0:8001/Quit the server with CONTROL-C.

V0.0

Page 7: Intro django

Первый темплейт

● get_folder● Первая вьюха● Первый урл● Первый темплей

Page 8: Intro django

Settings

Page 9: Intro django

Views and URLs

Page 10: Intro django

Index.html(he)~/www/he$ mkdir templates

Page 11: Intro django

Demo

V0.1

Page 12: Intro django

Базовый шаблон

● base.html● extends● static

Page 13: Intro django

Static(he)~/www/he$ mkdir static

Page 14: Intro django

base.html

Page 15: Intro django

index.html & Demo

V0.2

Page 16: Intro django

Подключаем Базу Данных

● Настройки● Установка postgresql и компонентов для

работы с python● requirements.txt

Page 17: Intro django

Валера, настало твое время

(he)~/www/he$ python manage.py runserver 0:8001Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x235be90>>Traceback (most recent call last): File "/home/oduvan/envs/he/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run self.validate(display_num_errors=True)[ … ] import psycopg2.extensionsImportError: No module named psycopg2.extensions

Page 18: Intro django

Ад ( … миним … )(he)~/www/he$ sudo aptitude install postgresql libpq-dev python-dev[ … ]Configuring postgresql.conf to use port 5432...update-alternatives: using /usr/share/postgresql/9.1/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode * Starting PostgreSQL 9.1 database server [ OK ] Setting up postgresql (9.1+136) ...Processing triggers for libc-bin ...ldconfig deferred processing now taking placeProcessing triggers for sgml-base ...Updating the super catalog... (he)~/www/he$ pip install psycopg2Downloading/unpacking psycopg2[ … ]/temp.linux-x86_64-2.7/psycopg/microprotocols_proto.o build/temp.linux-x86_64-2.7/psycopg/typecast.o -lpq -o no previously-included directories found matching 'doc/src/_build'Successfully installed psycopg2Cleaning up...

Page 19: Intro django

Молодец, Валера! Отличная работа!

(he)~/www/he$ python manage.py runserver 0:8001Validating models...

0 errors foundDjango version 1.4.2, using settings 'he.settings'Development server is running at http://0:8001/Quit the server with CONTROL-C.[05/Dec/2012 05:27:15] "GET / HTTP/1.1" 200 1183[05/Dec/2012 05:27:16] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 101265[05/Dec/2012 05:27:16] "GET /static/js/jquery-1.8.3.min.js HTTP/1.1" 200 93637[05/Dec/2012 05:27:16] "GET /static/js/bootstrap.min.js HTTP/1.1" 200 25743

Page 20: Intro django

pip freeze(he)~/www/he$ pip freezeDjango==1.4.2argparse==1.2.1distribute==0.6.24psycopg2==2.4.5wsgiref==0.1.2

(he)~/www/he$ pip freeze > requirements.txt(he)~/www/he$ pip install -r requirements.txt Requirement already satisfied (use --upgrade to upgrade): Django==1.4.2 in /home/oduvan/envs/he/lib/python2.7/site-packages (from -r requirements.txt (line 1))[ … ]Requirement already satisfied (use --upgrade to upgrade): psycopg2==2.4.5 in /home/oduvan/envs/he/lib/python2.7/site-packages (from -r requirements.txt (line 4))Requirement already satisfied (use --upgrade to upgrade): wsgiref==0.1.2 in /usr/lib/python2.7 (from -r requirements.txt (line 5))Cleaning up...

Page 21: Intro django

PostgeSQL

(he)~/www/he$ sudo -upostgres createuser oduvanShall the new role be a superuser? (y/n) y(he)~/www/he$ sudo -upostgres createdb he

Page 22: Intro django

Апа

● Создание первой апы● Создание новой модели● Настройка админки● PIL● syncdb

Page 23: Intro django

Создаем и настраиваем(he)~/www/he$ mkdir apps(he)~/www/he$ cd apps/(he)~/www/he/apps$ django-admin.py startapp board(he)~/www/he/apps$ ls board/__init__.py models.py tests.py views.py

Page 24: Intro django

Модель

Page 25: Intro django

Админка - настройка

Page 26: Intro django

Валера? PIL(he)~/www/he$ python manage.py runserver 0:8001Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x2668f10>>Traceback (most recent call last): [ … ] File "/home/oduvan/envs/he/local/lib/python2.7/site-packages/django/core/management/base.py", line 270, in validatedjango.core.management.base.CommandError: One or more models did not validate:board.post: "photo": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .

(he)~/www/he$ pip install pil(he)~/www/he$ pip freeze > requirements.txt

Page 27: Intro django

Syncdb(he)oduvan@ubuntuServer12:~/www/he$ python manage.py syncdbCreating tables ...Creating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_user_permissionsCreating table auth_user_groupsCreating table auth_userCreating table django_content_typeCreating table django_sessionCreating table django_siteCreating table board_postCreating table django_admin_log

You just installed Django's auth system, which means you don't have any superusers defined.Would you like to create one now? (yes/no): yesUsername (leave blank to use 'oduvan'): E-mail address: [email protected]: Password (again): Superuser created successfully.Installing indexes ...Installed 0 object(s) from 0 fixture(s)

Page 28: Intro django

Админка добавление

V0.3

Page 29: Intro django

Demo

V0.3

Page 30: Intro django

Demo

V0.3

Page 31: Intro django

Demo

V0.3

Page 32: Intro django

Demo

V0.3

Page 33: Intro django

ListView - Выводим посты

Page 34: Intro django

Demo

V0.4

Page 35: Intro django

Авторизация

● Контексты● Логин● Логаут● Ексепшен :)● Регистрация● И.... Куча мелких плюшек

Page 36: Intro django

Настройки и контекст

Page 37: Intro django

URL & Template

Page 38: Intro django

Demo

V0.5.1

Page 39: Intro django

Demo

V0.5.1

Page 40: Intro django

Demo

V0.5.1

Page 41: Intro django

Demo

V0.5.1

Page 42: Intro django

Fix it

Page 43: Intro django

Demo

V0.5.2

Page 44: Intro django

Demo

V0.5.2

Page 45: Intro django

Registration

Page 46: Intro django

Demo

V0.5.3

Page 47: Intro django

Добавления поста

● Url include● Forms● MEDIA

Page 48: Intro django

Forms

Page 49: Intro django

Views

Page 50: Intro django

URLs

Page 51: Intro django

Templates of board

Page 52: Intro django

Templates of HE

Page 53: Intro django

Media(he)~/www/he$ mkdir media

Page 54: Intro django

Demo

V0.6

Page 55: Intro django

Demo

V0.6

Page 56: Intro django

Demo

V0.6

Page 57: Intro django

Demo

V0.6

Page 58: Intro django

Спасибо, Саша!http://www.slideshare.net/AlexanderLyabah/intro-django