Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Quick Intro to Django

Quick Intro to Django

Hi, here's a short and quick intro to Django that I prepared for TechLadies Talk session.
My objective is to show how easy it is to create your first Django project and link it to Django Administration.

The setup that we're doing is also documented at Team AFA's Diary on http://techladies.github.io/afa-diary/prerequisites/

Hope you learn something!

Vina Melody

August 11, 2016
Tweet

More Decks by Vina Melody

Other Decks in Programming

Transcript

  1. ABOUT TECHLADIES ➤ TechLadies is a community-led, nonprofit initiative that

    introduces more women to programming. ➤ Find more information at http://www.techladies.co/ and at our Facebook page https://www.facebook.com/TechLadies/
  2. ABOUT ME ➤ Vina Rianti ➤ TechLadies Bootcamp, Team AFA

    ➤ WordPress, not really as a "full stack" PHP Developer :( ➤ Tons of new skills Tonight.... it's about sharing some nuggets about Django! In summary: 1. Python's Virtual Environment 2. Create new Django project 3. Django Administration
  3. PYTHON & PYTHON PACKAGES ➤ Python is a high-level, interpreted

    programming language. ➤ Readable, easy and quick to learn ➤ Used widely by academia and researchers due to large number of packages at Python Packages Index (https://pypi.python.org/pypi) ➤ Mac --> already installed, Windows --> need installation ➤ or.. https://try.jupyter.org/ for Python "on the fly"
  4. PYTHON VIRTUAL ENVIRONMENT ➤ Sandbox and isolated environment for Python

    ➤ Manage dependencies without dilemma ➤ Keep project directory clean ➤ On Mac, do 'pip install virtualenv' ➤ Use tool called virtualenvwrapper to make it even easier. The setup that we're doing is also documented at Team AFA's Diary on http://techladies.github.io/afa-diary/prerequisites/ More info on Virtualenv at http://docs.python-guide.org/en/latest/dev/virtualenvs/ pip install virtualenvwrapper
 export WORKON_HOME=~/Envs source /usr/local/bin/virtualenvwrapper.sh ➤ Create our virtualenv: ➤ mkvirtualenv --python=/usr/local/bin/python3 myvideoblog ➤ Create our project directory: myvideoblog
  5. WHAT IS DJANGO? ➤ Django (/ˈdʒæŋɡoʊ/ jang-goh) is a free

    and open source web application framework, written in Python. Let's just dive in ! !
  6. SETUP DJANGO: INSTALL, MIGRATE, ACTION! ➤ pip install django ➤

    django-admin startproject myvideoblog ➤ Open the project folder and observe the structure. ➤ Run the server: ./manage.py runserver
  7. DJANGO ADMINISTRATION: QUICK AND USER FRIENDLY ➤ It's built in

    and no further package needed ➤ Run migration ./manage.py migrate ➤ Create your account ./manage.py createsuperuser ➤ Visit admin url at http://localhost:8000/admin/ ➤ Observe Users and Groups feature
  8. GOT MORE TIME FOR MAGIC? ➤ Let's create app to

    handle Posts: django-admin startapp posts ➤ Plural for app, singular for model ➤ Add it to INSTALLED_APPS in settings.py ➤ Create the model in models.py from django.db import models # Create your models here. class Post(models.Model): user = models.ForeignKey('auth.User') created_on = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=512) video_url = models.URLField()
  9. GOT MORE TIME FOR MAGIC? ➤ Create the migration: ./manage.py

    makemigrations ➤ Django will automagically detect changes and create migration scripts ➤ Migrate it: ./manage.py migrate ➤ Create PostsAdmin class for Posts in admin.py from django.contrib import admin from . import models # Register your models here. class PostsAdmin(admin.ModelAdmin): pass admin.site.register(models.Post, PostsAdmin) ➤ Refresh the admin site again!
  10. WE MADE IT! ➤ Python Virtualenv and Virtualenvwrapper ➤ Django

    setup ➤ Django model ➤ Django administration site ➤ About 10 lines of Django code
  11. THAT'S IT FOR NOW ~~ JUST KEEP SWIMMING! ➤ Special

    thanks to our awesome mentor Martin Brochhaus ! ➤ Join PyLadies Slack community, links at http://pyladies.sg/ ➤ Singapore Djangonauts (http://www.meetup.com/Singapore- Djangonauts/) ➤ I'm still learning too and here to help!