1. download django:
svn co http://code.djangoproject.com/svn/djang
2. symlink django to the site-packages directory
ln -s pathtodjango-trunk/django /usr/lib/python2.x/site-packages/django
3. set up django:
python pathtodjango-admin.py startproject project
cd project
python manage.py startapp addnums
4. edit settings.py to point to a database called addnums
4.5 add project.addnums to INSTALLED_APPS in settings.py
5. create the database addnums
6. create a model in models.py which will be in the addnums directory:
class Addnum(models.Model):
num1 = models.IntegerField("First number")
num2 = models.IntegerField("Second number")
# display result:
def __unicode__(self):
return "%s plus %s is %s" %(self.num1,self.num2,selfnum1+self.num2)
6.5 create the database table
python manage.py syncdb
7. Create a form in views.py:
from django.template import Context,loader,RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.forms import ModelForm
from project.addnums.models import Addnum
class Addnumform(forms.ModelForm):
class Meta:
model = Addnum
8. Create a view to display and get the data from the form
def getnum(request):
if request.POST:
form = Addnumform(request.POST)
if form.is_valid:
f = form.save()
#return a success view with the result
return HttpResponseRedirect("/success/%d/" % f.id)
else:
form = Addnumform()
return render_to_response('web/addnum.html',
context_instance=RequestContext(request,
{"form":form,
}))
9. create the success view:
def success(request,id):
nums = Addnum.objects.get(pk=id)
return render_to_response('web/addnum.html',
context_instance=RequestContext(request,
{"nums":nums,
}))
10. create urls for django to find your views in urls.py:
url(r'getnum/$','project.addnums.views.g
url(r'success/(?P
11. create a base template called base.html:
{% block content%}{% endblock %}
12. create getnums.html:
{% extends base.html %}
{%block content %}
{% if form.errors %} Please correct the errors below {% endif %}