getting started with django

Date: March 16th 2016
Last updated: March 16th 2016

This entry contains the bare bones to get a django site up and running. I was mostly following the direction of https://docs.djangoproject.com/en/1.9/intro/tutorial01/ and https://jeffknupp.com/blog/2012/02/09/starting-a-django-project-the-right-way/.

command line upgrade

sudo apt-get install django --upgrade
python3

check version of django

import django
django.VERSION
# (1, 9, 4, 'final', 0)

create virtualenv

mkproject surfdiary
deactivate # leave virtual environment
workon surfdiary # work on virtual environment

start project

django-admin.py startproject mysite
# check it worked
cd mysite
python3 manage.py runserver 8080

check it worked: go to http://127.0.0.1:8080/

start an application

python3 manage.py startapp surferprofile

current directory tree

tree

## output
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── surferprofile
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

edit views.py

from django.http import HttpResponse
def index(request):
    return HttpResponse("Welcome to the surferprofile index page.")

create and connect urls.py for my surferprofile app
Connect the index page containing an HttpResponse for my surferprofile app to the website. However, this code alone is not enough, it returns a page not found error.

from django.conf.urls import url
from . import views
urlpatterns = [url(r'^$', views.index, name='index'), ]

modify the existing urls.py for mysite
After adding this code I can navigate to //127.0.0.1:8080/profile/ to see the HttpResponse created in views.py. The urls.py created for the surferprofile app is called which points to the index method sitting in views.py. admin was added by default.

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

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

results matching ""

    No results matching ""