Hello, I recently posted on my first impression on Django.
Generally, my first script for trying out anything new is a bookmark script because it can be as simple or complex as you want.
Before starting, this post assumes you have a working install of Django. I would strongly suggest looking over the tutorial here for a more in depth explanation as to what everything is doing. This post is only a mile high overview and is to simply show some of process of creating a project along with the (minimal) amount of code it takes.
Here’s the first part.
First lets setup a new project using the terminal. I’ll call this project “simplebookmark”.
My projects are created in /var/www/html so if this is different than yours, adjust your path accordingly
cd /var/www/html/ django-admin.py startproject simplebookmark
Now you should have a directory called simplebookmark and inside simplebookmark you should have the following files.
__init__.py
manage.py
settings.py
urls.py
We’re going to add a bookmark application to our simplebookmarks proejct. Inside the simplebookark directory run
python manage.py startapp bookmark
Now our structure has:
bookmark/
__init__.py
models.py
views.py
Now we’re going to need some tables setup. Our models file contains our table structure for the bookmark application. We’re going to need a table for categories and bookmarks. The bookmarks table will have a foreign key to the categories table.
Open models.py in your editor and add:
from django.db import models
class Categories(models.Model):
name = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.name
class Bookmarks(models.Model):
category = models.ForeignKey(Categories)
name = models.CharField(max_length=200)
url = models.CharField(max_length=255)
notes = models.TextField()
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.name
Now that we have our models setup, we have to configure our project to use the bookmarks app.
Open your project’s ‘settings.py’ file and enter your database settings (you will also need to create your database if you have not already done so).
DATABASE_ENGINE = 'mysql' DATABASE_NAME = 'simplebookmark' DATABASE_USER = 'username' DATABASE_PASSWORD = 'password'
Scroll to the INSTALLED_APPS section and add ‘simplebookmark.bookmark’ and ‘django.contrib.admin’ (django.contrib.admin tells django to autocreate the admin area)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'simplebookmark.bookmark',
'django.contrib.admin',
)
Alright, now we’re ready to generate some tables. In your terminal run
python manage.py sql bookmark
You should now see
BEGIN;
CREATE TABLE `bookmark_categories` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(200) NOT NULL,
`pub_date` datetime NOT NULL
)
;
CREATE TABLE `bookmark_bookmarks` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`category_id` integer NOT NULL,
`name` varchar(200) NOT NULL,
`url` varchar(255) NOT NULL,
`notes` longtext NOT NULL,
`pub_date` datetime NOT NULL
)
;
ALTER TABLE `bookmark_bookmarks` ADD CONSTRAINT category_id_refs_id_6dc05859 FOREIGN KEY (`category_id`) REFERENCES `bookmark_categories` (`id`);
COMMIT;
Now lets create the tables
python manage.py syncdb
As this is the first time you’ve synced, you will be asked to create a superuser, simply fill in the details with any criteria you want (remember the username and password you specify).
If that completed successfully, you can now check out all the tables generated (using PhpMyAdmin), you can see that we now have 13 tables, including one for our categories and bookmarks.

Before we can checkout the admin area, we have to update our urls.py file.
Open urls.py and uncomment the following lines.
from django.contrib import admin admin.autodiscover()
and
(r'^admin/(.*)', admin.site.root),
Now lets fire up the dev server and checkout our progress!!
Run
python manage.py runserver
The default port the dev server runs on is 8000, so open you web browser and goto http://127.0.0.1:8000/admin/
If everything is working correctly, you should see the login screen

Django Admin Login
Remember the username and password you entered when we ran the syncdb? Enter that now.
You should now see
Wait a minute, where’s the categories and bookmarks!! Simple, we have to tell django that the categories and bookmarks objects have an admin interface. To do this we have to create the admin.py file inside the bookmark directory and add
from simplebookmark.bookmark.models import Categories, Bookmarks from django.contrib import admin class BookmarksAdmin(admin.ModelAdmin): fields = ['category', 'name', 'url', 'notes', 'pub_date'] class CategoriesAdmin(admin.ModelAdmin): fields = ['name', 'pub_date'] admin.site.register(Bookmarks, BookmarksAdmin) admin.site.register(Categories, CategoriesAdmin)
Save the file and refresh the page.
You should now have

Great, but lets fix the “Bookmakrss” and “Categoriess”
To do this, we need to import ‘ugettext_lazy’. At the top of models.py add:
from django.utils.translation import ugettext_lazy as _
and for each class we need to define our verbose_names
class Meta:
verbose_name = _('Category')
verbose_name_plural = _('Categories')
and
class Meta:
verbose_name = _('Bookmark')
verbose_name_plural = _('Bookmarks')
Save and refresh the page and you should now have “Bookmarks” and “Categories”.
Now, lets create our first category. Click the “Add” button next to categories.
As you can see, we have a place to enter the category name, along with our date and time (including a nice popup calendar). Lets go ahead and create our first category.
Now lets checkout the bookmarks section.

As you can see, we have a typical form for adding our bookmarks, along with a nice drop box for selecting which category to assign the bookmark to. Also, notice the plus next to the dropbox. This is so that we can create a new category with out haveing to leave the page to add a category.
This is the very basics of Django. I have only been playing around with this wonderful framework a few days and there is so much more I could talk about (setting the Date Published to the current timestamp, adding multiple records at one time, filters, views, templates, etc, etc). I’ll have that for a later post.



This was great for a starter like me. Have you gotten any further with this app?
Glad you liked it.
I’ve only done a few more minor things, such as removing the date field and having it automatically filled in with the current timestamp, added filters and small stuff. Nothing really worth posting as it is all very basic and outlined in django’s tutorial.
hi thanks for shared.
how can i use in template?
i wanna use categories in template
thanks again
I really haven’t done much more with this as I’m busy with another project. The django site has pretty good documentation and you should easily be able to find an answer: http://www.djangoproject.com/
Thanks a lot for your assistance!
Good article, good looking weblog, added it to my favs!!