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 manage.py startapp polls

Migrate models

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

python manage.py migrate

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

python manage.py makemigrations

Create a super user

python manage.py createsuperuser

Run a server

python manage.py runserver

Start a shell for DB actions

python manage.py shell

Working with URLs

Steps for starting an app

Create an app:

python manage.py 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/views.py (may need to create the file)

from django.urls import path

from . import views

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

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

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

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