Django Learning Notes
Notes
Modify Model and Migration
The process of modifying a model:
- modify models.py
python manage.py makemigrations
to create migration record filepython manage.py migrate
to sync the operation to database
Python shell with Django env
1 | python manage.py shell |
we use manage.py
to import env variable from settings.py
Django Static files
1 | python manage.py collectstatic |
remember to use this command after update the static file
Django template with customize style
1 | <div class="progress"> |
In fact I havn’t find a better way to do this. (e.g use if-else in the class
tag)
Noticed that we can use {{var}}
in the tag to involve variable to html.
Numeric for loop in Django
ref: https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates
#1. Register
views.py
1 | from django.template.defaulttags import register |
template
1 | {% for _ in count|get_range %} |
#2. context
1 | ... |
and in the template:
1 | {% for i in range %} |