This guide assumes you have already created a virtual environment and Django is installed.
- create django project
django-admin startproject PROJECT
- cd PROJECT
- python manage.py runserver (check if it works)
- CONTROL+C to exit your server
- mkdir apps
- cd apps
- touch init.py (aka dunder file)
- python ../manage.py startapp APPNAME (creates APPNAME FOLDER)
- update settings.py (do not forget the comma):
INSTALLED_APPS = [
'apps.APPNAME',
...
]
- update PROJECT/urls.py (make sure your parens match, no $ in route):
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', include('apps.APPNAME.urls')),
]
- create PROJECT/apps/APPNAME/urls.py (routes file, use $, use comma):
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
]
- update views.py:
from django.shortcuts import render, redirect
def index(request):
return render(request, "APPNAME/index.html")
- create templates directory (and write something in index.html):
cd PROJECT/apps/APPNAME
mkdir templates
cd templates
mkdir APPNAME
cd APPNAME
touch index.html
- create static directories (PROJECT/apps/APPNAME/static/APPNAME/):
cd PROJECT/apps/APPNAME
mkdir static
cd static
mkdir APPNAME
cd APPNAME
mkdir css
mkdir images
mkdir js
- cd into PROJECT (directory with manage.py file inside)
- python manage.py runserver
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'second_app/css/style.css' %}">
<script src="{% static 'second_app/js/main.js' %}"></script>
</head>
<body>
<img src="{% static 'second_app/img/rick.gif' %}">
</body>
</html>
- urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^/en/(?P<djangoversion>[0-9]\.[0-9])/topics/http/urls/$', views.index)
]
- views.py:
from django.shortcuts import render, HttpResponse, redirect
# Create your views here.
def index(request, djangoversion):
print(djangoversion) # will result in a string e.g. "1.9"
print("hello, I am your first request")
return HttpResponse('hello')
{% csrf_token %}
in all form with method="POST"- access data via request.POST (not request.form as in flask)
python manage.py migrate
(directory with manage.py file inside)- bracket notation format for FILE.py:
request.session['key']
- dot notation format for FILE.html:
{{request.session.key}}
virtualenv djangoEnv
source djangoEnv/bin/activate
pip install django
{# {% load staticfiles %} #}