Django Tutorial

1) Installing Django

You can install either through pip or anaconda:

pip install django

conda install -c anaconda django=1.10.2

2) Creating your project

Run:

django-admin startproject projectname

3) Run server:

To run your server, navigate to your root directory and run:

python manage.py runserver

This starts a local server at http://127.0.0.1:8000/

Now the command line should look something like:

and every time you access that url, you should see a GET request notification on the command line.

4) To create a new app:

In your root directory run:

python manage.py startapp appname

This should create all the necessary starter files for your app in a folder with your app name. Your directory should now look something like:

My project was named seelviz so instead of the seelviz directory, you will see a directory with your project's name. That directory is kind of your central backbone directory, it won't display anything but you reference all your other apps and urls through that directory.

admin.py and models.py will be where you manage your database modes. views.py is where you manage your views aka your html/css files. tests.py is where you will put your tests.

5) Add references to your new app

Then add that app to settings.py by adding the name of your new app to the INSTALLED_APPS list of strings. It should look something like:

INSTALLED_APPS = [
    'appname',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Then add
url(r'^appname/', include('appname.urls'))

to urlpatterns in your main urls.py and be sure to import include in urls.py.

It should look somethign like:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^appname/', include('appname.urls')),
]

This allows your app to be accessed at the url: http://127.0.0.1:8000/appname

NOTE:

- Apps are essentailly different sections to your website e.g. Forum, Blog, Store
- they are accessed through whatever string you put in your urlpatterns in your main urls.py
    - in this case we used 'appname/'
- we can exchange apps between different websites/projects, like literally copy and paste an app's folder to another project and just add the app name to settings.py

6) Setting up your HTML/CSS files

Django keeps all these files separate and organized. The general concept is that inside each app folder, your going to have two other folders to house html/css files. One called 'templates' which is where your html wil go, and another called 'static' which is where your css/javascript and other bootstrap files will go. It's good practice to create folders under these templates and static folders with the name of the app each html/css/javascript file refers to because what you put in these templates and static folders become, in a way, global, i.e. different apps can have access to other apps' templates and static folders.

So your directory structure should look something like:

My app was named 'clarityviz' so your should see your app name whereever you see that.

7) Create your views:

In your app directory create the file 'urls.py' and add:

from django.conf.urls import url
from . import views # this imports views.py from the local package

urlpatterns = [
    url(r'^$', views.index, name='index')]

In appname/views.py add:

from django.http import HttpResponse

def index(request):
    return render(request, 'clarityviz/index.html')

The render(request, 'clarityviz/index.html') tells django to look in templates/clarityviz/index.html for your html file.


In [ ]: