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.pyurls.pyfrom . import views and views.<function_name> Create the file
views.py and then from django.http import HttpResponsefrom django.shortcuts import render (to render an html page rather than a response)action="using {% %}"wsgi.pyWithin 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 runserverThis 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 migrateCall 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.datasetSome 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-nameIt 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
/migrationsadmin.pyapps.pymodels.pytests.pyviews.pyMaking an app:
python manage.py migratepython manage.py makemigrationsadmin.pyfrom .models import Postadmin.site.register(Post)To begin using the admin interface you need to create a super user:
python manage.py createsuperuserSecurity 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 staticfrom django.conf import settingsIn 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