The idea here is to have a template like minimum working example for Django in order to quickly get going with a simple (prototype) front-end.
This project (so far) includes
- a simple input form (data input),
- a simple file upload form,
- a basic data model,
- admin site integration,
- basic CSS styling,
- basic computations and result output.
and should look like this:
- Python 3+ environment (I use Conda) with Django 2.2+. If you use Conda, you can create the environment from django.yaml (included).
conda env create -f django.yaml
- Activate your django environment
conda activate django
- Migrate data base (SQLite by default, see below how to use PostgreSQL). Hint: check out http://inloop.github.io/sqlite-viewer/ to see SQLite contents
python manage.py makemigrations form
python manage.py makemigrations upload
python manage.py migrate
- Create superuser (you could skip this step)
python manage.py createsuperuser --username=admin --email=email@domain.com
- Run server
python manage.py runserver
The console should show something like:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 06, 2020 - 13:28:11
Django version 2.2.5, using settings 'basicdjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
- Try out the form and admin area (login with your superuser)
http://127.0.0.1:8000/form
http://127.0.0.1:8000/admin
- Model: data access layer
- Template: presentation layer
- View: bridge between model and template
- Open console at project location (new project folder will be created here)
- Initiate the project
django-admin startproject basicdjango
This will create a project folder including manage.py, which is a wrapper for django admin commands specific to this project. Apps have to be intergated into:
- settings.py (holds secret key (!) ... do not version)
- urls.py (routing within project/site)
Each project (e.g. basicdjango) can contain multiple apps (e.g. blog, store). In this example, "form" is an app.
python manage.py startapp form
In this example we define a form class in form/forms.py
. More information at:
- https://docs.djangoproject.com/en/2.2/topics/forms/
- https://docs.djangoproject.com/en/2.2/ref/forms/fields/
In form/views.py
we then render the form via
render(request, 'form.html', {'form': form})
See https://djangobook.com/mdj2-django-templates/
Site templates, at their most basic, are HTML files that are displayed by your browser. Django’s approach to web design is simple—keep Django logic and code separate from design. It’s very important to understand that Django’s templates are not simply Python code embedded into HTML; it’s not actually possible to execute Python code in a Django template.
A template tag is surrounded by {% and %}. A template tag does something, e.g.:
- Display Logic. E.g.
{% if %}...{% endif %}
- Loop Control. E.g.
{% for x in y %}...{% endfor %}
A template variable is surrounded by {{ and }}. A template variable is something, e.g.:
- Simple Variable. E.g.
{{ title }}
- Object Attribute. E.g.
{{ page.title }}
- Dictionary Lookup. E.g.
{{ dict.key }}
- List Index. E.g.
{{ list_items.0 }}
Check this link if you want to use multiple DBs: https://docs.djangoproject.com/en/3.0/topics/db/multi-db/ Or this link for multiple Posgtres schemas: https://www.amvtek.com/blog/posts/2014/Jun/13/accessing-multiple-postgres-schemas-from-django/
- Change DATABASES in basicdjango/settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'basicdjango',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432'
}
}
- Make sure the package
psycopg2
is installed in your (conda) environment - Migrate to the new database
python manage.py migrate
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("""SELECT * FROM table""")
result = cursor.fetchall()
print(result)
# When using pandas as pd this is even simpler:
result = pd.read_sql("""SELECT * FROM table""", connection)
print(result)
# Hint: when serving data as JSON from pandas, this might help:
result.to_dict('list')
- Create migrations
python manage.py makemigrations form
- Check SQL code (optional)
python manage.py sqlmigrate form 0001
- Migrate
python manage.py migrate
The migrate command will only run migrations for apps in settings.py: INSTALLED_APPS.
- Add the data model of our form to the admin site, in
form/admin.py
:
from django.contrib import admin
# Register your models here.
from .models import SomeModel # <-- NEW
admin.site.register(SomeModel) # <-- NEW
- Try the admin functions: http://127.0.0.1:8000/admin
It is possible to integrate Django into legacy databases.
- Set up database connection for Django to connect to existing database in
settings.py
file. - Auto-generate models from an existing database via inspectdb.
python manage.py inspectdb
2.1 You can save this as a file by using standard Unix output redirection.
python manage.py inspectdb > models.py
2.2 You can pass table names as an argument.
python manage.py inspectdb table1 table2
https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-inspectdb
Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0.
- Find your IP (e.g. ipconfig)
- Add your ip to
basicdjango/settings.py
, e.g.
ALLOWED_HOSTS = ['192.168.2.1']
- Run server
python manage.py runserver 0.0.0.0:8000
- Access the form from another device: http://192.168.2.1.8:8000/form
Test you site on https://www.pythonanywhere.com/
Pythonanywhere has a free tier hosting service for python applications (incl. django). A complete guide can be found here: https://help.pythonanywhere.com/pages/DeployExistingDjangoProject/
There are only two changes you have to make to this project:
- Adjust
basicdjango.settings.py
DEBUG = False # <- CHANGE
ALLOWED_HOSTS = ["myusername.pythonanywhere.com"] # <- CHANGE
- Migrate
python manage.py makemigrations basicdjango # Or any other app
python manage.py makemigrations
python manage.py migrate
- Get the required static files (CSS), i.e. open a console in the project dir and run
python manage.py collectstatic
Do not forget to manually upload unversioned files, e.g. your .env or local.ini file if you use one
- Very well written tutorial that covers all the basics: https://docs.djangoproject.com/en/3.0/intro/tutorial01/
- A nice blog project: https://djangocentral.com/building-a-blog-application-with-django/
- Integrating existing DBs: https://dev.to/idrisrampurawala/creating-django-models-of-an-existing-db-288m