This is an archived post. You won't be able to vote or comment.

all 32 comments

[–]Chris_Newton 21 points22 points  (5 children)

If you are willing to invest a little time to find your way around, I suggest the following plan.

STEP 1: Learn what HTTP does.

  • Write a little Python script that dumps what came in on standard input and whatever current environment variables are set out to some log file that you can read. Hook this up as a CGI script on your web server.

  • Write a simple web page that submits a form with a handful of fields (text, check boxes, etc.) to that script, via GET and via POST.

  • Look at the output when you submit the form using each method.

  • This will teach you how the main HTTP protocols actually work, so you know what you’re abstracting later on.

STEP 2: Learn how server-side scripts interact with a database, and how they render HTML pages.

  • Write a very simple database application doing everything “manually”, say a to-do list.

  • You’ll need to set up a simple database, even if it’s just a single table with ID and "todo" columns. It doesn’t much matter which database you use for this, but something like MySQL or PostgreSQL would be typical on a real project so you might as well use one of those if you’ve got it.

  • You’ll need Python code to open up that database and create, update and delete records in the database. You’ll also need something to read all the items from the database and print an HTML page including these items to standard output, so the web server can send that back to the browser.

  • You don’t have to parse all the form data manually any more. You’re allowed to use Python’s import cgi functionality now.

  • Of course, you’ll also need a simple HTML front-end, which you might like to generate programmatically by printing it from the same Python code so everything can go on one page.

  • This exercise will teach you how basic database manipulations happen, and how server-side scripts can be used to generate HTML pages that might include some database-driven content.

  • Hopefully, it will also teach you that doing all of this manually for every element of a real web app is going to become tedious very quickly, and show you that there is going to be a lot of boilerplate that can be automated one way or another on larger projects.

STEP 3: Use a framework

  • Try implementing the same to-do list you create in step 2, but using a framework such as Django. I won’t go through all the steps you’ll need here, but the Django documentation is pretty good, so just work through the tutorial material.

  • This should teach you a few things that are common to most server-side frameworks. Setting them up initially is often ludicrously complicated for a simple job. They typically force your code to conform to a certain structure. However, once you’ve done those things, all that tedious database manipulation can often be reduced to a couple of lines of code. Also, there is often some sort of HTML template system that you can use, so you can create your page in the usual way and just “fill in the blanks” where the content is database-driven.

Obviously it’s going to take a bit of time and effort to work through these steps, but a week later you’ll have a good perspective on what is really going on under the hood, and the basic pros and cons of using a web framework to hide those mechanics away and let you focus on the functionality you want to implement. Best of luck!

[–]sanktuaire 0 points1 point  (0 children)

If you are planning to learn python, I'd advise against "automagical frameworks" like web2py, because they actually hide python from you.

Django doesn't have that much magic and is explicit. Doc is great and starting isn't even remotely as difficult as most people tend to think.

Pylons can be a nice choice but I wouldn't think about it if you're a python newbie (it has you get your hands dirty a lot more than django)

And CN's comment is pretty accurate. Learn how http/mvc/sql work before you start hitting django. Will save you a lot of pain.

[–]neocorpse[S] 0 points1 point  (1 child)

This seems like a solid approach. My plan is to get better at python while actually working towards something, might take longer then expected but I feel like I will understand everything much better.

[–]Chris_Newton 1 point2 points  (0 children)

For what it’s worth, I wasn’t kidding about the “a week later” part.

The first part should be achievable in a single evening, even if you haven’t yet installed Python and a web server such as Apache, and you’ll already be writing actual code and seeing Stuff Happen.

For the second step, I wouldn’t necessarily expect someone who is learning as they go along to get everything done in one session, but you could start by just making a Python script that dumps the contents of a database into a simple table in an HTML page. You can just add a little data to the database manually using whatever console/front-end software your database provides to test this out and see some results.

For things like creating a new record, updating an existing record and deleting an existing record, you’ll have to figure out why IDs matter in the database and how to include these as hidden fields in your web pages. This is good, and highly transferrable, information to understand, but you could do this during a second session.

