Last active
December 10, 2016 16:11
-
-
Save kvbik/ccace5e74051f726713d7c278033490d to your computer and use it in GitHub Desktop.
pyworking 2016/12/10 django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Nastavit http://bit.ly/pyworking-prg-01 | |
======================================= | |
* Pythonanywhere.com - ucet | |
* Github.com - ucet | |
* Vytvorit na https://github.com repository "prvniblog" | |
- Initialize with README | |
- Add .gitignore Python | |
- Create repository | |
! git clone https://github.com/MUJUCET/prvniblog | |
! cd prvniblog | |
! python3 -m venv myvenv | |
! source myvenv/bin/activate | |
! pip install django | |
! pip freeze | |
django==1.10.4 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Requirements | |
============ | |
* Github account | |
* Zakladni zkusenosti s gitem (add, push) | |
* Zkusenosti s cli (mkdir / ls / edit file) | |
* Repository na Githubu prvniblog s README souborem | |
* Pythonanywhere account | |
Uvod | |
==== | |
* Co se naucis | |
- cli piskvorky fajn, ale web je lepsi | |
- zaklady webu (internetu?) | |
- popularni FW v pythonu pro weby (django) | |
Instalace | |
========= | |
* Viz requirements | |
! Vsichni by 'pip freeze | django' | |
! django-admin help | |
Jak funguje internet | |
==================== | |
* Web stranka -> soubor v HTML (ctrl+u) | |
* Ulozena na serveru -> serviruji data | |
* Internet -> servery "ruzne" propojeny kabely | |
* Request -> dopis s (IP) adresou, ktery se posila pres posty/routery na cil/server | |
* DNS -> Telefonni seznam, znate jmeno, dostanete cislo a adresu | |
* HTTP -> Format dopisu (adresa, znamka, poradi jmeno, ulice, psc, zpatecni adresa...) | |
Co je Django | |
============ | |
* Knihovna/Framework | |
* Resi spolecne problemy vsech webu: | |
HTTP, registrace, uzivatele, urlresolving url->view/fce, generovani html, ... | |
* Frameworku je hodne (python i nepython) | |
Muj prvni projekt | |
================= | |
! git clone https://github.com/<your-github-username>/prvniblog.git | |
! cd prvniblog | |
# install django | |
! python3 -m venv myvenv | |
! source myvenv/bin/activate | |
! pip install --upgrade pip | |
! pip install django~=1.9.0 | |
# initialize project | |
! django-admin startproject mysite . | |
! ls | |
! ls mysite | |
* Co se stalo? | |
* manage.py | |
* urls.py | |
* wsgi.py | |
* settings.py | |
-- settings.py -- | |
! TIME_ZONE = 'Europe/Prague' | |
! DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | |
} | |
} | |
! STATIC_ROOT = os.path.join(BASE_DIR, 'static') | |
! ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com'] | |
* Nalejem si databazi | |
! python manage.py migrate | |
* Pustime server | |
! python manage.py runserver | |
Modely | |
====== | |
* Pamatujete si objekty? | |
* Co bude mit objekt blog post? | |
* Vytvorme si appku blog | |
! python manage.py startapp blog | |
! ls | |
! ls blog | |
-- settings.py -- | |
! INSTALLED_APPS = ( | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
'blog', | |
) | |
* Vytvorime model pro blog | |
-- blog/models.py -- | |
from django.db import models | |
from django.utils import timezone | |
class Post(models.Model): | |
author = models.ForeignKey('auth.User') | |
title = models.CharField(max_length=200) | |
text = models.TextField() | |
created_date = models.DateTimeField(default=timezone.now) | |
published_date = models.DateTimeField(blank=True, null=True) | |
def publish(self): | |
self.published_date = timezone.now() | |
self.save() | |
def __str__(self): | |
return self.title | |
* Reknem djangu o novym modelu | |
! python manage.py makemigrations blog | |
* A at ho prida do nasi db | |
! python manage.py migrate blog | |
Admin | |
===== | |
* Web rozhrani pro spravce | |
* Chceme pridat nas Post | |
-- blog/admin.py -- | |
from django.contrib import admin | |
from .models import Post | |
admin.site.register(Post) | |
* Udelame si ucet | |
! python manage.py createsuperuser | |
* Spustime server a prihlasime se | |
! python manage.py runserver | |
* otevri http://127.0.0.1:8000/admin | |
* pohraj si s nim (hlavne s posty), pridej zaznam atd | |
\o/ | |
Deploy (DELAT urcite, nejpozdeni 20min pred koncem!) | |
==================================================== | |
* Poslem to na github | |
! git status | |
* pridame par zaznamu do .gitignore | |
-- .gitignore -- | |
/db.sqlite3 | |
/myvenv/ | |
! git add .gitignore mysite blog manage.py | |
! git commit -m 'blog s adminem' | |
! git push | |
* Otevrete si svuj github.com/VASLOGIN/prvniblog a overte, ze tam jsou zdrojaky | |
* Prihlaste se do https://www.pythonanywhere.com/ | |
* Otevrit bash konzoli a tam | |
!PyA.com! git clone https://github.com/<your-github-username>/prvniblog.git | |
!PyA.com! tree prvniblog | |
!PyA.com! cd prvniblog | |
!PyA.com! virtualenv --python=python3.5 myvenv | |
!PyA.com! source myvenv/bin/activate | |
!PyA.com! pip install django | |
!PyA.com! python manage.py migrate | |
!PyA.com! python manage.py createsuperuser | |
* Vratit se do PyA a pridat web app (ne django, ale custom, cestu k venvu apod) | |
-- Virtualenv: -- | |
/home/<your-PythonAnywhere-username>/prvniblog/myvenv/ | |
-- WSGI configuration file: -- | |
import os | |
import sys | |
path = '/home/<your-PythonAnywhere-username>/prvniblog' # use your own PythonAnywhere username here | |
if path not in sys.path: | |
sys.path.append(path) | |
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' | |
from django.core.wsgi import get_wsgi_application | |
from django.contrib.staticfiles.handlers import StaticFilesHandler | |
application = StaticFilesHandler(get_wsgi_application()) | |
* zmacknout tlacitko nahore "Reload" | |
* JEDE NAM SAJTA! NA INTERNETU! | |
URLS | |
==== | |
* Co je URL | |
! cat mysite/urls.py | |
! Pridat | |
from django.conf.urls import url, include | |
url(r'', include('blog.urls')), | |
! -- blog/urls.py | |
from django.conf.urls import url | |
from . import views | |
urlpatterns = [ | |
url(r'^$', views.post_list, name='post_list'), | |
] | |
* pustit runserver? | |
VIEWS | |
===== | |
! -- blog/views.py -- | |
from django.shortcuts import render | |
def post_list(request): | |
return render(request, 'blog/post_list.html', {}) | |
* otevrit http://127.0.0.1:8000/ | |
TEMPLATES | |
========= | |
! mkdir blog/templates | |
! mkdir blog/templates/blog | |
! -- blog/templates/blog/post_list.html -- | |
* otevrit http://127.0.0.1:8000/ | |
(prazdno) | |
! -- blog/templates/blog/post_list.html -- | |
<html> | |
<h1>Muj blog</h1> | |
<p>Funguje to!</p> | |
</html> | |
* otevrit http://127.0.0.1:8000/ | |
<html> | |
<h1>Muj blog</h1> | |
<p>Funguje to!</p> | |
<h2>Prvni post</h2> | |
<p>To vypada super</p> | |
</html> | |
* otevrit http://127.0.0.1:8000/ | |
DEPLOY!! | |
(git status, add, commit, push) | |
PyA.com pull, WEB tab reload | |
ORM | |
==== | |
! python manage.py shell | |
[ explore ] | |
! from blog.models import Post | |
! Post.objects.all() | |
! Post.objects.first() | |
! Post.objects.create(title="Kuk z konzole", text="Ulozi se to?") | |
! Post.objects.all() | |
! Post.objects.get(title="Kuk z konzole") | |
! Post.objects.order_by('created_date') | |
! Post.objects.order_by('-created_date') | |
Dynamic templates | |
================= | |
-- blog/views.py -- | |
from blog.models import Post | |
def post_list(request): | |
posts = Post.objects.order_by('-created_date') | |
return render(request, 'blog/post_list.html', {'posts': posts}) | |
! -- blog/templates/blog/post_list.html -- | |
<html> | |
<h1>Muj blog</h1> | |
<p>Funguje to!</p> | |
<h2>Prvni post</h2> | |
<p>To vypada super</p> | |
{{ posts }} | |
</html> | |
! -- blog/templates/blog/post_list.html -- | |
<html> | |
<h1>Muj blog</h1> | |
<p>Funguje to!</p> | |
{% for post in posts %} | |
<h2>{{ post.title }}</h2> | |
<p>{{ post.text }}</p> | |
{% endfor %} | |
</html> | |
co chceme udelat: | |
blog | |
deploynout | |
... | |
slozitosti: | |
vysvetlovani regex v urls.py | |
via http://collabedit.com/ss4b5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment