## Editor: You can use whatever you like, but unless you have strong feelings today use this: http://www.activestate.com/komodo-edit/downloads ## Windows, for Mac or Linux see the other txt files Install Python 2.7 http://www.python.org/download/releases/2.7.2/ Download the MSI installer for your platform, either 32 bit or 64 bit Run the installer, accept the defaults Open a command prompt by searching for "cmd.exe" It should look like this: C:\Users\yourusername> Then create a place to work: C:\Users\yourusername> mkdir Django Download ez_setup.py: http://peak.telecommunity.com/dist/ez_setup.py And save it under C:\Users\yourusername\Django as .txt (or ideally .py) C:\Users\yourusername> cd Django ## If necessary C:\Users\yourusername\Django> move ez_setup_py.txt ez_setup.py C:\Users\yourusername\Django> C:\Python27\python.exe ez_setup.py C:\Users\yourusername\Django> C:\Python27\python.exe ez_setup.py virtualenv ## Create your virtualenvs C:\Users\yourusername\Django> C:\Python27\Scripts\virtualenv.exe myenv C:\Users\yourusername\Django> myenv\Scripts\activate.bat (myenv) C:\Users\yourusername\Django> pip install django # Installs Django 1.3.1 ## Project: Polls! (myenv) C:\Users\yourusername\Django> myenv\Scripts\python.exe myenv\Scripts\django-admin.py startproject mysite ## The virtualenv seems to be far from perfect under windows hence all the extreme relative path (myenv) C:\Users\yourusername\Django> cd mysite (myenv) C:\Users\yourusername\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) C:\Users\yourusername\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) C:\Users\yourusername\Django\mysite> python manage.py runserver # Startapp (myenv) C:\Users\yourusername\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) C:\Users\yourusername\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) C:\Users\yourusername\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) C:\Users\yourusername\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) C:\Users\yourusername\Django\mysite> mkdir templates (myenv) C:\Users\yourusername\Django\mysite> cd templates # Create a base.html file here (myenv) C:\Users\yourusername\Django\mysite\templates> mkdir polls (myenv) C:\Users\yourusername\Django\mysite\templates\polls>explorer . # Create two files one called index.html and one called details.html # You can also create new files in Komodo and Save AsÉ 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) C:\Users\yourusername\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) C:\Users\yourusername\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) C:\Users\yourusername\Django\mysite> python manage.py runserver