- In order to have the outline working on a Django project, the root folder needs to be added as a separate folder to the workspace in VS Code. After that, run
cmd+shift+pand selectPython: select Interpreter. Choose the option with('.venv')from the project directory. python manage.py migrate- syncs the settings and apps with the django project- Apps in Django are components
python manage.py createsuperuser- creating a superuser in theauthapplication to be able to log in fromadminapplication from Djangopython manage.py startapp app_name- create a new app in Django calledapp_namepython manage.py makemigrations- Syncs the database with the model. Command needs to be run in conjunction withmigratecommandpython manage.py shell- Open an interactive shell inside a Django project.from app.models import Class- imports a class created by the used in one of the applicationsclass_name.objects.all()- Lists all created objects of that class created by user inside the databaseclass_name.objects.create(field_1='', field_2='')- creates objects of the typeclass_nameinside the database
- Deleting all files and and folders except
__init__.pyfrommigrationsfolder inside an app and the sqlite db removes all data from from an app. Deleting the database removes also the users - model reference fields from Django official documentation can be found here
views.pyfrom an app contains the function or classes that affect the view of page. All functions that create a vew have to be added inurlpatternsfrom views.py in the project root directory.
- Started Python Django Web Framework - Full Course for Beginners from freeCodeCamp.org
- Methods for creating Python virtual environments:
virtualenv dir_name- creates a virtual environment indir_nameusing whatever python version is installed by default.virtualenv dir_name -p python3- creates a virtual environment indir_nameusing python version specified in pathvirtualenv . -p python3- creates a virtual environment in current dir, using a specific python version specified in path
- Virtual environments are activated with
source bin/activateand deactivated withdeactivate pip freeze- outputs installed packages in a requirements format.django-admin startproject name .- creating a new django projectpython manage.py runserver 192.168.0.113:8000- runs the webserver of a django project. Make sure the IP of the pc that is accessing the django server is added insrc/settings.pyin theALLOWED_HOSTS