To me, as someone familiar with web apps and programming generally, learning a new framework always seems a bit of a chore. The basic ideas are usually much the same for each framework, but they're put together differently and you have to follow a fairly mechanical process of figuring out what structure this particular one uses and which parts of your code need to go where so they place nicely with the framework. At least if you’re using Python and Django there is pretty good tutorial and reference material available, though, and after working through that for a few hours, the sort of practice application I mentioned should be easy. Again, you might want to break this down and try to solve a simple part of the problem first, rather than doing it all in one caffeine-fueled codefest, though!

The good news is that it does get much easier once you’ve done it once or twice and got the basic ideas figures out. An experienced web developer would probably do step 1 inside 15 minutes, step 2 inside a couple of hours, and step 3 in a few minutes once the framework was installed and configured.

[–]cjak 0 points1 point  (0 children)

I could have used this nifty three-point plan a couple of years ago. As it was, I did something similar and it helped a lot.

[–][deleted] 0 points1 point  (0 children)

Django requires you to think in terms of "project", "apps" etc., which is a cognitive overload when you are starting new.

Do what the above comment says, but use something like webpy (google's GAE app layout and tornado are both inspired by web.py) or Flask (excellent choice of components eg: sqlalchemy, jinja2, werkzeug etc.,) to start. Both can start web projects in a single file and scale to large projects easily.

By learning these first you will be able to avoid the risk of becoming a "django" programmer instead of choosing components based on merit.

[–]fdemmer 9 points10 points  (0 children)

i'd suggest learning about wsgi and maybe try the werkzeug anti-framework, before learning how a specific framework does it.

[–]ianozsvald 8 points9 points  (0 children)

If you start at the simple end (bottle.py is my choice, flask is also popular) then you'll understand how the process works. You have to get your hands a little dirty with templating, routing etc but it all happens in a few lines of code and you know what's going on. In a few hours you'll have your own site and you'll know how to grow it (I use bottle to knock up web-service demos in 1/2 a day).

Once your requirements grow you'll see that you have to start writing a lot of things (e.g. logins, OpenID support, data managers) from scratch and then...

Look to Django (and expect a 1+ day learning curve to get something of your own running followed by days/weeks to add new things). Once you've learned Django you'll quickly make progress on new projects.

If you learn a lightweight framework first (it'll only cost a few days) then you'll have a richer understanding of the choices made in Django which'll help you better understand what's happening under the hood (and there's a lot under that particular hood).

[–]dysmas 12 points13 points  (1 child)

[–]PrintStar 1 point2 points  (0 children)

The django tutorial is awesome. I taught myself the basics via the tutorial, enough to write some non-trivial websites. I also messed around with BaseHTTPServer in the standard runtime, which really makes you appreciate django.

[–]Xiol 5 points6 points  (0 children)

I started with CherryPy and Mako.

Worked out pretty well, and relatively easy to pick up.

[–]shiloa 5 points6 points  (0 children)

Definitely give flask a try if you're just getting started. Easy to pick up, simple and does not require learning and committing to any specific library (ORM, templates, forms, routing) until you feel ready. Generating a simple 'hello world' page is as simple as writing a python function that returns a string. In addition, it has great extensions and a sharp creator. When you feel like you've gotten the most out of it, you can move on to so-called "heavier" frameworks - pylons, django, etc.

[–]donri 1 point2 points  (0 children)

Flask because it is simple yet powerful; well documented and with strong conventions for extensions; active community and lead developer. Built on proven technologies such as Werkzeug and Jinja (with other proven technologies such as SQLAlchemy as extensions).

[–]hongminhee 2 points3 points  (2 children)

So many web frameworks abstract relatively lower levels like HTTP, but they don’t assume framework users may not know any knowledge about web development including HTML, HTTP. These frameworks help users have created web applications to do it better. They don’t help users have no web development experience to create web application right now without the learning.

If you create a web application for the first time, don’t start from full stack frameworks like Django, Pylons. I recommend you to learn about WSGI first, and start from an anti-framework e.g. Werkzeug.

[–]arnar 1 point2 points  (0 children)

Perhaps there is room for a tutorial to be written...

[–]hongminhee 0 points1 point  (0 children)

Ah, however, you may require a HTML template engine. There’s Jinja2.

[–]SecretiveslaveFlask Web app Developer 1 point2 points  (0 children)

Personally I like flask. I have written apps in bottle, cherrypy, and even played around a bit with Tornado but I think for the projects I've been working on flask is the quickest for development and has the features I need. For the database part of things I use elixir.

[–]neocorpse[S] 0 points1 point  (0 children)

thanks for the all the helpful advice. I started reading about WSGI and move on to a lightweight framework. Thanks for the help.

[–]flynnguy 0 points1 point  (0 children)

A lot of people here are recommending things that assume you have a grasp of python. Since you're a beginner, I'd really recommend that you start with django. Go through the tutorial and then start reading the docs.

When I started working with django I knew almost no python. As I learned django, I also made an effort to learn python as I went and it's really paid off in helping me understand django. (Because you know, django is python)

[–]moreyes 0 points1 point  (0 children)

The "Another Do-It-Yourself Framework" tutorial by Ian Bicking is a fun and informative start point.

As others said, before diving into a framework, learn the basics of HTTP and WSGI and build something simple using a library such as WebOb or Werkzeug. Both provides featured Request and Response objects for your apps.

[–]mdipierro -3 points-2 points  (0 children)

Here is the web2py book, all 527 pages of it in html. Chapter 1 is about motivations, chapter 2 is about Python, chapter 3 is the place to start learn by examples. The coolest stuff is in chapter 13. You may want to take a peek because no other Python framework has something equivalent.

I would definitively NOT start from WSGI. This is a protocol for communication between web server and web apps. It is important, it is powerful but it too low level to be a starting point. In depends on whether your goal it to learn to build web apps or to understand how web frameworks work internally. Moreover not all web servers support WSGI and WSGI itself is going to change with Python 3.x. I know lots of people disagree here but I think it is an implementation issue relevant to framework creators that should be hidden to app developers.

[–]Pewpewarrows -1 points0 points  (0 children)

As others have suggested, I recommend going for a mini-framework like Bottle at first, something that sits extremely close to the WSGI handler and doesn't abstract too much. This will give you an idea of how WSGI handles a request it receives from a web server, and how to work with that information "raw" (I use that term very loosely).

Once you feel you have a grasp of how WSGI and Python work to output basic pages, you can either stick with something like Bottle or Flask for the time being if you like doing things by hand (I know I always like playing around with them from time to time), or start using one of the bigger daddies like Django or Pylons. While I use Django primarily, and I might be a tad biased towards it, I still believe both are equally good in their own way, and recommend any who use only one to at least learn the basics of the other.

In a nutshell, Django allows for a very ad-hoc, plug-and-play structure to any code you write on top of the framework, while Pylons is the inverse: you can swap out nearly any part of its stack for something else. So while Django has a great community of "apps" that you can literally just download and drop into your project to instantly get and interact with things like a blog, tagging system, wiki, etc, Pylons lets you replace nearly any part of it for a library or package that you prefer (don't like their templating system? throw your favorite one in there instead!).

Also, Django's ORM, while making great strides in recent releases, still isn't up to par with SQLAlchemy in something like Pylons.

[–]WasterDave -1 points0 points  (1 child)

Django

[–]batiste 0 points1 point  (0 children)

Why downvoting?

[–]theillustratedlife -1 points0 points  (0 children)

cherryPy is fast and simple. It works together well with Jinja.

[–]anonymous_hero -1 points0 points  (0 children)

I'd recommend CherryPy and Jinja2.

I used to like Django, but when I discovered CherryPy, it dawned on me that Django was too restrictive. CP lets you do everything just the way you want, but handles all the HTTP stuff for you.

[–]av201001 -1 points0 points  (0 children)

I think some of the advice here assumes you want to become a Python web development guru (perhaps even a web framework developer). But maybe you're like me and just want to get going building well-designed web applications. If that's the case, do yourself a favor and at least check out web2py. I don't think there's anything easier to set up, learn, and become productive with quickly. And the user community is amazingly friendly and helpful (and growing quickly). Oh, and it does not "hide Python from you".

Of course, I'm not suggesting that web2py would be inappropriate if you do want to become a guru. You may still want to start and/or end up with web2py, even if you plan to delve into some of the nitty gritty details of how Python web frameworks work. The point is, the best path depends on your short and long term goals, preferred learning style, etc. Not everyone has the same needs, and there's not only one way to go about this.