django-admin startproject <project_name>
This sets up the project it generates a directory with as <project_name>
which has within it.
a manage.py
file and a sub-folder also called /<project_name>
the subfolder has the following files:
settings.py
urls.py
from . import views
and views.<function_name>
Create the file
views.py
and then from django.http import HttpResponse
from django.shortcuts import render
(to render an html page rather than a response)action="using {% %}"
wsgi.py
Within the top level project folder we should have a templates
folder to hold the html pages, if this isn't automatically generated by the startproject
then this should be created and added as a location in the settings file under TEMPLATES = [ { ... 'DIRS': ['templates'],...
of settings.py
.
Django uses objects, by convention and necessarily the objects begin with a capital. For example a table should be called People
The manage.py
file is for admin things, don't change the code in this file, it is used to manage the server with
$ python manage.py runserver
This starts the development server and can be seen in a browser at 127.0.0.1:8000 or localhost:8000
and generates a db.sqlite3 databaste (and a __pycache__
folder)
The file manage.py
manages the tables and updates when there are changes with
$ python manage.py makemigrations
$ python manage.py migrate
Call all DataFile objects related to a given Dataset
>>> ds = Dataset.objects.first()
>>> datafiles = ds.datafile_set.all()
Reversing this operation
>>> f = DataFile.objects.first()
>>> ds = f.dataset
Some other useful commands
>>> from django.db.models import Count, Min, Sum, Avg
>>> Dataset.objects.aggregate(oldest=Min('datafile__start_time')) {'oldest': datetime.datetime(2005, 12, 1, 0, 0, tzinfo=<UTC>)}
Create an app by:
python manage.py startapp apps-name
It is good practice to make a new app for each different thing, this means they can be re-used by other Django apps
Creates a directory apps-name
with
/migrations
admin.py
apps.py
models.py
tests.py
views.py
Making an app:
python manage.py migrate
python manage.py makemigrations
admin.py
from .models import Post
admin.site.register(Post)
To begin using the admin interface you need to create a super user:
python manage.py createsuperuser
Security issues options
1. random url for admin interface
2. Very strong password!
Need to install Pillow package
In urls.py
need:
from django.conf.urls.static import static
from django.conf import settings
In after urlpatterns [...]
include + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Go to settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/pics/'
MEDIA_ROOT = BASE_DIR
In the .html
file to include image use:
<img src="{{ post.image.url }}" />
When using bootstrap ensure images are within a container: <div class="container"> .... </div>
Modify the image tag with: class="img-responsive centre-block" style="max-height:150px;"
Use glyphicon exmaple code to add icons
pkill -9 -f runserver