Starting with Django - carloramferrer/Django-AWS GitHub Wiki

Starting with Django

These are detailed steps to setup your Django Applications.

Creating a Django Project on a Virtual Environment

I. Setting up Django

  1. Create a virtual environment inside your project folder.
python -m pip install virtualenv
python -m virtualenv env
env/Scripts/activate
  1. Create your Django Project
python -m pip install django==2.1.5
django-admin.py startproject <project name>
  1. Create your database and administrator
python manage.py migrate
python manage.py createsuperuser
  1. Test and run your project python manage.py runserver.

II. Creating a Git Repository

Now, we can have our changes on our git repository before we add our application.

  1. Initialize a git repository git init
  2. Add a .gitignore file and include the pycache and virtual environment files:
env
*.DS_Store
*.pyc
__pycache__
  1. Add and commit changes git add . and git commit -m "initial commit"
  2. Add your remote repository: git remote add origin <repository url>
  3. Push your changes to git git push origin master
  4. Create your application python manage.py startapp <app name>

III. Adjusting Settings and File Directories

The following shall be added in settings.py

  1. Add your application to INSTALLED_APPS
INSTALLED_APPS = [
..
  '<app name>',
 ..
]
  1. Create a templates folder at the root directory and add the following code to your settings:
TEMPLATES_DIR = os.path.join(BASE_DIR,'templates')

add this directory in your TEMPLATES configuration:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
  1. Change your appropriate timezone
TIME_ZONE = 'Asia/Singapore'
  1. Add a static folder for your assets at the root directory and add the following settings:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/ 

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/static/'

# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

IV. Adding Templates

  1. In your templates folder create two files: base.html and index.html
  2. In base.html, create your standard html structure for your site.
{% load static %}
<!DOCTYPE html>
<html>
    <style>
    </style>
    <head>
    </head>
    <body>
        <div class="nav">
        </div>
        {% block content %}
        <!-- Content Goes here -->
        {% endblock content %}
        <div class="footer">
        </div>
    </body>
</html>
  1. Blocks will override parts of a template by its children template. Let's create a use index.html child template. Add the following:
{% extends "base.html" %}
{% load static %}
{% block content %}
<p>Hello World!</p>
{% endblock %}
  1. We need to create views and methods for this page.In views.py, we add:
def Index(request):
    return render(request, 'index.html')
  1. Lastly, we include this to our urls. We create a urls.py file in our application directory and add the following:
from . import views
from django.urls import path

urlpatterns = [
    path('home', views.Index, name='home'),
]

We import these urlpatterns to our main project urls.py

from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
..
path('', include('<app name>.urls')),
path('', include('django.contrib.auth.urls')),
..
]

and we add additional paths for our media files

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  1. Test this: python manage.py runserver
  2. Commit and push this to your repository
git add .
git commit -m "creating applications in django"
git push origin master

Models and Generic Views

You can find more references from RealPython

⚠️ **GitHub.com Fallback** ⚠️