Starting with Django - carloramferrer/Django-AWS GitHub Wiki
These are detailed steps to setup your Django Applications.
- Create a virtual environment inside your project folder.
python -m pip install virtualenv
python -m virtualenv env
env/Scripts/activate
- Create your Django Project
python -m pip install django==2.1.5
django-admin.py startproject <project name>
- Create your database and administrator
python manage.py migrate
python manage.py createsuperuser
- Test and run your project
python manage.py runserver.
Now, we can have our changes on our git repository before we add our application.
- Initialize a git repository
git init - Add a .gitignore file and include the pycache and virtual environment files:
env
*.DS_Store
*.pyc
__pycache__
- Add and commit changes
git add .andgit commit -m "initial commit" - Add your remote repository:
git remote add origin <repository url> - Push your changes to git
git push origin master - Create your application
python manage.py startapp <app name>
The following shall be added in settings.py
- Add your application to INSTALLED_APPS
INSTALLED_APPS = [
..
'<app name>',
..
]
- 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',
],
},
},
]
- Change your appropriate timezone
TIME_ZONE = 'Asia/Singapore'
- 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')
- In your templates folder create two files: base.html and index.html
- 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>
- 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 %}
- We need to create views and methods for this page.In views.py, we add:
def Index(request):
return render(request, 'index.html')
- 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)
- Test this:
python manage.py runserver - Commit and push this to your repository
git add .
git commit -m "creating applications in django"
git push origin master
You can find more references from RealPython