$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

More Decks by Mahdi Yusuf

Other Decks in Programming


  1. Introduction to Django @myusuf3 because who reads documentation

  2. What is Django? The D is silent.

  3. BlogIt The future of online writing, maybe.

  4. $ django-admin startproject blogit blogit ├── blogit │ ├── __init__.py

    │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py
  5. $ django-admin startapp blog * be sure to add new

    app in settings file
  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)
  7. $ python manage.py makemigrations blog

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

    model Blog
  9. $ python manage.py migrate

  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
  11. View from Top The steward of control

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

    {'post': post})
  13. Templates and Rendering Making things look beautiful

  14. <h1>{{ post.title }}</h1> <h4> Posted: {{ post.pub_date }}</h4> <h4>Author: {{post.author.name}}</h4>

    <p> {{post.text}} </p>
  15. Feature Request! CEO has epiphany!!

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

  17. {% for post in posts %} <h1>{{ post.title }}</h1> <h4>

    Posted: {{ post.pub_date }}</h4> <h4>Author: {{post.author.name}}</h4> <p> {{post.text}} </p> {% endfor %}
  18. Requirements Got a list checking it twice!

  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

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

    the word difficult.
  21. Inquiries? @myusuf3 Do something magical now!