Notes

Modify Model and Migration

The process of modifying a model:

  1. modify models.py
  2. python manage.py makemigrations to create migration record file
  3. python 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
2
3
4
5
6
7
<div class="progress">
{% if value >= 40 %}
<div class="progress-bar progress-bar-striped bg-yes" role="progressbar" style="width: {{ value }}% " aria-valuenow="10" aria-valuemin="0" aria-valuemax="100"></div>
{% else %}
<div class="progress-bar progress-bar-striped bg-no" role="progressbar" style="width: {{ value }}% " aria-valuenow="10" aria-valuemin="0" aria-valuemax="100"></div>
{% endif %}
</div>

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
2
3
4
5
from django.template.defaulttags import register

@register.filter
def get_range(value):
return range(value)

template

1
2
3
{% for _ in count|get_range %}
<option>{{ forloop.counter }}</option>
{% endfor %}
#2. context
1
2
3
...
render_to_response('foo.html', {..., 'range': range(10), ...}, ...)
...

and in the template:

1
2
3
{% for i in range %}
...
{% endfor %}