Board » Webapp Framework Comparison » Logs » Django

Page: 1 2 Next

ok .. let's try django .. i already know django for a year.. so the time i need is probably not really comparable to other frameworks.. but the result is .. (i guess)

so .. i skip the download + setup procedure (which .. boils down to a SVN checkout + symlink into python's site-packages directory)


Hey, we have Signatures !!! Great, isn't it ? ;)
creating a new project for our site.. this does not really generate code.. just empty files .. (except manage.py which is only used during development)

herbert-pouls-computer:~/dev/frameworkcomparison/django herbert$ ~/dev/python/django_trunk/django/bin/django-admin.py startproject barsite
herbert-pouls-computer:~/dev/frameworkcomparison/django herbert$ find .
.
./barsite
./barsite/__init__.py
./barsite/manage.py
./barsite/settings.py
./barsite/urls.py

Hey, we have Signatures !!! Great, isn't it ? ;)
as a next step we create our app:

(again .. it only creates empty files)

herbert-pouls-computer:~/dev/frameworkcomparison/django/barsite herbert$ ./manage.py startapp barcrud
herbert-pouls-computer:~/dev/frameworkcomparison/django/barsite herbert$ find barcrud 
barcrud
barcrud/__init__.py
barcrud/models.py
barcrud/views.py
herbert-pouls-computer:~/dev/frameworkcomparison/django/barsite herbert$ 


and we also need to add it to our list of INSTALLED_APPS

Hey, we have Signatures !!! Great, isn't it ? ;)
to our models ..
as described in WebappFrameworkComparison/Planning


from django.db import models
from django.contrib.auth.models import User

# Create your models here.

WIFI_CHOICES = (
    ( 'y', 'yes', ),
    ( 'n', 'no', ),
    ( 'p', 'paid', ),
)

class Bar(models.Model):
    name = models.CharField(max_length=250)
    location = models.CharField(max_length=250)
    wifi = models.CharField(max_length=1, choices = WIFI_CHOICES)
    creationdate = models.DateTimeField()
    editdate = models.DateTimeField()
    lasteditor = models.ForeignKey(User)


class Beer(models.Model):
    name = models.CharField(max_length = 250)

class BarBeer(models.Model):
    bar = models.ForeignKey(Bar)
    beer = models.ForeignKey(Beer)

class Comment(models.Model):
    author = models.ForeignKey(User)
    comment = models.TextField()


(I decided to use my own model 'BarBeer' instead of going with the \ManyToMany relation because this would allow us to add additional settings to the relation, like a price or a 'bottle' flag)
Hey, we have Signatures !!! Great, isn't it ? ;)
this creates the following sql statements for us (which are automatically executed)

herbert-pouls-computer:~/dev/frameworkcomparison/django/barsite herbert$ ./manage.py sql barcrud
BEGIN;
CREATE TABLE "barcrud_comment" (
    "id" serial NOT NULL PRIMARY KEY,
    "author_id" integer NOT NULL,
    "comment" text NOT NULL
)
;
CREATE TABLE "barcrud_beer" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(250) NOT NULL
)
;
CREATE TABLE "barcrud_bar" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(250) NOT NULL,
    "location" varchar(250) NOT NULL,
    "wifi" varchar(1) NOT NULL,
    "creationdate" timestamp with time zone NOT NULL,
    "editdate" timestamp with time zone NOT NULL,
    "lasteditor_id" integer NOT NULL
)
;
CREATE TABLE "barcrud_barbeer" (
    "id" serial NOT NULL PRIMARY KEY,
    "bar_id" integer NOT NULL REFERENCES "barcrud_bar" ("id") DEFERRABLE INITIALLY DEFERRED,
    "beer_id" integer NOT NULL REFERENCES "barcrud_beer" ("id") DEFERRABLE INITIALLY DEFERRED
)
;
-- The following references should be added but depend on non-existent tables:
-- ALTER TABLE "barcrud_comment" ADD CONSTRAINT author_id_refs_id_e9bdfa FOREIGN KEY ("author_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED;
-- ALTER TABLE "barcrud_bar" ADD CONSTRAINT lasteditor_id_refs_id_2e7c7016 FOREIGN KEY ("lasteditor_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;
herbert-pouls-computer:~/dev/frameworkcomparison/django/barsite herbert$ 

Hey, we have Signatures !!! Great, isn't it ? ;)
ok .. we have a new view with some output

the URL config:

from django.conf.urls.defaults import *

urlpatterns = patterns('barcrud.views',
                       (r'^$', 'list'),
                       )


the view code:


def list(request):
    bars = Bar.objects.all()

    return render_to_response('barcrud/listbars.html',
                              { 'bars': bars, },
                              context_instance = RequestContext(request),
                              )


there is still nothing to see .. since we have no real template yet ..

i just integrate the django auth system now .. so users can login and post new bars.. let's see how that works out ..
Hey, we have Signatures !!! Great, isn't it ? ;)
so .. login/logout works perfectly ..

so get to the create bar part ...
Hey, we have Signatures !!! Great, isn't it ? ;)
ok .. i now have creating of bars, list of bars and bar details

and i'm pretty sure editing should also work one i've put in a link .. let's try
Hey, we have Signatures !!! Great, isn't it ? ;)
ok .. i was right .. editing works perfect..

now add comments.. (i'm not using the django comments application .. i want to create a simple interface from scratch)
Hey, we have Signatures !!! Great, isn't it ? ;)
ok.. comments are working .. now add some beers..
Hey, we have Signatures !!! Great, isn't it ? ;)

Page: 1 2 Next

Please login to post a reply.

Personal website and blog of Herbert Poul. Also check out my Photo Gallery.




Herby's Photo Gallery

Subscriptions

User

You are not logged in.
Login
Register