This will install Django on your machine.
pip install django
Create your repository folder then run:
django-admin startproject myproject
- manage.py: Runs commands for the project
- [projectname]/init.py: Tells python that the folder contains python code (also called dunder init)
- [projectname]/wsgi.py | asgi.py: Provides hooks for webservers such as Apache, Nginx, etc
- [projectname]/settings.py: Configures the Django project
- [projectname]/urls.py: Routes web requests based on URL
Navigate to your project path where manage.py exists, then run:
python manage.py runserver
This command will also create a SqLite database for your project.
- A component in a Django project
- Each app fits a specific purpose e.g. Blog, Forum, Wiki etc
- A folder with a set of python files
To create an app in your Django project, navigate to your project folder then run where your manage.py is:
python manage.py startapp myapp
Add your app name in settings.py in your main project folder.
App's folder structure
File/folder | Desc |
---|---|
apps.py | settings related to the app (rare to use) |
models.py | database schema, queries, etc |
admin.py | administrative tasks |
urls.py | route options |
views.py | end user views |
tests.py | project tests |
migrations/ | db migrations related files |
URL Patterns (routes user's request to its relevant view) --> Views uses Models (logics, db communications, etc) --> Templates (Response contents)
To run migration commands navigate to your main project folder where manage.py is, then run:
python manage.py makemigrations # to create a new migration
python manage.py showmigrations # to see your migrations
python manage.py migrate # to apply your migrations
python manage.py migrate <appname> <number> # to move to an specific migration
Add your model to your admin.py file, then create your super user by running:
from .models import Pet
# Register your models here.
@admin.register(Pet)
class Admin(admin.ModelAdmin):
pass
python3 manage.py createsuperuser
-
{{ variable }} e.g.
<h3>{{ pet.name }}</h3>
-
{% tag %} e.g. for loop
{% for pet in pets %} <li>{{ pet.name }}</li> {% endfor %}
-
{{ variable|filter }} e.g. datetime output formatting
<h3>{{ pet.name|capfirst }}</h3>
-
some template tags doesn't have end tags e.g.
{% url 'home' %} #output is / {% url 'pet_detail' pet.id %} #output /myapp/1/