Skip to content

Latest commit

 

History

History
289 lines (232 loc) · 8.36 KB

File metadata and controls

289 lines (232 loc) · 8.36 KB

Development Note

12/18/2017

Development Tools

  • Visual Studio Code
  • Yarn
  • Python 3.6
  • Pip
  • Pipenv
  • Postgres
  • Django 1.11

Django

  • Overview
  • Framework
  • Not a programming language
  • Very fast for developers
  • Secure
  • Handy
  • Used by (Instagram, Pinterest, Spotify, NASA)
  • A library is a tool
  • A framework is a way of life
  • A library is just functions you can call
  • A framework calls your code
  • You call the library
  • The framework calls you

Django comes with

  • ORM (talk to the database)
  • Admin Panel
  • User Authentication
  • URL Dispatcher
  • Template Engine

Virtual Environments

  • Imagine a bubble
  • What happens in the bubble stays in the bubble
  • You can have as many bubbles as you want

Why Virtual Environment?

  • Because global dependencies are not okay
  • Many times you will need different dependency versions

How?

  • Virtualenv
  • Virtualenvwrapper
  • Pipenv

Creating a Virtual Environment

pipenv --three 	        // create a bubble with python version 3
pipenv install django   // install django package in this bubble 
pipenv shell            // go into a bubble 
exit 	                // exit bubble  

Parts of Django

Settings, Urls, and Apps

Settings

  • Change the default behavior of Django
  • Install more modules
  • Remove default ones
  • Django will look at this file whenever it starts

Urls

  • The Django project is gonna be open to the internet
  • URLS are how you talk to the Django project and make him do things
  • When Django matches an URL it will execute a view function.

Apps

  • Apps are what the applications is made of
  • Apps have a defined scope and they responsibilities are very precise
  • A Django project can have as many apps as you want

Creating Django Project

pipenv install cookiecutter
pipenv shell
cookiecutter https://github.com/pydanny/cookiecutter-django

Installing the requirements

pipenv --three
pipenv shell
pipenv install -r requirements/local.tx

12/21/2017

Production Settings and Local Settings

  • Production Settings: Settings that are gonna be loaded on the live server

  • Local Settings: Settings that are gonna be loaded on the local server

Creating database with Postgres

CREATE DATABASE databasename;

Migrating

  • A migration is a process to change the shape of the database models
  • When we add, remove or update DB columns we have to migrate
python manage.py makemigrations
python manage.py migrate
  • Abstract base classes are useful when you want to put some common information into a number of other models.

Model Relationships

  • Django _set Automatically groups all the related objects into a property To call the objects related to the owner all we have to do is call ‘modelName_set’

  • Define many-to-many relationship

followers = models.ManyToManyField(‘self’)
following = models.ManyToManyField(‘self’)
pipenv install djangorestframework
  • Serializer converts Python Object to JSON (and other way around)
  • Bridge between Python and JavaScript

12/22/2017

Django URL Dispatcher

url(r'^images/', include('sodagram.images.urls', namespace='images')) 

Nested Relationships Serializers

image = ImageSerializer()

Hidden Model Fields in Django

The Request Object in Django

Making Queries

Capturing arguments on Django URL's

regex=r'(?P<image_id>[0-9]+)/like/'

12/23/17

>>> Entry.objects.all()[:5]         // returns first five objects

Getting User Profile

  • Fields to be shown when clicking profile
  • username
  • name
  • bio
  • website
  • post count
  • followers count
  • following count
  • images
  • Define UserProfileImageSerializer in images.serializers
class UserProfileImageSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Image
        fields =(
            'id',
            'file',
            'comment_count',
            'like_count'
        )
  • Import UserProfileImageSerializer from images.serializers to user.serializer
from sodagram.images import serializers as images_serializers

// and get images of this profile

images = images_serializers.UserProfileImageSerializer(many=True)

12/24/17

Hashtag

pipenv install django-taggit
python manage.py migrate
pipenv install django-taggit-serializer

12/28/17

Change password

JWT Token (JSON Web Token)

// include into base.py
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}
// include into urls.py
from rest_framework_jwt.views import obtain_jwt_token
#...

urlpatterns = [
    '',
    # ...

    url(r'^api-token-auth/', obtain_jwt_token),
]
  • API call using JSON Web Token

12/29/17

Login : Signup

Login with Facebook