Notes 10/01 on URLs and views

urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'main.views.home', name='home'),
    url(r'^hello/$', 'main.views.hello', name='hello'),
    url(r'^books/$', 'main.views.books', name='books'),
    # url(r'^bookswap/', include('bookswap.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),
)

views.py

# Create your views here.
from django.http import HttpResponse
from models import Book

def home(request):
    return HttpResponse('Welcome to my page. '+
        '<br>What is your name?'+
        '<form action="/hello/">'+
        '<input type="text" name="id">' +
        '<input type="submit" name="Submit">' +
        '</form>'
)

def hello(request):
    return HttpResponse('<b>Hello</b> <i>' +
        request.GET['id'] +
        '</i><br><a href="/">Back</a>')

def books(request):
    bs = Book.objects.all()
    buf = ''
    for b in bs:
        buf += b.title + '<br>'
    return HttpResponse(buf)