- Visual Studio Code
- Yarn
- Python 3.6
- Pip
- Pipenv
- Postgres
- Django 1.11
- 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
- ORM (talk to the database)
- Admin Panel
- User Authentication
- URL Dispatcher
- Template Engine
- Imagine a bubble
- What happens in the bubble stays in the bubble
- You can have as many bubbles as you want
- Because global dependencies are not okay
- Many times you will need different dependency versions
- Virtualenv
- Virtualenvwrapper
- Pipenv
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 Settings, Urls, and Apps
- Change the default behavior of Django
- Install more modules
- Remove default ones
- Django will look at this file whenever it starts
- 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 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
- Production-ready-size
- Cookiecutter Django
pipenv install cookiecutter
pipenv shell
cookiecutter https://github.com/pydanny/cookiecutter-djangopipenv --three
pipenv shell
pipenv install -r requirements/local.tx-
Production Settings: Settings that are gonna be loaded on the live server
-
Local Settings: Settings that are gonna be loaded on the local server
CREATE DATABASE databasename;
- 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.
-
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
url(r'^images/', include('sodagram.images.urls', namespace='images')) image = ImageSerializer()Hidden Model Fields in Django
- Django uses request and response objects to pass state through the system.
- When a page is requested, Django creates an HttpRequest object that contains metadata about the request.
- Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.
- Each view is responsible for returning an HttpResponse object.
- Mostly used attributes (POST, USER)
- Request and Response Objects
- Request Attributes set by Middleware (user)
- Django Middleware Documentation
- Understanding Django Middlewares
regex=r'(?P<image_id>[0-9]+)/like/'>>> Entry.objects.all()[:5] // returns first five objects- 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)pipenv install django-taggit
python manage.py migratepipenv install django-taggit-serializer- Check password documentation
check_password(raw_password)set_password(raw_password)
- Introduction to Json Web Tokens
- REST framework JWT Auth
pipenv install djangorestframework-jwt- Protection for our API
// 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