• Check out your django version by typing following
     python -c "import django; print(django.get_version())"
  • Create a new folder in home folder eg: home/abc and type command
    django-admin.py startproject mysite

    This will create a mysite folder in abc document which consists of various files like

    mysite/

    manage.py          mysite/
    __init__.py
    settings.py
    urls.py
    wsgi.py

  • Now sync database, type commandcd abc/mysite   press enter
    python manage.py syncdb
  • To create a polls type command
    python manage.py startapp polls

This will create a directory polls
polls/
__init__.py
admin.py
models.py
tests.py
views.py

  • Go to abc > mysite >polls > manage.py and add
    from django.db import models
    
    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
    class Choice(models.Model):
        poll = models.ForeignKey(Poll)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
  • Edit the mysite/settings.py file again, and change the INSTALLED_APPS setting to include the string 'polls'. So it’ll look like this:
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'polls',
    )
  • Now Django knows to include the polls app. Run command:
    python manage.py sql polls

Table is created which looks like

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_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL
);
COMMIT;
  •  Now sync database
    python manage.py syncdb
  •  Now, let’s hop into the interactive Python shell and play around with the free API Django
    gives you. To invoke the Python shell, use this command:

    python manage.py shell
  • Once you’re in the shell, explore the database API:
    >>> from polls.models import Poll, Choice
    >>> Poll.objects.all()
    >>> from django.utils import timezone
    >>> p = Poll(question="What's new?", pub_date=timezone.now())
    >>> p.save()
    >>> p.id
    >>> p.question
    >>> p.pub_date
    >>> p.question = "What's up?" 
    >>> p.save()
    >>> Poll.objects.all()
        [<Poll: Poll object>]

Wait a minute. <Poll: Poll object> is, utterly, an unhelpful representation of this object.
Let’s fix that by editing the polls model (in the polls/models.py file) and adding a
__unicode__() method to both Poll and Choice. On Python 3, simply replace __unicode__
by __str__ in the following example:

  • Go to polls/models,py add
    from django.db import models
    
    class Poll(models.Model):
        # ...
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.questionclass Choice(models.Model):
        # ...
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.choice_text
  • Now open terminal type
    cd abc/mysite
    after entering into mysite folder type
    python manage.py runserver
  • Open browser type address
    localhost:8000/admin or localhost:8000
  • It displays it works but something has to change. Read that and you are done.