zagreb workshop

37
PYLADIES PRESENTS: Build your own blog with Django! 1

Upload: lynn-root

Post on 06-May-2015

626 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Zagreb workshop

PYLADIES PRESENTS:

Build your own blog with Django!

1

Page 2: Zagreb workshop

{{ GOALS }}

Have funLearn about web development

Create something

2

Page 3: Zagreb workshop

{{ LAYOUT }}

Overview of Django FrameworkCode together

3

Page 4: Zagreb workshop

{{ DJANGO }}

Django is to Pythonas

Rails is to Ruby

4

Page 5: Zagreb workshop

{{ LET’S GET STARTED }}

Quick note:do NOT copy text

on these slides into your text editor. Write it out yourself.

5

Page 6: Zagreb workshop

{{ LET’S GET STARTED }}Activate your virtualenv$ workon MyBlog

orC:\User\Desktop\Projects\MyBlog\Tools\Scripts

\activate6

Page 7: Zagreb workshop

{{ LET’S GET STARTED }}Start a Python shell

And type ‘import django’

(MyBlog)$ python

>>> import django7

Page 8: Zagreb workshop

{{ LET’S GET STARTED }}Back in the terminal (NOT in the

python shell!, type "one line!:

(MyBlog)$ django-admin.py startproject ZagrebWorkshop

8

Page 9: Zagreb workshop

{{ RUNSERVER }}On the terminal/console:

(MyBlog)$ python manage.py runserver

http://localhost:8000

In a web browser, go to:

BAM it works.9

Page 10: Zagreb workshop

{{ SETTINGS.PY }}Open up settings.pyDATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'MyBlog.db', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', }}

10

Page 11: Zagreb workshop

{{ SYNCDB }}Back on the terminal/console:

(MyBlog)$ python manage.py syncdb

and follow prompts.BAM your DB is set up.

11

Page 12: Zagreb workshop

{{ STARTAPP }}Back on the terminal/console:

(MyBlog)$ python manage.py startapp MyBlog

BAM more files!

(MyBlog)$ cd MyBlog

(MyBlog)$ ls OR (MyBlog)C:\User\Desktop\> dir

12

Page 13: Zagreb workshop

{{ MODELS.PY }}Open up models.py

from django.db import models

class Post(models.Model): title = models.CharField(max_length=60) body = models.TextField() created = models.DateTimeField(auto_now_add=True)

def __unicode__(self): return self.title

13

Page 14: Zagreb workshop

{{ SETTINGS.PY }}Return to settings.py file.

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'MyBlog',)

14

Page 15: Zagreb workshop

{{ SYNCDB }}Back on the terminal/console:

(MyBlog)$ python manage.py syncdb

again. Then do:(MyBlog)$ python manage.py shell

Let’s play around a little.15

Page 16: Zagreb workshop

{{ SETTINGS.PY }}Return to settings.py file.

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'MyBlog',)

16

Page 17: Zagreb workshop

{{ ADMIN }}Something is missing.

Create MyBlog/admin.pyfrom django.contrib import admin

from MyBlog.models import Post

class PostAdmin(admin.ModelAdmin): search_fields = ['title']

admin.site.register(Post, PostAdmin)

17

Page 18: Zagreb workshop

{{ ADMIN }}Back on the terminal/console:(MyBlog)$ python manage.py runserver

In a web browser, go to:http://localhost:8000/admin

woah.18

Page 19: Zagreb workshop

{{ URLS }}Open urls.py

And edit for the following:from django.conf.urls import patterns, include, urlfrom django.contrib import admin

admin.autodiscover()

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

19

Page 20: Zagreb workshop

{{ ADMIN }}Back on the terminal/console:(MyBlog)$ python manage.py runserver

In a web browser, go to:http://localhost:8000/admin

woah.20

Page 21: Zagreb workshop

{{ URLS }}Now go to:

http://localhost:8000

Missing something...

21

Page 22: Zagreb workshop

{{ URLS }}Reopen urls.py

And edit for the following:from django.conf.urls import patterns, include, urlfrom django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('', url(r'^$', ‘MyBlog.views.home’, name='home'), url(r'^(\d+)/$', ‘MyBlog.views.post’, name='post'), url(r'^admin/', include(admin.site.urls)),)

22

Page 23: Zagreb workshop

{{ ADMIN }}Back on the terminal/console:(MyBlog)$ python manage.py runserver

In a web browser, go to:http://localhost:8000/

Hrmph. No Views.23

Page 24: Zagreb workshop

{{ VIEWS }}Open views.py...

And let’s write two views together.

24

Page 25: Zagreb workshop

{{ TEMPLATES }}Download http://l.ynn.me/SVaCxp &

unzip/open the file.

Move the “templates” folder to under the main

“ZagrebWorkshop” directory.25

Page 26: Zagreb workshop

{{ TEMPLATES }}

Move the “static” folder to under the second

“ZagrebWorkshop” directory.

26

Page 27: Zagreb workshop

{{ TEMPLATES }}

Optional: open “ZagrebWorkshop/templates/blog/

base.html” and edit for Author, Email, and/or Site Title

27

Page 28: Zagreb workshop

{{ TEMPLATES }}Optional: If you created a Disqus

account, copy and paste your JavaScript code in:

“ZagrebWorkshop/templates/blog/post.html”

28

Page 29: Zagreb workshop

!"" ZagrebWorkshop/ #"" MyBlog/ $   #"" __init__.py $   #"" admin.py $   #"" models.py $   #"" tests.py $   #"" views.py #"" ZagrebWorkshop/ $   #"" __init__.py $   #"" settings.py | !"" static/ $   #"" urls.py $   #"" wsgi.py #"" manage.py #"" MyBlog.db !"" templates/ !"" blog/

{{ DIRECTORIES }}

29

Page 30: Zagreb workshop

{{ SETTINGS.PY }}Reopen settings.py

and add these few bits.Refer to notes for specific line

numbers.https://gist.github.com/4144268

30

Page 31: Zagreb workshop

{{ COLLECTSTATIC }}

(MyBlog)$ python manage.py collectstatic

31

Page 32: Zagreb workshop

{{ TA-DA }}

One last time(MyBlog)$ python manage.py runserver

You should see your blog!

32

Page 33: Zagreb workshop

{{ SETUP HEROKU }}

Make a venv snapshot

(MyBlog)$ pip freeze > requirements.txt

33

Page 34: Zagreb workshop

{{ SETUP HEROKU }}

Make a new file called “Procfile”and save it in your main

“ZagrebWorkshop” directory web: python manage.py runserver 0.0.0.0:$PORT

--noreload

34

Page 35: Zagreb workshop

{{ SETUP HEROKU }}

$ git config --global user.name “Your Name”$ git config --global user.email “Your Email”

$ git init$ git add .

$ git commit -m “My Django Application”

35

Page 36: Zagreb workshop

{{ SETUP HEROKU }}(MyBlog) $ heroku login(MyBlog) $ heroku create

Follow the steps for ‘heroku create’. Take note of the URL that Heroku

gives you on your terminal(MyBlog) $ git push heroku master

36

Page 37: Zagreb workshop

{{ DEPLOY }}

It may take a couple of minutes. Then, navigate to that URL

from previous step

(MyBlog) $ git push heroku master

37