User Tools

Site Tools


programming:django

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming:django [2018/11/26 23:52] clemixprogramming: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
 +python manage.py migrate
 +</code>
 +
 +Create a super user account, which can login via http://localhost:8000/admin
 +<code bash>
 +python manage.py createsuperuser
 </code> </code>
  
-== Launch the server ==+==== Launch the server ====
 <code lang=bash> <code lang=bash>
 python manage.py runserver python manage.py runserver
 </code> </code>
  
-== 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 15: Line 22:
 </code> </code>
  
-== Register URL ==+==== Register URL ====
 In the polls/urls.py file include the following code: In the polls/urls.py file include the following code:
 <code python polls/urls.py> <code python polls/urls.py>
Line 69: Line 76:
 </code> </code>
  
-== Templates ==+==== Templates ====
 Templates are in the subfolder of the app. e.g. `mysite/polls/templates/*` Templates are in the subfolder of the app. e.g. `mysite/polls/templates/*`
 <code html polls/templates/polls/index.html> <code html polls/templates/polls/index.html>
Line 104: Line 111:
  
  
-== Namespace url patterns ==+==== Namespace url patterns ====
 Define the namespace by adding `app_name = <namespace>` inside of urls.py. Define the namespace by adding `app_name = <namespace>` inside of urls.py.
 <code python polls/urls.py> <code python polls/urls.py>
Line 118: Line 125:
 </code> </code>
  
-== 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 126: Line 133:
 </code> </code>
  
-=== Django Rest Framework ===+Run this command to store static files in proper folder: 
 +<code bash> 
 +python manage.py collectstatic 
 +</code> 
 +==== django_tables2 ==== 
 +=== Mixedin === 
 + 
 +<code python> 
 +class UsefulMixin(tables.Table): 
 +    age = AgeColumn() 
 +    options = tables.Column(empty_values=()) 
 + 
 +    def render_options(self): 
 +        return format_html('<button>DELETE</button>'
 +#    age = tables.Column() 
 + 
 +class InstanceTable(UsefulMixin, tables.Table): 
 +    age = AgeColumn() 
 +    opt = tables.LinkColumn('delete_instance', args=[A('pk')], text='delete'
 +    class Meta: 
 +        model = Instance 
 +        template_name = 'django_tables2/bootstrap.html' 
 + 
 +</code> 
 + 
 +===  Custom tables.Column === 
 +<code python> 
 +class AgeColumn(tables.Column): 
 +    def render(self, record): 
 +        diff = record.age() 
 +        color = '' 
 +        if (diff.seconds > 60 * 60 ): 
 +            color = 'red' 
 +        else: 
 +            color = 'green' 
 +        return format_html('<span style="color:{}">{}</span>',color, diff) 
 + 
 + 
 +class InstanceTable(tables.Table): 
 +    age = AgeColumn() 
 +    opt = tables.LinkColumn('delete_instance', args=[A('pk')], text='delete'
 +    class Meta: 
 +        model = Instance 
 +        template_name = 'django_tables2/bootstrap.html' 
 +</code> 
 + 
 +To make links of a value https://django-tables2.readthedocs.io/en/latest/pages/api-reference.html?highlight=LinkColumn#django_tables2.columns.LinkColumn 
 +==== Update database ==== 
 +Change the model file accordingly. [[https://docs.djangoproject.com/en/2.1/ref/django-admin/#makemigrations|doc:makemigrations]] 
 +<code bash> 
 +python manage.py makemigrations 
 +python manage.py migrate 
 +</code> 
 + 
 +==== Bootstrap ==== 
 +https://www.w3schools.com/bootstrap/default.asp 
 + 
 + 
 +==== Django Rest Framework ====
 https://www.django-rest-framework.org/tutorial/quickstart/ https://www.django-rest-framework.org/tutorial/quickstart/
  
programming/django.1543272738.txt.gz · Last modified: by clemix

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki