Django Quickstart
- #Python
 - #Django
 - #Tips
 - #Know-how
 
- 2019/05/11
 
What is Django?
A full-stack web framework for Python, on par with Ruby on Rails or Laravel.
Setup
Install Python, then install Django via pip. Here is the path from install to Hello World:
django-admin startproject sample
python3 manage.py startapp hello
Edit hello/views.py to return “Hello World,” add the app to INSTALLED_APPS in sample/settings.py, and wire it up in sample/urls.py. Run the dev server with python3 manage.py runserver and hit http://localhost:8000/hello/.
Remember: django-admin startproject creates the project directory with settings.py (for app configuration) and urls.py (routing).
Models
Define schemas in hello/models.py (created via startapp). Implement __str__. By default we use SQLite, but you can edit settings.py to point to another DB. After writing the model:
python3 manage.py makemigrations hello
python3 manage.py sqlmigrate hello 0001
python3 manage.py migrate
Open a shell via python3 manage.py shell to poke at the ORM:
>>> from hello.models import Choice, Question
>>> Question.objects.all()
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
>>> q.save()
Admin (http://localhost:8000/admin)
Create a superuser (python3 manage.py createsuperuser), register your models in hello/admin.py, and you can edit DB rows via the admin UI. Run the server and log in.
Views
Include hello/urls.py inside sample/urls.py and map routes such as hello/1/, hello/1/results/, etc. Templates use {{ var }} for variables and {% if %}, {% for %} for logic. Use render to pass context dictionaries. Prefer {% url 'namespace:view-name' %} over hardcoded paths. Define app_name to create namespaces.
Forms
Use {% csrf_token %} inside <form> to enable CSRF protection. {{ forloop.counter }} gives the loop index. Since Django follows MTV, handle logic inside views. Because Question and Choice have a 1:N relation (foreign key), you can insert data via the admin UI and test voting. Use reverse() to redirect without hardcoded URLs.
Generic views
Django ships with generic views. In urls.py you can point to class-based views, inherit from ListView, DetailView, etc., set template_name, model, and override get_queryset. The pk path segment feeds into DetailView automatically.
Tests
Write test cases in hello/tests.py and run them via python3 manage.py test hello.
Static files
Place CSS in hello/static/hello/style.css and load it with the {% static %} tag.
Admin customization
Modify hello/admin.py to change list displays, filters, search, and inline relationships. To override admin templates, locate Django’s source via python -c "import django; print(django.__path__)", copy templates from django/contrib/admin/templates into your own templates/ directory (matching the same structure), and edit away.
Packaging
To reuse the app, package it. Install setuptools, move the app directory out (mv hello/ ../django-hello/), create README.rst and LICENSE (BSD is common), and prepare it like any other Python package.