English 中文(简体)
Python for web scripting
原标题:

I m just starting out with Python and have practiced so far in the IDLE interface. Now I d like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, or well, vice-versa. (I m assuming HTML is allowed in Python, just like PHP? If not, are there any modules/template engines for that?)

What modules do I need to install to run .py from my localhost? Googling a bit, it seems there re various methods — mod_python, FastCGI etc.. which one should I use and how to install it with MAMP Pro 1.8.2?

Many thanks

最佳回答

I think probably the easiest way for you to get started is to work with something like Django. It s a top-to-bottom web development stack which provides you with everything you need to develop and run a backend server. Things can be very simple in that world, no need to mess around with mod_python or FastCGI unless you really have the need.

It s also nice because it conforms to WSGI, which is a Python standard which allows you to plug together unrelated bits of reusable code to add specific functionality to your web app when needed (say for example on-the-fly gzip compression, or OpenID authentication). Once you have outgrown the default Django stack, or want to change something specific you can go down this road if you want.

Those are a few pointers to get you started. You could also look at other alternative frameworks such as TurboGears or paste if you wanted but Django is a great way to get something up and running quickly. Anyway, I m sure you ll enjoy the experience: WSGI makes it a real joy knocking up web apps with the wealth of Python code you ll find on the web.

[edit: you may find it helpful to browse some of the may Django related questions here on stack-overflow if you run into problems]

问题回答

You asked whether HTML is allowed within Python, which indicates that you still think too much in PHP terms about it. Contrary to PHP, Python was not designed to create dynamic web-pages. Instead, it was designed as a stand-alone, general-purpose programming language. Therefore you will not be able to put HTML into Python. There are some templating libraries which allow you to go the other way around, somewhat, but that s a completely different issue.

With things like Django or TurboGears or all the other web-frameworks, you essentially set up a small, stand-alone web-server (which comes bundled with the framework so you don t have to do anything), tell the server which function should handle what URL and then write those functions. In the simplest case, each URL you specify has its own function.

That handler function (or view function in Django terminology) receives a request object in which interesting info about the just-received request is contained. It then does whatever processing is required (a DB query for example). Finally, it produces some output, which is returned to the client. A typical way to get the output is to have some data passed to a template where it is rendered together with some HTML.

So, the HTML is separated in a template (in the typical case) and is not in the Python code.

About Python 3: I think you will find that the vast majority of all Python development going on in the world is still with Python 2.*. As others have pointed out here, Python 3 is just coming out, most of the good stuff is not available for it yet, and you shouldn t be bothered about that.

My advise: Grab yourself Python 2.6 and Django 1.1 and dive in. It s fun.

Django is definitely not the easiest way.

check out pylons. http://pylonshq.com/

also check sqlalchemy for sql related stuff. Very cool library.

On the other hand, you can always start with something very simple like mako for templating. http://www.makotemplates.org/





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签