Django Getting Started

My notes on learning Django. I'm using this Django Tutorial.

Set up a virtual Environment

Find instructions to do that here

Create a virtual environment with virtualenv.

## In the directory where you wish to create the environment
virtualenv nameOfEnvironment
## Move into folder that the environment was created
cd nameOfEnvironment
## activate that
source bin/activate
## Install Django
pip install Django

Every time you want to run Django.

Create a project

cd to project parent folder and run:

django-admin startproject name_of_project
cd name_of_project

Create app within project

python startapp polls

Migrate models

Good to do this at the very beginning of the site.

python migrate

If you have a new model you should run this first:

python makemigrations

Create a super user

python createsuperuser

Run a server

python runserver

Start a shell for DB actions

python shell

Working with URLs

Steps for starting an app

Create an app:

python startapp polls

Make a view. The very basics:

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at the polls index.")

Create a url for the view: app/ (may need to create the file)

from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),

Then point the root file to the app the root/ file should look like this:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('herds/', include('herds.urls')),