Django Site Structure
It is typyically as follows:
1+- Hello-World <-- Project Root
2| |
3| +-- db.sqlite3 <-- Database
4| +-- manage.py <-- Management Tool
5| +-- myapp/ <-- Custom App
6| +-- forum/ <-- Custom App
7| +-- myadmin/ <-- Custom App
8| +-- mysite/ <-- Project Package
9|
10+- venv <-- virtual environment (Django + Python)
Each custom app is a project feature.
Default content for new "app"
1myapp
2 |
3 +-- __init__.py
4 +-- __pycache__
5 +-- admin.py
6 +-- apps.py
7 +-- migrations/
8 +-- models.py
9 +-- templates/
10 +-- tests.py
11 +-- views.py
init.py - usually an empty file that marks this directory as a Python package.
pycache - contains bytecode that makes the program start faster
admin.py - admin interface you can use to manage content. Register models in this file so they are available like below:
1from django.contrib import admin
2from myapp.models import Post
3admin.site.register(Post)
- apps.py - used to configure the app. Example, you can change the readable name for the app like this:
1from django.apps import AppConfig
2class MyConfig(AppConfig):
3 verbose_name = "Excellent App"
migrations - migration files for the app, used to apply changes to the database. Think of it as version control for the DB.
models.py - store information about the data you want ot work with. Typically, each model maps to a database table. Example of a "Flower" model (e.g. myapp/models.py:
1from django.db import models
2
3class Flower(models.Model):
4 title = models.CharField(max_length=255, default='')
Model is mapped to a db table called "Flower" and each attribute like "title" to a db field.
myapp/templates/myapp - for templates or views. Django has it's own template language where you mix static HTML, variables, tags and filters to generate final HTML. Always create each app subfolder inside templates folder.
tests.py - typical place for app testing code
views.py - views functions are here. View function takes a web request and returns a web response. Example, in
myapp/views.py, we used "myapp/index.html" as argument to the "render" function:
1from django.shortcuts import render
2
3def index(request):
4 return render(request, 'myapp/index.html') ## << here
Exploring the project package
1+- Hello-World
2| |
3| +-- db.sqlite3
4| +-- manage.py
5| +-- myapp/
6| +-- mysite/
7| |
8| +-- __init__.py
9| +-- __pycache__
10| +-- settings.py ## < here
11| +-- urls.py ## < here
12| +-- wsgi.py ## < here
Most of the project configuration happens in the settings.py file
Example, the default database configuration looks like this (in mysite/settings.py):
1DATABASES = {
2 'default': {
3 'ENGINE': 'django.db.backends.sqlite3',
4 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
5 }
6}
In a PostgreSQL database, we would do something as this:
1DATABASES = {
2 'default': {
3 'ENGINE': 'django.db.backends.postgresql_psycopg2',
4 'NAME': 'mysitedb',
5 'USER': 'username',
6 'PASSWORD': 'password',
7 'HOST': 'localhost',
8 'PORT': '',
9 }
10}
For "Heroku" platform, this is taken cared of by "django-heroku" package.
- urls.py - contains url patterns. When a user requests for specific page, it is here where Django checks for patterns and stops when a match is seen.
In our "Hello World" example, the index view will be called when user visits the homepage:
1from django.contrib import admin
2from django.urls import path
3
4from myapp import views as myapp_views
5
6urlpatterns = [
7 path('admin/', admin.site.urls),
8 path('', myapp_views.index, name='index'), ## < here
9]
- wsgi.py - default configuration for WSGI, a specification that deals with interaction between web servers and Python web application.