programming:django
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming:django [2019/01/18 12:17] – clemix | programming:django [2019/04/24 16:22] (current) – [Static files] clemix | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | == Create a project == | + | |
| + | ==== Create a project | ||
| <code bash> | <code bash> | ||
| django-admin startproject mysite | django-admin startproject mysite | ||
| Line 10: | Line 11: | ||
| </ | </ | ||
| - | == Launch the server == | + | ==== Launch the server |
| <code lang=bash> | <code lang=bash> | ||
| python manage.py runserver | python manage.py runserver | ||
| </ | </ | ||
| - | == Create a app inside a project== | + | ==== Create a app inside a project |
| Everything are apps. Each app has it's own subfolder. To create a app sceleton run | Everything are apps. Each app has it's own subfolder. To create a app sceleton run | ||
| <code lang=bash> | <code lang=bash> | ||
| Line 21: | Line 22: | ||
| </ | </ | ||
| - | == Register URL == | + | ==== Register URL ==== |
| In the polls/ | In the polls/ | ||
| <code python polls/ | <code python polls/ | ||
| Line 75: | Line 76: | ||
| </ | </ | ||
| - | == Templates == | + | ==== Templates |
| Templates are in the subfolder of the app. e.g. `mysite/ | Templates are in the subfolder of the app. e.g. `mysite/ | ||
| <code html polls/ | <code html polls/ | ||
| Line 110: | Line 111: | ||
| - | == Namespace url patterns == | + | ==== Namespace url patterns |
| Define the namespace by adding `app_name = < | Define the namespace by adding `app_name = < | ||
| <code python polls/ | <code python polls/ | ||
| Line 124: | Line 125: | ||
| </ | </ | ||
| - | == Static files == | + | ==== Static files ==== |
| Static files can be placed in the `static/` folder of each app. | Static files can be placed in the `static/` folder of each app. | ||
| Then refere to them inside the template like: | Then refere to them inside the template like: | ||
| Line 132: | Line 133: | ||
| </ | </ | ||
| - | === Update database === | + | Run this command to store static files in proper folder: |
| + | <code bash> | ||
| + | python manage.py collectstatic | ||
| + | </ | ||
| + | ==== django_tables2 ==== | ||
| + | === Mixedin === | ||
| + | |||
| + | <code python> | ||
| + | class UsefulMixin(tables.Table): | ||
| + | age = AgeColumn() | ||
| + | options = tables.Column(empty_values=()) | ||
| + | |||
| + | def render_options(self): | ||
| + | return format_html('< | ||
| + | # age = tables.Column() | ||
| + | |||
| + | class InstanceTable(UsefulMixin, | ||
| + | age = AgeColumn() | ||
| + | opt = tables.LinkColumn(' | ||
| + | class Meta: | ||
| + | model = Instance | ||
| + | template_name = ' | ||
| + | |||
| + | </ | ||
| + | |||
| + | === Custom tables.Column === | ||
| + | <code python> | ||
| + | class AgeColumn(tables.Column): | ||
| + | def render(self, | ||
| + | diff = record.age() | ||
| + | color = '' | ||
| + | if (diff.seconds > 60 * 60 ): | ||
| + | color = ' | ||
| + | else: | ||
| + | color = ' | ||
| + | return format_html('< | ||
| + | |||
| + | |||
| + | class InstanceTable(tables.Table): | ||
| + | age = AgeColumn() | ||
| + | opt = tables.LinkColumn(' | ||
| + | class Meta: | ||
| + | model = Instance | ||
| + | template_name = ' | ||
| + | </ | ||
| + | |||
| + | To make links of a value https:// | ||
| + | ==== Update database | ||
| Change the model file accordingly. [[https:// | Change the model file accordingly. [[https:// | ||
| <code bash> | <code bash> | ||
| python manage.py makemigrations | python manage.py makemigrations | ||
| python manage.py migrate | python manage.py migrate | ||
| - | </bash> | + | </code> |
| + | |||
| + | ==== Bootstrap ==== | ||
| + | https:// | ||
| - | === Django Rest Framework === | + | ==== Django Rest Framework |
| https:// | https:// | ||
programming/django.1547810227.txt.gz · Last modified: by clemix
