Django - robbiehume/CS-Notes GitHub Wiki
- Uses a design similar to MVC, called MVT (model, view, template)
- The view and template in Django’s MVT pattern make up the view in the MVC pattern of other web frameworks
- Although it’s based on the MVC pattern, Django handles the controller part itself
- There’s no need to define how the database and views interact, it’s all done for you
- Benefits of Django: rapid development, provides extensive library (security, sessions, authentication, etc.), it's very scalable
- A Django website consists of a single project that's split into separate apps
- The idea is that each app handles a self-contained task that the site needs to preform
- A Django project contains at least one app. But even when there are more apps in the Django project, you commonly refer to a Django project as a web app
- A Django site starts off as a project, and you build it up with a number of applications that each handle separate functionality. Each app follows the model-view-template pattern
-
Project:
- The Django project holds some configurations that apply to the project as a whole, such as project settings, URLs, shared templates and static files
-
App:
- Each app can have its own database, and it’ll have its own functions to control how it displays data to the user in HTML templates
- Each app also has its own URLs as well as its own HTML templates, and static files, such as JavaScript and CSS
-
django-admin start-project <project_name>
: creates a new project -
__init__.py
: tells Python to treat the directory -
asgi.py
/wsgi.py
: they allow django to communicate with the web server; you don't have to deal with them yourself -
settings.py
: overall settings file -
urls.py
: used to configure URL routes- This can be at the project- or app-level
-
manage.py
: a special command line tool to do different things -
python manage.py startapp <app_name>
(run from within project): create a new app- Then go into the project subdirectory (<proj_name>/<proj_name>) and add the app_name to
settings.py
INSTALLED_APPS
list
- Then go into the project subdirectory (<proj_name>/<proj_name>) and add the app_name to
-
Apps: At the heart of every Django website is apps, which are small pieces of the project that make up the entire website
- Apps typically contain some kind of database models, URLs, views, and other info about about a particular part of the website
- Views: functions that are in charge of processing a users request when they visit a certain URL or endpoint on the website.
-
URL Routing: to handle URL routing in a Django app, you create URL patterns in a list and attach different paths to those views
- This is how Django knows which view to fire off when a user visits a URL on the website
- This can be at the project or app level
- project-level (
my_project/my_project/urls.py
)from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('my_app.urls')) # this will forward all URLs that start with / to the my_app urls.py and be routed from there ]
- app-level (
my_project/my_app/urls.py
)from django.urls import path from . import views # gets all the local views urlpatterns = [ path('', views.hello_world, name='hello') # this will map / to the views.hello_world() function ]
-
from django.shortcuts import render, HttpResponse def hello_world(request): return HttpResponse('hello world') # or you can render a template, e.g. return render(request, 'home.html')
- Different modules:
- HttpResponse
- Can use blocks to extend templates and fill in variables
-
home.html
(extends abase.html
template that already exists: link){% extends 'base.html' %} {% block title %} Home Page {% endblock%} {% block content %} <p>this is the home page</p> {% endblock %}
-
- Need to register models in
admin.py
- Any time you make a change to your database models, you need to make a "migration"
python manage.py makemigrations
python manage.py migrate
- The migration is some automated code that Django will apply to the database, which allows you to change your models and update them, while maintaining that data and not breaking any previous schema or data
-
from django.db import models class TodoItem(models.Model): title = models.CharField(max_length=200) completed = models.BooleanField(default=False)
-
from django.contrib import admin from .models import TodoItem admin.site.register(TodoItem)