basant/basant/raw/django.wiki

52 lines
1.5 KiB
Plaintext

= 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 {% <code> %}
= 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:
<Model name>.objects.all()
get the first entry:
<Mn>.objects.filter(id=1)
<Mn>.objects.get(pk=1)