$30 off During Our Annual Pro Sale. View Details »

Introduction to Django

Introduction to Django

Small talk I did for our local Python Group!

Mahdi Yusuf

May 21, 2015
Tweet

More Decks by Mahdi Yusuf

Other Decks in Programming

Transcript

  1. Introduction to Django
    @myusuf3
    because who reads documentation

    View Slide

  2. What is Django?
    The D is silent.

    View Slide

  3. BlogIt
    The future of online writing, maybe.

    View Slide

  4. $ django-admin startproject blogit
    blogit
    ├── blogit
    │ ├── __init__.py
    │ ├── settings.py
    │ ├── urls.py
    │ └── wsgi.py
    └── manage.py

    View Slide

  5. $ django-admin startapp blog
    * be sure to add new app in settings file

    View Slide

  6. from django.db import models
    class Author(models.Model):
    name = models.CharField(max_length=50)
    class Blog(models.Model):
    title = models.CharField(max_length=200)
    text = models.TextField()
    pub_date = models.DateTimeField('date published', auto_now_add=True,)
    author = models.ForeignKey(Author)

    View Slide

  7. $ python manage.py makemigrations blog

    View Slide

  8. Migrations for 'blog':
    0001_initial.py:
    - Create model Author
    - Create model Blog

    View Slide

  9. $ python manage.py migrate

    View Slide

  10. Operations to perform:
    Synchronize unmigrated apps: staticfiles, messages
    Apply all migrations: admin, blog, contenttypes, auth, sessions
    Synchronizing apps without migrations:
    Creating tables...
    Running deferred SQL...
    Installing custom SQL...
    Running migrations:
    Rendering model states... DONE
    Applying blog.0001_initial... OK

    View Slide

  11. View from Top
    The steward of control

    View Slide

  12. def post(request, post_id):
    post = get_object_or_404(Blog, pk=post_id)
    return render(request, 'post.html', {'post': post})

    View Slide

  13. Templates and Rendering
    Making things look beautiful

    View Slide

  14. {{ post.title }}
    Posted: {{ post.pub_date }}
    Author: {{post.author.name}}

    {{post.text}}

    View Slide

  15. Feature Request!
    CEO has epiphany!!

    View Slide

  16. def latest(request):
    posts = Blog.objects.all()
    return render(request, 'posts.html', {'posts': posts})

    View Slide

  17. {% for post in posts %}
    {{ post.title }}
    Posted: {{ post.pub_date }}
    Author: {{post.author.name}}

    {{post.text}}

    {% endfor %}

    View Slide

  18. Requirements
    Got a list checking it twice!

    View Slide

  19. dj-database-url==0.3.0
    dj-static==0.0.6
    Django==1.8
    gevent==1.0.1
    greenlet==0.4.5
    gunicorn==19.3.0
    honcho==0.6.6
    psycopg2==2.6
    static3==0.5.1
    static==1.1.1

    View Slide

  20. What is Deploying?
    The D isn’t silent, just like in the word difficult.

    View Slide

  21. Inquiries?
    @myusuf3
    Do something magical now!

    View Slide