==== Create a project ==== django-admin startproject mysite python manage.py migrate Create a super user account, which can login via http://localhost:8000/admin python manage.py createsuperuser ==== Launch the server ==== python manage.py runserver ==== Create a app inside a project ==== Everything are apps. Each app has it's own subfolder. To create a app sceleton run python manage.py startapp polls ==== Register URL ==== In the polls/urls.py file include the following code: polls/urls.py¶ from django.urls import path from . import views urlpatterns = [ # ex: /polls/ path('', views.index, name='index'), # ex: /polls/5/ path('/', views.detail, name='detail'), # ex: /polls/5/results/ path('/results/', views.results, name='results'), # ex: /polls/5/vote/ path('/vote/', views.vote, name='vote'), ] The corresponding view looks like this. The name correspond with the method name of views.py. from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.") def detail(request, question_id): return HttpResponse("You're looking at question %s." % question_id) def results(request, question_id): response = "You're looking at the results of question %s." return HttpResponse(response % question_id) def vote(request, question_id): return HttpResponse("You're voting on question %s." % question_id) Now we need to add this url to our project: from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ] ==== Templates ==== Templates are in the subfolder of the app. e.g. `mysite/polls/templates/*` {% if latest_question_list %} {% else %}

No polls are available.

{% endif %}
To render such a templates you need to do response the corresponding HttpResponse: latest_question_list = Question.objects.order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = { 'latest_question_list': latest_question_list, } return HttpResponse(template.render(context, request)) or in short from django.shortcuts import render def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) ==== Namespace url patterns ==== Define the namespace by adding `app_name = ` inside of urls.py. app_name = 'polls' urlpatterns = [ # ex: /polls/ path('/', views.detail, name ='detail'); ] Then you need to specfiy the namespace in `{% url %}` of the template like this
  • {{ question.question_text }}
  • ==== Static files ==== Static files can be placed in the `static/` folder of each app. Then refere to them inside the template like: {% load static %} Run this command to store static files in proper folder: python manage.py collectstatic ==== django_tables2 ==== === Mixedin === class UsefulMixin(tables.Table): age = AgeColumn() options = tables.Column(empty_values=()) def render_options(self): return format_html('') # 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' === Custom tables.Column === 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('{}',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' 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]] python manage.py makemigrations python manage.py migrate ==== Bootstrap ==== https://www.w3schools.com/bootstrap/default.asp ==== Django Rest Framework ==== https://www.django-rest-framework.org/tutorial/quickstart/