Overview | Develop | Deploy | Data |
This is the backend section of the shared-world application built using Djano, with React as frontend. The project is deployed on Google App Engine Flexible Environment with access to Google Cloud Storage and Cloud SQL.
This is the most complicated project I have built using Django, and so this part of the project was used with the assistance of Youtube tutorials
Setup
These are the steps I went through to start this project.
Getting Started
- Install python
- Go to “Windows Powershell”
- Check python, pip and django-admin versions
python –version pip –version django-admin –version
- Allow execution of scripts
Set-ExecutionPolicy RemoteSigned
- Go to project directory and make directory for backend
cd shared.world mkdir backend
- Install and create virtual environment and activate
pip install virtualenv cd backend virtualenv env ./env/Scripts/activate
- Install django
pip install django
- Create django project and check inside directory
django-admin startproject shared_world cd shared_world dir
- Check server runs
python manage.py runserver
- Check server running at given address http://127.0.0.1:8000/
- Go to visualstudio and see the new folders
- Change shared_world folder to ‘src’
Django REST Framework
- Install djangorestframework
pip install djangorestframework
- Add to INSTALLED_APPS in shared_world – settings.py
- Add REST_FRAMEWORK in shared_world – settings.py
REST_FRAMEWORK = {'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'] }
- Add url path to shared_world – urls.py
Corsheaders
- Install cors headers
pip install django-cors-headers
- Add to INSTALLED_APPS in shared_world – settings.py
- Add ‘Corsheaders’ to middleware at top
MIDDLEWARE = ['corsheaders.middleware.CorsMiddleware']
- Allow cors by adding add at bottom
CORS_ORIGIN_ALLOW-ALL = True
Media
- Install pillow to handle media files
pip install pillow
- Add to shared_world - settings.py
MEDIA_ROOT=os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
- Add static to urlpatterns ```
- static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ```
Apps
- Start app (under src)
python manage.py startapp app_name
- Add ‘app_name’ to INSTALLED_APPS
- Create ‘app_name’ in models.py
- Register in admin.py
- Create api folder under ‘app_name’
- Create init.py file
- Create serializers.py file
- Create views.py file
- Create urls.py
Django Root
- Add ‘include’ to import
- add url paths to ‘urlpatterns’ for each app at shared_world - urls.py
- add each app to INSTALLED_APPS in shared_world – settings.py
- change to root project directory in shared_world – settings.py
ROOT_URLCONF = 'shared_world.urls' WSGI_APPLICATION = 'shared_world.wsgi.application'
Post
- author is ForeignKey relating to a profile_id
- country is ForeignKey relating to a country_name
- post_id is pk
- ListView of all psots
- ListView of posts by a single author
- ListView of posts for a country
- DetailView of post
- Serializer for fields of post
- Serializer for fields of post plus all informaiton for country and post
Profile
- extends the Django User model
- one-to-one with user
- when user is created so is a profile
- the profile_id is the pk
- ListView of all profile
- DetailView of single profile
Country
- uses choice fields to predefine the country names
- name is pk
- ListView of all countries
- DetailView of Country
Interests (taggit)
- Install taggit and taggit-serializer
pip install django-taggit pip install django-taggit-serializer
- add to INSTALLED_APPS in shared_world - settings.py
- Add to models (profile and post) – import and add taggable manager in model
from taggit.managers import TaggableManager
- Add to serializer – import and add
from taggit_serializer.serializers import (TagListSerializerField, TaggitSerializer)
- Add to shared_world - settings.py
TAGGIT_CASE_INSENSITIVE = True
- many-to-many relationship
- no restriction (max. was not able to be implemented successfully)