Log in

No account? Create an account
Previous Entry Share Next Entry
how to add two numbers in django
For people coming from backgrounds of php or of application servers, the process by which django works is slightly puzzling. Maybe a simple tutorial that does the barest minimum would put them on the right path - so here is a attempt to do this wrt a thread that came up on the django-users mailing list. Comments?

1. download django:
svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk

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)
        form = Addnumform()
    return render_to_response('web/addnum.html',

9. create the success view:
def success(request,id):
    nums = Addnum.objects.get(pk=id)
    return render_to_response('web/addnum.html',

10. create urls for django to find your views in urls.py:


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 %}

{% csrf_token %} {{ form }} {% endblock %}

13. create success.html:
{% extends base.html %}
{%block content %}
{% endblock %}

14. add the directory holding your templates to the templates directory in settings.py

15. run python manage.py runserver

16. point browser to and start learning arithmetic.

  • 1
this does not look good in the journal as the code is not appearing properly - I have put up a proper version here:

Step 2 of the post can be replaced by a more 'portable' (in this imperfect world, people still use windows) method as described in http://djangosnippets.org/snippets/641/

A virtualenv is another good alternative for setting up the env and paths...

Version of django

It will be good if you can mention the version of the django that you have used

  • 1