Useful Commands
- django-admin : check if django is installed correctly
- django-admin startproject : create a django project
- python3 manage.py runserver : run the project on Port 8000
- python3 manage.py startapp : create an app in the project file
- python manage.py createsuperuser : create an admin user
- pip install django-cors-headers : install cors headers
Basic Project File Structure
- manage.py - don't tocuh this file. Gives list of commands we'll use
- wsgi.py - web serveer files
- urls.py - a url routing system. we'll add a list of paths
- settings.py - the main configurations to your django project
Useful Commands & Steps
- python3 manage.py startproject crm - create the django project
- python3 manage.py startapp accounts - create an application within the prject called accounts
- Register the app in the project in settings.py
Basic App File Structure Key Files
- admin.py
- models.py
- views.py
Setps done
- Create a urls.py file in accounts app
- Create views functions in views.py
- Link views.py funcions to app urls.py
- Link app urls.py to projects.py
- Django specifically looks for :
--- app
-----templates
------app
-------your html files
!!!note Be sure to name the folder templates; that's what django looks for
- We build the templates/accounts files
- Note media urls and static urls in settings.py
- Also note statict folder and how we import static iles into html templates *{% load static %}
-
Worked on models.py
-
python3 manage.py makemigrations & python3 manage.py migrate - the command used to create a model in models.py in the databse
-
Remember to register your models in admin.py
One to Many Relationship
- An Example : One Customer has placed 3 different orders
Many to Many Relationship
- An Example : A tag can have many products and a product can also have many tags.
- We added dummy data at this stage
- We work from an interactive console accesed by inputing the command python3 manage.py shell
- Query CheatSheet
- Worked on creating the variables that hold query results in views.py
- Also linked the value of these variables and input them in the respective html files
- We worked on the urls.py files as well as updating the href links for a tags across the templates and the
- str:pk : string : primary key
- {% csrf_token %} is a way of passing our data securely
- Note the update and delte order forms in templates and views.py
- Inline formsets are used to create multile forms within one form.
- {{ form.management_form }} - sends data for each individual form in the formset.
- Reference djngo filter package
- icontains means be case insensitive in the filter
- Would be wise to create an auth_view.py for auth view functions