Kitaab

Django notes

dev python web

published 1970-01-01 00:00

updated 2023-06-04 00:43

%date

basics

django-admin.py is stored in .local/lib/python3.7/django/bin

create a project: django-admin.py startproject mysite

start server: python3 manage.py runserver

create app within a project: python3 manage.py startapp appname

migrate the model: python3 manage.py migrate

add myapp migrations: add 'myapp.apps.myappConfig' to mysite/settings.py python3 manage.py makemigrations myapp python3 manage.py migrate

view migration 0001: python manage.py sqlmigrate myapp 0001

launch interactive shell: python manage.py shell

views

to add a view, we must first have a route routes are defined in urls.py files and are defined with django.urls.path() in the urlpatterns list

for an app subcomponent to the view, urls.py in the main site must provide a route to the app urls.py

each view should have an associated function in the urls path. this function takes a template and applies the necessary transformations by adding the specific data to that template. we do this by defining the context in the .py file and then adding the code to the .html file with {% %}

models

on creating a project we must migrate to create the database

models are created of fields, and each database is a class that extends the models.Model class from django.db

there are many sorts of Fields (as well as other variables within a model)

shell

django has a rich api for querying models, these are only examples:

get all entries:

.objects.all()

get the first entry:

.objects.filter(id=1)

.objects.get(pk=1)


Backlinks