Training Django
- Install virtual environement :
pip install virtualenv
- Create a virtual environment for a project :
virtualenv venv
- Begin to use env :
Windows:
env\Scripts\activate
Linux:
source /env/bin/activate
- For stop :
deactivate
pip install -r requirements.txt
pip freeze > requirements.txt
We put the CI for run tests : .github/workflows/pythonapp.yml for the beging that will test only test_pytest.py
django-admin startproject <mysite>
That will build folder mysite with folders and files :
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
I need to enter in my folder :
cd test_site
and run my server :
py manage.py runserver
you can check now your page : http://127.0.0.1:8000/
All available public IPs: Listen on all available public IPs use: (0 is a shortcut for 0.0.0.0. )
py manage.py runserver 0:8000
- we need to be at the same level of manage.py before to run this:
python manage.py startapp polls
that will create :
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
- We will add a file urls.py :
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
and put something in views.py :
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
that will read our function index when we enter in /polls/
- For rooting our new app And in test_site/urls.py we will import include and this line:
path('polls/', include('polls.urls')),
relaunch our server Now we can check the URL: http://localhost:8000/polls/
python manage.py migrate
we will set in the database: 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
- We want to create database now : We will build a model into polls/models.py
- For activate model in settings.py we need to add a line into INSTALLED_APPS: 'polls.apps.PollsConfig'
- Indicate model change : python manage.py makemigrations polls 3 bis) Indicate what kind of model : python manage.py sqlmigrate polls 0001
- add the new migration : python manage.py migrate
python manage.py createsuperuser
Now we can manage some data in the administation
we need to build a folder into polls and by convention add an other folder with the same name as our app (in this example polls ) i this folders we will put all template html we need we will manage thoses templates with urls.py and views.py
Test must be a file in polls the name of the file must begin by test or tests by convention In this system it s will find automaticly all tests if the name begin by test for launch tests in pools:
python manage.py test polls
we need to build a directory static in polls like templates static is in link with STATICFILES_FINDERS into Django AppDirectoriesFinder search « static » into each app of INSTALLED_APPS Into static we need also polls
polls/static/polls/style.css
load static styles.csspython manage.py runserver
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
polls/static/polls/images/background.png