How to make a database-driven website
July 21, 2010 8:15 AM   Subscribe

Building a super-simple database-driven website, from scratch - where to start?

I have a medium amount of experience in constructing MySQL databases, writing (offline) Python (and bash) scripts, and writing HTML... but only separately.

I now want to setup a web server and make a very basic database-driven site, but I don't really know where to start. I was looking at coding it in Python but only because I know Python, but I don't know whether to start from scratch or use some kind of framework like Django, or even whether I should avoid Python altogether and look at PHP. (Since PHP seems to be the default choice for this kind of thing, and my (Ubuntu LAMP) web server already understand PHP files, but not Python files, for some reason.)

Am I overlooking something? Where should I start?
posted by Mwongozi to Computers & Internet (9 answers total)
If you are already comfortable coding in python then PHP should have a very easy learning curve. A decade ago I built my first database driven website from scratch using MySQL + PHP. I had no prior experience with PHP at the point but I was a C++ programmer.

There are plenty of PHP tutorials but honestly I relied almost entirely on the material at
posted by special-k at 8:19 AM on July 21, 2010

There's no reason to use PHP if you know python. There is nothing really better about PHP.

I've written web apps in python without the use of any additional framework beyond pythons' basic cgi tools. It's doable. I've also written web applications in a number of other technologies, java servlets, tcl application servers, perl, etc. It's basically all the same, you use your language of choice to generate HTML and do mild CGI interactions (getting headers, form data, client data etc from the web servers, setting up headers and data to send out)

Most of the time I start from very little and build up the basic framework I need. I've done this a lot of times so it rarely takes me more than an afternoon to do this, the first time I probably piddled around with it more than was warranted.

Anyway, some of these frameworks may be great, I dunno, but don't feel like you need to box yourself into one.
posted by RustyBrooks at 8:22 AM on July 21, 2010

Whenever I build a website I try to find the highest-level way I can of doing it. So I start by considering if something like Wordpress (+plugins) will do what I need. After ruling that out, I might consider a more generalised CMS like Drupal. Then, if that's not going to do the trick either, it's usually time to pick a framework. I've had a certain amount of success with CodeIgniter, although for smaller projects I'll usually just fall back on my own set of PHP classes and function libraries that have evolved over the years.

My own feeling is that the more you can avoid writing new code from scratch, the better, unless it's a learning exercise, which is a different kettle of ballgames.
posted by le morte de bea arthur at 8:27 AM on July 21, 2010

Yes, this is (partly) a learning exercise. :)
posted by Mwongozi at 8:33 AM on July 21, 2010

If you're actually building something that you want to put on the web for people to use then generic PHP hosting is usually cheaper and more widespread than hosting with support for python / wsgi.

I'd sat it's worth learning a framework or two, even if you don't use them later, as the whole MVC structure is worth being familiar with.

Anyway, if you fancy having a go at things the python way then you could do worse than work through the Pylons book.
posted by Luddite at 9:35 AM on July 21, 2010

There are tons of open source DB-driven web apps. Go to somewhere like Freshmeat and start browsing. You don't necessarily have to understand the code in detail, just look at how they accomplish the major things: how the DB queries are structured, how query parameters are passed between the HTML form and the receiving script, how state is maintained across multiple actions, how HTML is rendered with DB result sets, etc. After you've got the general gist of it then sit down and write something simple from scratch. The from-scratch method is not recommended at all if your desire is to actually get work done as there are many existing building blocks that you can use to make life easier, but it is a good exercise if you want to learn how things come together.

A possible intermediate step would be to add a functionality to an existing script, if you're not quite completely solid on how things come together yet.
posted by Rhomboid at 12:36 PM on July 21, 2010

It's pretty easy to set up Ubuntu for Python. I've done it on my personal web Linode, as I've been working through the Django book in my motivated spare time. It's slightly out of date, but the online book allows people to comment on specific paragraphs, which is very helpful.

The most important note is that mod_python is no more. The replacement is WSGI, which is pretty easy to set up, and has some useful features for learning. Here's the Django doc on it; just make sure to install the Ubuntu package libapache2-mod-wsgi. WSGI is faster, lighter and has more features. One such feature: if you update the timestamp on the config file (touch website.wsgi), then your webapp source code will be reloaded. If you do this, other sites apache is running will remain up while you reload your website.

The key information you'll need to learn about integrating this all is HTML forms. Forms make the world go round, but because they rely on a script to process them, books on HTML are rarely useful on the subject, but books on web programming almost always have a section on this part of HTML. What Python/Django brings to the table over PHP may be counterintuitive: you can't easily mix Python and HTML. It's all too easy to write a PHP app that's one big file with inline HTML, javascript and PHP mixed together.

In the Python world people, have written template langauges to merge HTML and data. There's countless variations of this, including Django, Jinja, XSLT(!), Genshi, Mako, etc. Template engines seem to wander between Python frameworks quite easily, which probably accounts for their large number.
posted by pwnguin at 3:26 PM on July 21, 2010 [1 favorite]

Definitely with Django

- It has the best docs of any open source project I've seen. This is almost the best thing about the framework (apart from the framework itself, which rocks of course)
- It has an awesome community. Very active (and helpful) mailing list with a high tolerance for noob questions. Subscribe and read everyday. You'll see all of the pain points and learn all of the standard tricks/tips. Hang out on #django on If you hit a tough spot ask about it. We're very helpful on there!
- For a database-driven app Django is going to let you really press ahead quickly because of the built-in admin framework.
posted by i_am_a_Jedi at 6:25 PM on July 21, 2010

The major win for PHP here is trivial deployment on shared webhosting, as others have pointed out.

If that's not your situation (you have a server), then just use one of the *many* Python web frameworks.
Some choices (with GROSSLY SIMPLIFIED descriptions! this is an overview!):

tiny / zero-install / shallow curve / few features (maybe a win?)

(there are others, both of these are trivial)
* bottle

put your own pieces together

* werkzeug
* paste
* restish (an oddity!)


* django (if you go this route, do djangobook NOT the tutorial)
* pylons
* web2py (hard to classify, honestly!)

The major parts of the framework will be:

* request / response (i.e., taking an HTTP request, doing some stuff
and making sure that what comes back is valid)... think webob,
parts of django... you won't have to look at this :)
Forget about this for now :)
* URL routing.... mapping the url into some actions. All of these will
have some solution for this, either regex based, or routes based.
For simple stuff, who cares!
* Models... basically mapping database (datastore) things onto
python objects. This is django-models, or sqlachemy or whatever
thing web2py has! Some people find this stuff tasty -- I mostly find it
to be headache. The light frameworks won't come with these,
and you'll be on your own there. For your level, this might be fine!
* Templates.... basically stuff so you don't need to type so much html by hand
These include jinja2, genshi, mako. All are similar in features, and
most can work with any framework.
* A toy server for running it. For real deployment, stick your app behind
Apache or whatever. You won't need to for a while though, unless you're
getting dozens of hits per second or something. Worry about that later!

So why / why not Django:

- app admin is pretty great
- djangobook is great
- wide community; commonly known

- their version of jinja smells
- I hate their object model (compared to sqlalchemy)
- I'm not into the scaffolding / project directory magic
- their terminology (MTV vs MVC) seems crazy to me.

Me, I'd go with or Bottle! I tend to think the interesting part
is styling, jQuery and friends.
posted by gregglind at 6:32 PM on July 21, 2010 [1 favorite]

« Older Gift(s) for girlfriend traveling in SE Asia?   |   Am I doing pillows wrong? Newer »
This thread is closed to new comments.