Create your first Django Project.

KBleivik
Site Admin
Posts: 178
Joined: Tue Sep 29, 2009 6:25 pm
Location: Moss Norway
Contact:

Create your first Django Project.

Postby KBleivik » Mon Jan 31, 2011 9:28 pm

I use the following book http://withdjango.com/ chapter 2 to set up a blog on Windows.

1. Create a project.

In DOS prompt issue:

cd\
md py\django
cd py\django
django-admin.py startproject mysite
cd mysite
dir

Now you have a Python Django project folder - py\django - with a mysite project folder with the following Python files:
manage.py
settings.py
urls.py
__init__.py

2. Run Django's built-in Webserver.

At the DOS prompt issue:

manage.py runserver

If everything is OK, you should get a message that the server is running at http://127.0.0.1:800/

Open that address in your browser and you should se a page that start with the message: "It worked".

3. Creating the Blog Application

Quit the Web Searer (CTRL + Break) and at the DOS prompt in the c:\py\django\mysite folder issue:

manage.py startapp blog

edit settings.py

Go to the bottom of the file and in INSTALLED_APPS tuple add the following line

'mysite.blog',

save and close the file.

4. Choose your Python editor and design your model.

Now you have to edit your models.py file that you find in the blog folder you just created. For the purpose of editing Python source code, we use a Python editor like IDLE or SPE that can both be downloaded with Python 2.5.1 from the following http://www.manning.com/sande/Installati ... ml#windows windows installer. We prefer SPE (Stani Michiels Python Editor) that can also be downloaded from the internet (e.g. http://pythonide.stani.be/). Load the editor from windows and open the file c:\py\django\mysite\blog\models.py

Delete the comment starting with #, and add the following lines:

Code: Select all

class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    timestamp = models.DateTimeField()


That is Python's object oriented code and if you master that, you will see that the BlogPost class is a subclass of an existing Django class, django.db.models.Model.

Save the file.

5. Setting up the Database

Since we use Python 5.2.1 and run our project on windows, we use the SQLite database that is distributed with Python 5.2.1. (Browse or search the Python25 library where you installed Python). SQLlite is not a stand alone database server like MySQL, Oracle or PostGreSQL that we would use on a web site. SQLite uses the file system on your disk and a database is simply a file. So in the c:\py\django folder go to the DOS prompt and make a new folder, db by issuing:

md db

We name our database, django.db. So the only thing that remains is to edit the settings.py file that we created in the c:\py\django\mysite folder. So in that folder issue:

edit the database settings in settings.py to:

Code: Select all

DATABASES = {
    'default': {
        'ENGINE': 'sqlite3',           
        'NAME': r'c:\py\django\db\django.db',
    }
}


and save the file.

Now you can make some tables in your database by issuing:

manage.py syncdb

at the DOS prompt. The screen is filled with what is created. At the bottom you can read:

You just installed Django's auth system, which means you don't have any super users installed. Would you like to create one now? (yes/no): yes

and fill in the requested information like user name etc. When everything is OK, at the DOS prompt issue:

cd..\db
dir

to observe that django.db is created as a file in your file system in the c:\py\django\db directory.

Continue here http://www.informit.com/articles/article.aspx?p=1273658 if you wan't to build a fast blog.

To learn Django, you can start on the official Django site: http://www.djangoproject.com/. The
documentation is so good that it can be read as a book.
Here is our recommended path through the
documentation:

1. Get a Birds eye view of the Framework. Read what is written on the home page.
2. Get an overview http://docs.djangoproject.com/en/dev/intro/overview/ of the Django framework.
3. Continue with the 4 tutorials: http://docs.djangoproject.com/en/dev/
4. We also recommend the above mentioned book: http://withdjango.com/
5. Participate in the Django community: http://www.djangoproject.com/community/
6. Google group: http://groups.google.com/group/django-users

Here http://www.djangosites.org/ you get a list of web pages developed with the Django framework.

Return to “Django”

Who is online

Users browsing this forum: No registered users and 1 guest