editor: You can use whatever you like, but unless you have strong feelings today use this: http://www.activestate.com/komodo-edit/downloads ## Mac or Linux, for Windows see other text file Install Python 2.7 http://www.python.org/download/releases/2.7.2/ Mac: Lion already has it installed Linux: apt-get install python, or use your local package manager. # Working directory ~/$ mkdir django ~/$ cd django # Install virtualenv Mac: ~/django$ curl http://peak.telecommunity.com/dist/ez_setup.py # or you can download it with a browser or wget ~/django$ python ez_setup.py ~/django$ python ez_setup.py virtualenv Linux: apt-get install python-virtualenv ## Create your virtualenvs ~/django$ virtualenv myenv ~/django$ . myenv\bin\activate (myenv) ~/django$ pip install django # Installs Django 1.3.1 ## Project: Polls! (myenv) ~/django$ django-admin.py startproject mysite (myenv) ~/django$ cd mysite (myenv) ~/django/mysite$ python manage.py runserver # Edit files settings.py: # Django settings for mysite project. import os.path PATH = os.path.abspath(os.path.dirname(__file__)) def relative(path): return os.path.abspath(os.path.join(PATH, path)) ... DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2'... 'NAME': relative('data.db'), # Or path to database file if using sqlite3. .... } ... TEMPLATE_DIRS = ( relative("templates"), # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. ) INSTALLED_APPS = ( 'django.contrib.auth', .... 'django.contrib.staticfiles', 'django.contrib.admin', ) ... urls.py: from django.conf.urls.defaults 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'^$', 'mysite.views.home', name='home'), # url(r'^mysite/', include('mysite.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)), ) # Sync the Database (myenv) ~/django/mysite$ python manage.py syncdb Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (Leave blank to use 'amjoconn'): demouser E-mail address: demouser@example.com Password: Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... No fixtures found. # Run server (myenv) ~/django/mysite$ python manage.py runserver # Startapp (myenv) ~/django/mysite$ python manage.py startapp polls # Edit files... polls/models.py: from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(max_length=200) votes = models.IntegerField() def __unicode__(self): return self.choice settings.py: INSTALLED_APPS = ( 'django.contrib.auth', ... 'polls' ) # Try this (myenv) ~/django/mysite$ python manage.py sql polls BEGIN; CREATE TABLE "polls_poll" ( "id" integer NOT NULL PRIMARY KEY, "question" varchar(200) NOT NULL, "pub_date" datetime NOT NULL ) ; CREATE TABLE "polls_choice" ( "id" integer NOT NULL PRIMARY KEY, "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"), "choice" varchar(200) NOT NULL, "votes" integer NOT NULL ) ; COMMIT; # Sync your db (myenv) ~/django/mysite$ python manage.py syncdb # Create polls/admin.py beside polls/models.py polls/admin.py: from polls.models import Poll, Choice from django.contrib import admin class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class PollAdmin(admin.ModelAdmin): list_display = ('question', 'pub_date') search_fields = ['question'] fieldsets = [ (None, {'fields': ['question']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [ChoiceInline] admin.site.register(Poll, PollAdmin) admin.site.register(Choice) # Run the server again (myenv) ~/django/mysite$ python manage.py runserver # Your own view: urls.py urls.py: from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', (r'^polls/$', 'polls.views.index'), (r'^polls/(?P\d+)/$', 'polls.views.detail'), #(r'^polls/(?P\d+)/results/$', 'polls.views.results'), #(r'^polls/(?P\d+)/vote/$', 'polls.views.vote'), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), ) # Your own views: views.py polls/views.py: from django.shortcuts import render, get_object_or_404 from polls.models import Poll def index(request): latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] # Like render_to_response but automatically includes the request context instance! return render(request, 'polls/index.html', dict(latest_poll_list=latest_poll_list)) def detail(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) return render(request, 'polls/detail.html', dict(poll=p)) # Your own views: templates (myenv) ~/django/mysite$ mkdir templates (myenv) ~/django/mysite$ cd templates (myenv) ~/django/mysite/templates$ touch base.html (myenv) ~/django/mysite/templates$ mkdir polls (myenv) ~/django/mysite/templates$ cd polls (myenv) ~/django/mysite/templates/polls$ touch index.html details.html templates/base.html: Polls {% block contents %}

Default Contents

You only see this if you don't override the block

{% endblock contents%} templates/polls/index.html: {% extends "base.html" %} {% block contents %}

Polls

{% endblock contents %} templates/polls/details.html: {% extends "base.html" %} {% block contents %}

{{ poll.question }}

    {% for choice in poll.choice_set.all %}
  • {{ choice.choice }}
  • {% endfor %}
{% endblock contents %} # Authentication is CAS (myenv) ~/django/mysite$ pip install django-cas Edit settings.py: #... MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django_cas.middleware.CASMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'django_cas.backends.CASBackend', ) CAS_SERVER_URL = "https://cas.uwaterloo.ca/cas/" # the https is important ROOT_URLCONF = 'mysite.urls' #... Edit urls.py: from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', (r'^polls/$', 'polls.views.index'), (r'^polls/(?P\d+)/$', 'polls.views.detail'), #(r'^polls/(?P\d+)/results/$', 'polls.views.results'), #(r'^polls/(?P\d+)/vote/$', 'polls.views.vote'), (r'^accounts/login/$', 'django_cas.views.login'), (r'^accounts/logout/$', 'django_cas.views.logout'), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), ) Edit polls/views.py: from django.shortcuts import render, get_object_or_404 from django.contrib.auth.decorators import login_required from polls.models import Poll @login_required #Python language feature called decorators, forces user to be authed def index(request): latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] # Like render_to_response but automatically includes the request context instance! return render(request, 'polls/index.html', dict(latest_poll_list=latest_poll_list)) @login_required def detail(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) return render(request, 'polls/detail.html', dict(poll=p)) (myenv) ~/django/mysite$ python manage.py shell >>> from django.contrib.auth.models import User #Just a models in a models.py like Polls >>> User.objects.all() [, ] >>> user = User.objects.get(username="amjoconn") >>> user.is_staff = True >>> user.is_superuser = True >>> user.save() # CTRL-D or type exit to see how to do it in Windows (myenv) ~/django/mysite$ python manage.py runserver