﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Sealyu-随笔分类-Python</title><link>http://www.blogjava.net/sealyu/category/34557.html</link><description>--- The devil's in the Details</description><language>zh-cn</language><lastBuildDate>Tue, 18 May 2010 10:27:00 GMT</lastBuildDate><pubDate>Tue, 18 May 2010 10:27:00 GMT</pubDate><ttl>60</ttl><item><title>MySQLdb User's Guide</title><link>http://www.blogjava.net/sealyu/archive/2010/05/17/321217.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 17 May 2010 15:56:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2010/05/17/321217.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/321217.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2010/05/17/321217.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/321217.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/321217.html</trackback:ping><description><![CDATA[<div topic="">
<p first=""><a id="contents" name="contents">Contents</a></p>
<ul>
    <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#introduction" id="id1" name="id1">Introduction</a></li>
    <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#installation" id="id2" name="id2">Installation</a></li>
    <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysql" id="id3" name="id3">_mysql</a>
    <ul>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysql-c-api-translation" id="id4" name="id4">MySQL C API translation</a></li>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysql-c-api-function-mapping" id="id5" name="id5">MySQL C API function mapping</a></li>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#some-mysql-examples" id="id6" name="id6">Some _mysql examples</a></li>
    </ul>
    </li>
    <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysqldb" id="id7" name="id7">MySQLdb</a>
    <ul>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#functions-and-attributes" id="id8" name="id8">Functions and attributes</a></li>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#connection-objects" id="id9" name="id9">Connection Objects</a></li>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#cursor-objects" id="id10" name="id10">Cursor Objects</a></li>
        <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#some-examples" id="id11" name="id11">Some examples</a></li>
    </ul>
    </li>
    <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#using-and-extending" id="id12" name="id12">Using and extending</a></li>
    <li><a href="http://mysql-python.sourceforge.net/MySQLdb.html#embedded-server" id="id13" name="id13">Embedded Server</a></li>
</ul>
</div>
<div>
<h1><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id1" id="introduction" name="introduction">Introduction</a></h1>
<p>MySQLdb is an thread-compatible interface to the popular MySQL
database server that provides the Python database API.</p>
</div>
<div>
<h1><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id2" id="installation" name="installation">Installation</a></h1>
<p>The <tt literal="">README</tt>
file has complete installation instructions.</p>
</div>
<div>
<h1><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id3" id="mysql" name="mysql">_mysql</a></h1>
<p>If you want to write applications which are portable across
databases,
use <a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysqldb">MySQLdb</a>,
and avoid using this module directly. <tt literal="">_mysql</tt>
provides an interface which mostly implements the MySQL C API. For
more information, see the <a href="http://dev.mysql.com/doc/">MySQL documentation</a>. The
documentation
for this module is intentionally weak because you probably should use
the higher-level MySQLdb module. If you really need it, use the
standard MySQL docs and transliterate as necessary.</p>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id4" id="mysql-c-api-translation" name="mysql-c-api-translation">MySQL C API
translation</a></h2>
<p>The MySQL C API has been wrapped in an object-oriented way. The only
MySQL data structures which are implemented are the <tt literal="">MYSQL</tt>
(database connection handle) and <tt literal="">MYSQL_RES</tt> (result handle)
types. In general, any function which takes <tt literal="">MYSQL *mysql</tt> as an
argument is now a method of the connection object, and any function
which takes <tt literal="">MYSQL_RES
*result</tt> as an argument is a method of the
result object. Functions requiring none of the MySQL data structures
are implemented as functions in the module. Functions requiring one of
the other MySQL data structures are generally not implemented.
Deprecated functions are not implemented. In all cases, the <tt literal="">mysql_</tt>
prefix is dropped from the name. Most of the <tt literal="">conn</tt> methods listed
are also available as MySQLdb Connection object methods. Their use is
non-portable.</p>
</div>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id5" id="mysql-c-api-function-mapping" name="mysql-c-api-function-mapping">MySQL
C API function mapping</a></h2>
<table border="1">
    <colgroup>
    <col width="47%">
    <col width="53%">
    </colgroup>
    <thead valign="bottom">
        <tr>
            <th>C API</th>
            <th><tt literal="">_mysql</tt></th>
        </tr>
    </thead>
    <tbody valign="top">
        <tr>
            <td><tt literal="">mysql_affected_rows()</tt></td>
            <td><tt literal="">conn.affected_rows()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_autocommit()</tt></td>
            <td><tt literal="">conn.autocommit()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_character_set_name()</tt></td>
            <td><tt literal="">conn.character_set_name()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_close()</tt></td>
            <td><tt literal="">conn.close()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_commit()</tt></td>
            <td><tt literal="">conn.commit()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_connect()</tt></td>
            <td><tt literal="">_mysql.connect()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_data_seek()</tt></td>
            <td><tt literal="">result.data_seek()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_debug()</tt></td>
            <td><tt literal="">_mysql.debug()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_dump_debug_info</tt></td>
            <td><tt literal="">conn.dump_debug_info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_escape_string()</tt></td>
            <td><tt literal="">_mysql.escape_string()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_fetch_row()</tt></td>
            <td><tt literal="">result.fetch_row()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_get_character_set_info()</tt></td>
            <td><tt literal="">conn.get_character_set_info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_get_client_info()</tt></td>
            <td><tt literal="">_mysql.get_client_info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_get_host_info()</tt></td>
            <td><tt literal="">conn.get_host_info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_get_proto_info()</tt></td>
            <td><tt literal="">conn.get_proto_info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_get_server_info()</tt></td>
            <td><tt literal="">conn.get_server_info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_info()</tt></td>
            <td><tt literal="">conn.info()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_insert_id()</tt></td>
            <td><tt literal="">conn.insert_id()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_num_fields()</tt></td>
            <td><tt literal="">result.num_fields()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_num_rows()</tt></td>
            <td><tt literal="">result.num_rows()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_options()</tt></td>
            <td>various options to <tt literal="">_mysql.connect()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_ping()</tt></td>
            <td><tt literal="">conn.ping()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_query()</tt></td>
            <td><tt literal="">conn.query()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_real_connect()</tt></td>
            <td><tt literal="">_mysql.connect()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_real_query()</tt></td>
            <td><tt literal="">conn.query()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_real_escape_string()</tt></td>
            <td><tt literal="">conn.escape_string()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_rollback()</tt></td>
            <td><tt literal="">conn.rollback()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_row_seek()</tt></td>
            <td><tt literal="">result.row_seek()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_row_tell()</tt></td>
            <td><tt literal="">result.row_tell()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_select_db()</tt></td>
            <td><tt literal="">conn.select_db()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_set_character_set()</tt></td>
            <td><tt literal="">conn.set_character_set()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_ssl_set()</tt></td>
            <td><tt literal="">ssl</tt>
            option to <tt literal="">_mysql.connect()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_stat()</tt></td>
            <td><tt literal="">conn.stat()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_store_result()</tt></td>
            <td><tt literal="">conn.store_result()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_thread_id()</tt></td>
            <td><tt literal="">conn.thread_id()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_thread_safe_client()</tt></td>
            <td><tt literal="">conn.thread_safe_client()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_use_result()</tt></td>
            <td><tt literal="">conn.use_result()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">mysql_warning_count()</tt></td>
            <td><tt literal="">conn.warning_count()</tt></td>
        </tr>
        <tr>
            <td><tt literal="">CLIENT_*</tt></td>
            <td><tt literal="">MySQLdb.constants.CLIENT.*</tt></td>
        </tr>
        <tr>
            <td><tt literal="">CR_*</tt></td>
            <td><tt literal="">MySQLdb.constants.CR.*</tt></td>
        </tr>
        <tr>
            <td><tt literal="">ER_*</tt></td>
            <td><tt literal="">MySQLdb.constants.ER.*</tt></td>
        </tr>
        <tr>
            <td><tt literal="">FIELD_TYPE_*</tt></td>
            <td><tt literal="">MySQLdb.constants.FIELD_TYPE.*</tt></td>
        </tr>
        <tr>
            <td><tt literal="">FLAG_*</tt></td>
            <td><tt literal="">MySQLdb.constants.FLAG.*</tt></td>
        </tr>
    </tbody>
</table>
</div>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id6" id="some-mysql-examples" name="some-mysql-examples">Some _mysql examples</a></h2>
<p>Okay, so you want to use <tt literal="">_mysql</tt> anyway. Here are some examples.</p>
<p>The simplest possible database connection is:</p>
<pre>import _mysql<br />
db=_mysql.connect()<br />
</pre>
<p>This creates a connection to the MySQL server running on the local
machine using the standard UNIX socket (or named pipe on Windows),
your login name (from the USER environment variable), no password, and
does not <tt literal="">USE</tt> a
database.  Chances are you need to supply more
information.:</p>
<pre>db=_mysql.connect("localhost","joebob","moonpie","thangs")<br />
</pre>
<p>This creates a connection to the MySQL server running on the local
machine via a UNIX socket (or named pipe), the user name "joebob", the
password "moonpie", and selects the initial database "thangs".</p>
<p>We haven't even begun to touch upon all the parameters <tt literal="">connect()</tt>
can take.  For this reason, I prefer to use keyword parameters:</p>
<pre>db=_mysql.connect(host="localhost",user="joebob",<br />
passwd="moonpie",db="thangs")<br />
</pre>
<p>This does exactly what the last example did, but is arguably easier
to
read. But since the default host is "localhost", and if your login
name really was "joebob", you could shorten it to this:</p>
<pre>db=_mysql.connect(passwd="moonpie",db="thangs")<br />
</pre>
<p>UNIX sockets and named pipes don't work over a network, so if you
specify a host other than localhost, TCP will be used, and you can
specify an odd port if you need to (the default port is 3306):</p>
<pre>db=_mysql.connect(host="outhouse",port=3307,passwd="moonpie",db="thangs")<br />
</pre>
<p>If you really had to, you could connect to the local host with TCP by
specifying the full host name, or 127.0.0.1.</p>
<p>Generally speaking, putting passwords in your code is not such a good
idea:</p>
<pre>db=_mysql.connect(host="outhouse",db="thangs",read_default_file="~/.my.cnf")<br />
</pre>
<p>This does what the previous example does, but gets the username and
password and other parameters from ~/.my.cnf (UNIX-like systems). Read
about <a href="http://dev.mysql.com/doc/mysql/en/Option_files.html">option files</a>
for more details.</p>
<p>So now you have an open connection as <tt literal="">db</tt> and want to do a
query. Well, there are no cursors in MySQL, and no parameter
substitution, so you have to pass a complete query string to
<tt literal="">db.query()</tt>:</p>
<pre>db.query("""SELECT spam, eggs, sausage FROM breakfast<br />
WHERE price &lt; 5""")<br />
</pre>
<p>There's no return value from this, but exceptions can be raised. The
exceptions are defined in a separate module, <tt literal="">_mysql_exceptions</tt>,
but <tt literal="">_mysql</tt>
exports them. Read DB API specification <a href="http://www.python.org/peps/pep-0249.html">PEP-249</a> to
find out what they are, or you can use the catch-all <tt literal="">MySQLError</tt>.</p>
<p>At this point your query has been executed and you need to get the
results. You have two options:</p>
<pre>r=db.store_result()<br />
# ...or...<br />
r=db.use_result()<br />
</pre>
<p>Both methods return a result object. What's the difference?
<tt literal="">store_result()</tt>
returns the entire result set to the client
immediately. If your result set is really large, this could be a
problem. One way around this is to add a <tt literal="">LIMIT</tt> clause to your
query, to limit the number of rows returned. The other is to use
<tt literal="">use_result()</tt>,
which keeps the result set in the server and sends
it row-by-row when you fetch. This does, however, tie up server
resources, and it ties up the connection: You cannot do any more
queries until you have fetched <strong>all</strong> the rows. Generally I
recommend using <tt literal="">store_result()</tt>
unless your result set is really
huge and you can't use <tt literal="">LIMIT</tt>
for some reason.</p>
<p>Now, for actually getting real results:</p>
<pre>&gt;&gt;&gt; r.fetch_row()<br />
(('3','2','0'),)<br />
</pre>
<p>This might look a little odd. The first thing you should know is,
<tt literal="">fetch_row()</tt>
takes some additional parameters. The first one is,
how many rows (<tt literal="">maxrows</tt>)
should be returned. By default, it returns
one row. It may return fewer rows than you asked for, but never
more. If you set <tt literal="">maxrows=0</tt>,
it returns all rows of the result
set. If you ever get an empty tuple back, you ran out of rows.</p>
<p>The second parameter (<tt literal="">how</tt>)
tells it how the row should be
represented. By default, it is zero which means, return as a tuple.
<tt literal="">how=1</tt> means,
return it as a dictionary, where the keys are the
column names, or <tt literal="">table.column</tt>
if there are two columns with the
same name (say, from a join). <tt literal="">how=2</tt> means the same as <tt literal="">how=1</tt>
except that the keys are <em>always</em> <tt literal="">table.column</tt>; this is for
compatibility with the old <tt literal="">Mysqldb</tt> module.</p>
<p>OK, so why did we get a 1-tuple with a tuple inside? Because we
implicitly asked for one row, since we didn't specify <tt literal="">maxrows</tt>.</p>
<p>The other oddity is: Assuming these are numeric columns, why are they
returned as strings? Because MySQL returns all data as strings and
expects you to convert it yourself. This would be a real pain in the
ass, but in fact, <tt literal="">_mysql</tt>
can do this for you. (And <tt literal="">MySQLdb</tt>
does do this for you.) To have automatic type conversion done, you
need to create a type converter dictionary, and pass this to
<tt literal="">connect()</tt> as
the <tt literal="">conv</tt>
keyword parameter.</p>
<p>The keys of <tt literal="">conv</tt>
should be MySQL column types, which in the
C API are <tt literal="">FIELD_TYPE_*</tt>.
You can get these values like this:</p>
<pre>from MySQLdb.constants import FIELD_TYPE<br />
</pre>
<p>By default, any column type that can't be found in <tt literal="">conv</tt> is
returned as a string, which works for a lot of stuff. For our
purposes, we probably want this:</p>
<pre>my_conv = { FIELD_TYPE.LONG: int }<br />
</pre>
<p>This means, if it's a <tt literal="">FIELD_TYPE_LONG</tt>,
call the builtin <tt literal="">int()</tt>
function on it.  Note that <tt literal="">FIELD_TYPE_LONG</tt> is an <tt literal="">INTEGER</tt>
column, which corresponds to a C <tt literal="">long</tt>, which is also the type used
for a normal Python integer. But beware: If it's really an <tt literal="">UNSIGNED
INTEGER</tt> column, this could cause
overflows. For this reason,
<tt literal="">MySQLdb</tt>
actually uses <tt literal="">long()</tt>
to do the conversion. But we'll
ignore this potential problem for now.</p>
<p>Then if you use <tt literal="">db=_mysql.connect(conv=my_conv...)</tt>,
the
results will come back <tt literal="">((3,
2, 0),)</tt>, which
is what you would
expect.</p>
</div>
</div>
<div>
<h1><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id7" id="mysqldb" name="mysqldb">MySQLdb</a></h1>
<p>MySQLdb is a thin Python wrapper around <tt literal="">_mysql</tt> which makes it
compatible with the Python DB API interface (version 2).  In reality,
a fair amount of the code which implements the API is in <tt literal="">_mysql</tt>
for the sake of efficiency.</p>
<p>The DB API specification <a href="http://www.python.org/peps/pep-0249.html">PEP-249</a> should be
your primary guide for
using this module. Only deviations from the spec and other
database-dependent things will be documented here.</p>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id8" id="functions-and-attributes" name="functions-and-attributes">Functions
and attributes</a></h2>
<p>Only a few top-level functions and attributes are defined within
MySQLdb.</p>
<dl><dt>connect(parameters...)</dt><dd>
<p>Constructor for creating a connection to the
database. Returns a Connection Object. Parameters are the
same as for the MySQL C API.  In addition, there are a few
additional keywords that correspond to what you would pass
<tt literal="">mysql_options()</tt>
before connecting. Note that some
parameters must be specified as keyword arguments! The
default value for each parameter is NULL or zero, as
appropriate. Consult the MySQL documentation for more
details. The important parameters are:</p>
<dl docutils=""><dt>host</dt><dd>name of host to connect to. Default: use the local host
via a UNIX socket (where applicable)</dd><dt>user</dt><dd>user to authenticate as. Default: current effective user.</dd><dt>passwd</dt><dd>password to authenticate with. Default: no password.</dd><dt>db</dt><dd>database to use. Default: no default database.</dd><dt>port</dt><dd>TCP port of MySQL server. Default: standard port (3306).</dd><dt>unix_socket</dt><dd>location of UNIX socket. Default: use default location or
TCP for remote hosts.</dd><dt>conv</dt><dd>type conversion dictionary.  Default: a copy of
<tt literal="">MySQLdb.converters.conversions</tt></dd><dt>compress</dt><dd>Enable protocol compression. Default: no compression.</dd><dt>connect_timeout</dt><dd>Abort if connect is not completed within
given number of seconds. Default: no timeout (?)</dd><dt>named_pipe</dt><dd>Use a named pipe (Windows). Default: don't.</dd><dt>init_command</dt><dd>Initial command to issue to server upon
connection. Default: Nothing.</dd><dt>read_default_file</dt><dd>MySQL configuration file to read; see
the MySQL documentation for <tt literal="">mysql_options()</tt>.</dd><dt>read_default_group</dt><dd>Default group to read; see the MySQL
documentation for <tt literal="">mysql_options()</tt>.</dd><dt>cursorclass</dt><dd>cursor class that <tt literal="">cursor()</tt>
uses, unless
overridden. Default: <tt literal="">MySQLdb.cursors.Cursor</tt>.
<em>This
must be a keyword parameter.</em></dd><dt>use_unicode</dt><dd>
<p>If True, CHAR and VARCHAR and TEXT columns are
returned as
Unicode strings, using the configured character set. It is
best to set the default encoding in the server
configuration, or client configuration (read with
read_default_file).  If you change the character set after
connecting (MySQL-4.1 and later), you'll need to put the
correct character set name in connection.charset.</p>
<p>If False, text-like columns are returned as normal strings,
but you can always write Unicode strings.</p>
<p><em>This must be a keyword parameter.</em></p>
</dd><dt>charset</dt><dd>
<p>If present, the connection character set will be
changed
to this character set, if they are not equal. Support for
changing the character set requires MySQL-4.1 and later
server; if the server is too old, UnsupportedError will be
raised. This option implies use_unicode=True, but you can
override this with use_unicode=False, though you probably
shouldn't.</p>
<p>If not present, the default character set is used.</p>
<p><em>This must be a keyword parameter.</em></p>
</dd><dt>sql_mode</dt><dd>
<p>If present, the session SQL mode will be set to the
given
string. For more information on sql_mode, see the MySQL
documentation. Only available for 4.1 and newer servers.</p>
<p>If not present, the session SQL mode will be unchanged.</p>
<p><em>This must be a keyword parameter.</em></p>
</dd><dt>ssl</dt><dd>This parameter takes a dictionary or mapping, where the
keys are parameter names used by the <a href="http://dev.mysql.com/doc/mysql/en/mysql_ssl_set.html">mysql_ssl_set</a>
MySQL
C API call. If this is set, it initiates an SSL connection
to the server; if there is no SSL support in the client,
an exception is raised. <em>This must be a keyword
parameter.</em></dd></dl>
</dd></dl>
<dl><dt>apilevel</dt><dd>String constant stating the supported DB API level. '2.0'</dd><dt>threadsafety</dt><dd>
<p>Integer constant stating the level of thread safety
the
interface supports. This is set to 1, which means: Threads may
share the module.</p>
<p>The MySQL protocol can not handle multiple threads using the
same connection at once. Some earlier versions of MySQLdb
utilized locking to achieve a threadsafety of 2. While this is
not terribly hard to accomplish using the standard Cursor class
(which uses <tt literal="">mysql_store_result()</tt>),
it is complicated by
SSCursor (which uses <tt literal="">mysql_use_result()</tt>;
with the latter you
must ensure all the rows have been read before another query can
be executed.  It is further complicated by the addition of
transactions, since transactions start when a cursor execute a
query, but end when <tt literal="">COMMIT</tt>
or <tt literal="">ROLLBACK</tt>
is executed by
the Connection object.  Two threads simply cannot share a
connection while a transaction is in progress, in addition to
not being able to share it during query execution. This
excessively complicated the code to the point where it just
isn't worth it.</p>
<p>The general upshot of this is: Don't share connections between
threads. It's really not worth your effort or mine, and in the
end, will probably hurt performance, since the MySQL server runs
a separate thread for each connection.  You can certainly do
things like cache connections in a pool, and give those
connections to one thread at a time. If you let two threads use
a connection simultaneously, the MySQL client library will
probably upchuck and die.  You have been warned.</p>
<p>For threaded applications, try using a connection pool.
This can be done using the <a href="http://dustman.net/andy/python/Pool">Pool module</a>.</p>
</dd><dt>charset</dt><dd>The character set used by the connection. In MySQL-4.1 and newer,
it is possible (but not recommended) to change the connection's
character set with an SQL statement. If you do this, you'll also
need to change this attribute. Otherwise, you'll get encoding
errors.</dd><dt>paramstyle</dt><dd>
<p>String constant stating the type of parameter
marker formatting
expected by the interface. Set to 'format' = ANSI C printf
format codes, e.g. '...WHERE name=%s'. If a mapping object is
used for conn.execute(), then the interface actually uses
'pyformat' = Python extended format codes, e.g. '...WHERE
name=%(name)s'. However, the API does not presently allow the
specification of more than one style in paramstyle.</p>
<p>Note that any literal percent signs in the query string passed
to execute() must be escaped, i.e. %%.</p>
<p>Parameter placeholders can <strong>only</strong> be used
to insert column
values. They can <strong>not</strong> be used for other parts of SQL,
such as
table names, statements, etc.</p>
</dd><dt>conv</dt><dd>
<p>A dictionary or mapping which controls how types
are converted
from MySQL to Python and vice versa.</p>
<p>If the key is a MySQL type (from <tt literal="">FIELD_TYPE.*</tt>), then the value
can be either:</p>
<ul>
    <li>a callable object which takes a string argument (the MySQL
    value),' returning a Python value</li>
    <li>a sequence of 2-tuples, where the first value is a combination
    of flags from <tt literal="">MySQLdb.constants.FLAG</tt>,
    and the second value
    is a function as above. The sequence is tested until the flags
    on the field match those of the first value. If both values
    are None, then the default conversion is done. Presently this
    is only used to distinquish TEXT and BLOB columns.</li>
</ul>
<p>If the key is a Python type or class, then the value is a
callable Python object (usually a function) taking two arguments
(value to convert, and the conversion dictionary) which converts
values of this type to a SQL literal string value.</p>
<p>This is initialized with reasonable defaults for most
types. When creating a Connection object, you can pass your own
type converter dictionary as a keyword parameter. Otherwise, it
uses a copy of <tt literal="">MySQLdb.converters.conversions</tt>.
Several
non-standard types are returned as strings, which is how MySQL
returns all columns. For more details, see the built-in module
documentation.</p>
</dd></dl>
</div>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id9" id="connection-objects" name="connection-objects">Connection Objects</a></h2>
<p>Connection objects are returned by the <tt literal="">connect()</tt> function.</p>
<dl><dt>commit()</dt><dd>If the database and the tables support transactions, this
commits the current transaction; otherwise this method
successfully does nothing.</dd><dt>rollback()</dt><dd>If the database and tables support transactions, this rolls back
(cancels) the current transaction; otherwise a
<tt literal="">NotSupportedError</tt>
is raised.</dd><dt>cursor([cursorclass])</dt><dd>MySQL does not support cursors; however, cursors are easily
emulated.  You can supply an alternative cursor class as an
optional parameter.  If this is not present, it defaults to the
value given when creating the connection object, or the standard
<tt literal="">Cursor</tt> class.
Also see the additional supplied cursor
classes in the usage section.</dd></dl>
<p>There are many more methods defined on the connection object which
are MySQL-specific. For more information on them, consult the internal
documentation using <tt literal="">pydoc</tt>.</p>
</div>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id10" id="cursor-objects" name="cursor-objects">Cursor Objects</a></h2>
<dl><dt>callproc(procname, args)</dt><dd>
<p>Calls stored procedure procname with the sequence
of arguments
in args. Returns the original arguments. Stored procedure
support only works with MySQL-5.0 and newer.</p>
<p><strong>Compatibility note:</strong> <a href="http://www.python.org/peps/pep-0249.html">PEP-249</a> specifies
that if there are
OUT or INOUT parameters, the modified values are to be
returned. This is not consistently possible with MySQL. Stored
procedure arguments must be passed as server variables, and
can only be returned with a SELECT statement. Since a stored
procedure may return zero or more result sets, it is impossible
for MySQLdb to determine if there are result sets to fetch
before the modified parmeters are accessible.</p>
<p>The parameters are stored in the server as @_*procname*_*n*,
where <em>n</em> is the position of the parameter. I.e., if you
cursor.callproc('foo', (a, b, c)), the parameters will be
accessible by a SELECT statement as @_foo_0, @_foo_1, and
@_foo_2.</p>
<p><strong>Compatibility note:</strong> It appears that the
mere act of
executing the CALL statement produces an empty result set, which
appears after any result sets which might be generated by the
stored procedure. Thus, you will always need to use nextset() to
advance result sets.</p>
</dd><dt>close()</dt><dd>Closes the cursor. Future operations raise <tt literal="">ProgrammingError</tt>.
If you are using server-side cursors, it is very important to
close the cursor when you are done with it and before creating a
new one.</dd><dt>info()</dt><dd>Returns some information about the last query. Normally
you don't need to check this. If there are any MySQL
warnings, it will cause a Warning to be issued through
the Python warning module. By default, Warning causes a
message to appear on the console. However, it is possible
to filter these out or cause Warning to be raised as exception.
See the MySQL docs for <tt literal="">mysql_info()</tt>,
and the Python warning
module. (Non-standard)</dd><dt>setinputsizes()</dt><dd>Does nothing, successfully.</dd><dt>setoutputsizes()</dt><dd>Does nothing, successfully.</dd><dt>nextset()</dt><dd>
<p>Advances the cursor to the next result set,
discarding the remaining
rows in the current result set. If there are no additional result
sets, it returns None; otherwise it returns a true value.</p>
<p>Note that MySQL doesn't support multiple result sets
until 4.1.</p>
</dd></dl>
</div>
<div>
<h2><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id11" id="some-examples" name="some-examples">Some examples</a></h2>
<p>The <tt literal="">connect()</tt>
method works nearly the same as with <a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysql">_mysql</a>:</p>
<pre>import MySQLdb<br />
db=MySQLdb.connect(passwd="moonpie",db="thangs")<br />
</pre>
<p>To perform a query, you first need a cursor, and then you can execute
queries on it:</p>
<pre>c=db.cursor()<br />
max_price=5<br />
c.execute("""SELECT spam, eggs, sausage FROM breakfast<br />
WHERE price &lt; %s""", (max_price,))<br />
</pre>
<p>In this example, <tt literal="">max_price=5</tt>
Why, then, use <tt literal="">%s</tt>
in the
string? Because MySQLdb will convert it to a SQL literal value, which
is the string '5'. When it's finished, the query will actually say,
"...WHERE price &lt; 5".</p>
<p>Why the tuple? Because the DB API requires you to pass in any
parameters as a sequence. Due to the design of the parser, (max_price)
is interpreted as using algebraic grouping and simply as max_price and
not a tuple. Adding a comma, i.e. (max_price,) forces it to make a
tuple.</p>
<p>And now, the results:</p>
<pre>&gt;&gt;&gt; c.fetchone()<br />
(3L, 2L, 0L)<br />
</pre>
<p>Quite unlike the <tt literal="">_mysql</tt>
example, this returns a single tuple,
which is the row, and the values are properly converted by default...
except... What's with the L's?</p>
<p>As mentioned earlier, while MySQL's INTEGER column translates
perfectly into a Python integer, UNSIGNED INTEGER could overflow, so
these values are converted to Python long integers instead.</p>
<p>If you wanted more rows, you could use <tt literal="">c.fetchmany(n)</tt> or
<tt literal="">c.fetchall()</tt>.
These do exactly what you think they do. On
<tt literal="">c.fetchmany(n)</tt>,
the <tt literal="">n</tt> is
optional and defaults to
<tt literal="">c.arraysize</tt>,
which is normally 1. Both of these methods return a
sequence of rows, or an empty sequence if there are no more rows.  If
you use a weird cursor class, the rows themselves might not be tuples.</p>
<p>Note that in contrast to the above, <tt literal="">c.fetchone()</tt> returns <tt literal="">None</tt>
when there are no more rows to fetch.</p>
<p>The only other method you are very likely to use is when you have to
do a multi-row insert:</p>
<pre>c.executemany(<br />
"""INSERT INTO breakfast (name, spam, eggs, sausage, price)<br />
VALUES (%s, %s, %s, %s, %s)""",<br />
[<br />
("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),<br />
("Not So Much Spam Plate", 3, 2, 0, 3.95 ),<br />
("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )<br />
] )<br />
</pre>
<p>Here we are inserting three rows of five values. Notice that there is
a mix of types (strings, ints, floats) though we still only use
<tt literal="">%s</tt>. And also
note that we only included format strings for one
row. MySQLdb picks those out and duplicates them for each row.</p>
</div>
</div>
<div>
<h1><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id12" id="using-and-extending" name="using-and-extending">Using and extending</a></h1>
<p>In general, it is probably wise to not directly interact with the DB
API except for small applicatons. Databases, even SQL databases, vary
widely in capabilities and may have non-standard features. The DB API
does a good job of providing a reasonably portable interface but some
methods are non-portable. Specifically, the parameters accepted by
<tt literal="">connect()</tt> are
completely implementation-dependent.</p>
<p>If you believe your application may need to run on several different
databases, the author recommends the following approach, based on
personal experience: Write a simplified API for your application which
implements the specific queries and operations your application needs
to perform. Implement this API as a base class which should be have
few database dependencies, and then derive a subclass from this which
implements the necessary dependencies. In this way, porting your
application to a new database should be a relatively simple matter of
creating a new subclass, assuming the new database is reasonably
standard.</p>
<p>Because MySQLdb's Connection and Cursor objects are written in
Python,
you can easily derive your own subclasses. There are several Cursor
classes in MySQLdb.cursors:</p>
<dl><dt>BaseCursor</dt><dd>The base class for Cursor objects.  This does not raise Warnings.</dd><dt>CursorStoreResultMixIn</dt><dd>Causes the Cursor to use the <tt literal="">mysql_store_result()</tt> function to
get the query result. The entire result set is stored on the
client side.</dd><dt>CursorUseResultMixIn</dt><dd>Causes the cursor to use the <tt literal="">mysql_use_result()</tt> function to
get the query result. The result set is stored on the server side
and is transferred row by row using fetch operations.</dd><dt>CursorTupleRowsMixIn</dt><dd>Causes the cursor to return rows as a tuple of the column values.</dd></dl>
<p>CursorDictRowsMixIn</p>
<blockquote>
Causes the cursor to return rows as a dictionary, where the keys
are column names and the values are column values. Note that if
the column names are not unique, i.e., you are selecting from two
tables that share column names, some of them will be rewritten as
<tt literal="">table.column</tt>.
This can be avoided by using the SQL <tt literal="">AS</tt>
keyword. (This is yet-another reason not to use <tt literal="">*</tt> in SQL
queries, particularly where <tt literal="">JOIN</tt> is involved.)</blockquote>
<dl><dt>Cursor</dt><dd>The default cursor class. This class is composed of
<tt literal="">CursorWarningMixIn</tt>,
<tt literal="">CursorStoreResultMixIn</tt>,
<tt literal="">CursorTupleRowsMixIn,</tt>
and <tt literal="">BaseCursor</tt>,
i.e. it raises
<tt literal="">Warning</tt>, uses
<tt literal="">mysql_store_result()</tt>,
and returns rows as
tuples.</dd><dt>DictCursor</dt><dd>Like <tt literal="">Cursor</tt>
except it returns rows as dictionaries.</dd><dt>SSCursor</dt><dd>A "server-side" cursor. Like <tt literal="">Cursor</tt> but uses
<tt literal="">CursorUseResultMixIn</tt>.
Use only if you are dealing with
potentially large result sets.</dd><dt>SSDictCursor</dt><dd>Like <tt literal="">SSCursor</tt>
except it returns rows as dictionaries.</dd></dl>
</div>
<h1><a href="http://mysql-python.sourceforge.net/MySQLdb.html#id13" id="embedded-server" name="embedded-server">Embedded Server</a></h1>
<p>Instead of connecting to a stand-alone server over the network,
the embedded server support lets you run a full server right in
your Python code or application server.</p>
<p>If you have built MySQLdb with embedded server support, there
are two additional functions you will need to make use of:</p>
<blockquote>
<dl><dt>server_init(args, groups)</dt><dd>
<p>Initialize embedded server. If this client is not
linked against
the embedded server library, this function does nothing.</p>
<dl docutils=""><dt>args</dt><dd>sequence of command-line arguments</dd><dt>groups</dt><dd>sequence of groups to use in defaults files</dd></dl>
</dd><dt>server_end()</dt><dd>Shut down embedded server. If not using an embedded server, this
does nothing.</dd></dl>
</blockquote>
<p>See the MySQL documentation for more information on the embedded
server.</p>
<table field-list="" frame="void" rules="none">
    <col>
    <col>
    <tbody valign="top">
        <tr>
            <th>Title:</th>
            <td>MySQLdb: a Python interface for MySQL</td>
        </tr>
        <tr>
            <th>Author:</th>
            <td>Andy Dustman</td>
        </tr>
        <tr>
            <th>Version:</th>
            <td>$Revision: 421 $</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/sealyu/aggbug/321217.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2010-05-17 23:56 <a href="http://www.blogjava.net/sealyu/archive/2010/05/17/321217.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>简明 Python  编程规范（转）</title><link>http://www.blogjava.net/sealyu/archive/2010/05/14/320937.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 13 May 2010 18:59:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2010/05/14/320937.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/320937.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2010/05/14/320937.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/320937.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/320937.html</trackback:ping><description><![CDATA[本文最初发表于赖勇浩（恋花蝶）的博客：<a href="http://blog.csdn.net/lanphaday"><font color="#006bad">http://blog.csdn.net/lanphaday</font></a>，如蒙<a href="javascript:;" onclick="javascript:tagshow(event, '%D7%AA%D4%D8');" target="_self"><u><strong>转载</strong></u></a>，必须保留全文完整，未经本人同意，不得用于商业目的。<br />
<br />
<font style="font-size: 22pt;" face="黑体">简明 <a href="javascript:;" onclick="javascript:tagshow(event, 'Python');" target="_self"><u><strong>Python</strong></u></a>
编程规范</font><span style="display: none;">51Testing软件测试网!L2kA
c(M}&amp;k("</span><br />
<br />
<font style="font-size: 16pt;" face="宋体"><strong>编
码</strong></font><span style="display: none;">51Testing软件测试网3EFD`G
Xm</span><br />
<br />
&nbsp;&nbsp;&nbsp; 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*-
。设置编辑器，默认保存为 utf-8 格式。<span style="display: none;">51Testing软件测试网3}f
n9u5@h'uC9?~</span><br />
<br />
<font style="font-size: 16pt;" face="宋体"><strong>注
释</strong></font><span style="display: none;">51Testing软件测试网K4@�ICJ
{)b"k"</span><br />
<br />
&nbsp;&nbsp;&nbsp; 业界普遍认同 Python 的注释分为两种的概念，一种是由 # 开头的&#8220;真正的&#8221;注释，另一种是
docstrings。前者表明为何选择当前实现以及这种实现的原理和难点，后者表明如何使用这个包、模块、类、函数（方法），甚至包括使用示例和<a href="javascript:;" onclick="javascript:tagshow(event,
'%B5%A5%D4%AA%B2%E2%CA%D4');" target="_self"><u><strong>单元测试</strong></u></a>。<br />
&nbsp;&nbsp;&nbsp;
坚持适当注释原则。对不存在技术难点的代码坚持不注释，对存在技术难点的代码必须注释。但与注释不同，推荐对每一个包、模块、类、函数（方法）写
docstrings，除非代码一目了然，非常简单。<span style="display: none;">51Testing软件测试网
'Y:w2S%ao(_)Lk</span><br />
<br />
<font style="font-size: 16pt;" face="宋体"><strong>缩
进</strong></font><span style="display: none;">51Testing软件测试网J�g} G-`5U
Gs'Hq</span><br />
<br />
&nbsp;&nbsp;&nbsp; Python 依赖缩进来确定代码块的层次，行首空白符主要有两种：tab
和空格，但严禁两者混用。如果使用 tab 缩进，设定 tab 为 4 个空格。<br />
&nbsp;&nbsp;&nbsp;
公司内部推荐使用 4 个空格的 tab 进行缩进。<span style="display: none;">51Testing软件测试网-j
a
`+z~r.oQ
Zs</span><br />
<font style="font-size: 16pt;" face="宋体"><strong>空格</strong></font><br />
<br />
&nbsp;&nbsp;&nbsp; 空格在 Python 代码中是有意义的，因为
Python 的语法依赖于缩进，在行首的空格称为前导空格。在这一节不讨论前导空格相关的内容，只讨论非前导空格。非前导空格在 Python
代码中没有意义，但适当地加入非前导空格可以增进代码的可读性。<br />
<span style="display: none;">
MmOx
nYM0</span>1）&nbsp;在二元算术、逻辑运算符前后加空格：如 a = b + c；<br />
2）&nbsp;在一元前缀运算符后不加空格，如 if !flg:
pass；<br />
3）&nbsp;&#8220;:&#8221;
用在行尾时前后皆不加空格，如分枝、循环、函数和类定义语言；用在非行尾时两端加空格，如 dict 对象的定义 d = {&#8216;key&#8217; :
&#8217;value&#8217;}。<br />
4）&nbsp;括
号（含圆括号、方括号和花括号）前后不加空格，如 do_something(arg1, arg2)，而不是 do_something( arg1,
arg2 )；<br />
5）&nbsp;逗号后面
加一个空格，前面不加空格；s<br />
<br />
<span style="display: none;">E'M/z
Yv"v
lh0</span><font style="font-size: 16pt;" face="宋体"><strong>空行</strong></font><br />
<br />
&nbsp;&nbsp;&nbsp;
适当的空行有利于增加代码的可读性，加空行可以参考如下几个准则：<br />
1）&nbsp;在
类、函数的定义间加空行；<br />
2）&nbsp;在
import 不同种类的模块间加工行；<span style="display: none;">51Testing软件测试网L7[2e
N+S
rm</span><br />
3）&nbsp;在函数中的逻辑段落间加空行，即把相关的代码紧凑写在一起，作为一个逻辑段落，段落间以空行分隔；<br />
<br />
<font style="font-size: 16pt;" face="宋体"><strong>断行</strong></font><br />
&nbsp; <br />
&nbsp; 尽管现在的宽屏显示器已经可以单屏显示超过 256
列字符，但本规范仍然坚持行的最大长度不得超过 78 个字符的标准。折叠长行的方法有以下几种方法：<br />
<span style="display: none;">f�k
c&amp;nJ4nR0</span>1)&nbsp;为长变量名换一个短名，如：<br />
&nbsp;&nbsp;&nbsp;
this.is.a.very.long.variable_name = this.is.another.long.variable_name<br />
&nbsp;&nbsp;&nbsp;
应改为：<br />
&nbsp;&nbsp;&nbsp;
variable_name1 = this.is.a.very.long.variable_name<span style="display: none;">51Testing软件测试网M	a
cb5"8`1X{</span><br />
&nbsp;&nbsp;&nbsp; variable_name2 = this.is.another.variable_name<br />
<span style="display: none;">,_JE9@
p0</span>&nbsp;&nbsp;&nbsp; variable_name1 = variable_name2s<br />
2)&nbsp;在括号（包括圆括号、方括号和花括号）内换行，如：<span style="display: none;">51Testing软件测试网+y?"Mqe:X$Ylr
Q*S</span><br />
&nbsp;&nbsp;&nbsp; class Edit(CBase):<br />
<span style="display: none;">U"2|u!?
C�Y0</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def __init__(self, parent, width,&nbsp;<span style="display: none;">51Testing软件测试网m-dN,u
b fa(I
y</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; font = FONT, color = BLACK,
pos = POS, style = 0):<br />
<span style="display: none;">QK/IpVh&amp;HB!c:f
r0</span>&nbsp;&nbsp;&nbsp; 或：<br />
<span style="display: none;"> x"B}Qm
hf!E0</span>&nbsp;&nbsp;&nbsp; very_very_very_long_variable_name = Edit(parent, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
width, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
font, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
color, "<br />
<span style="display: none;">2r8o,V
O#}`.GS�S/B0</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pos)<br />
&nbsp;&nbsp;&nbsp;
如果行长到连第一个括号内的参数都放不下，则每个元素都单独占一行：<span style="display: none;">51Testing软件
测试网,kTP"D/ux4af</span><br />
&nbsp;&nbsp;&nbsp; very_very_very_long_variable_name =
ui.widgets.Edit( "<span style="display: none;">51Testing软件测试网g5lj"I.
{6~z1t</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
panrent, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
width, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
font, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
color, "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pos)<br />
3)&nbsp;在长行加入续行符强行断
行，断行的位置应在操作符前，且换行后多一个缩进，以使维护人员看代码的时候看到代码行首即可判定这里存在换行，如：<br />
<span style="display: none;">8_
V,w)j-mn@-j0</span>if color == WHITE or color == BLACK "<br />
or color == BLUE: # 注意 or
操作符在新行的行首而不是旧行的行尾<br />
do_something(color);<br />
<font face="宋体"><strong style="font-size: 16pt;"><br />
命名</strong></font><span style="display: none;">51Testing
软件测试网/"Xq5|E%Kn</span><br />
<br />
&nbsp;&nbsp;&nbsp;
一致的命名可以给开发人员减少许多麻烦，而恰如其分的命名则可以大幅提高代码的可读性，降低维护成本。<br />
<font style="font-size: 16pt;" face="宋体"><br />
常量</font><br />
<br />
&nbsp;常
量名所有字母大写，由下划线连接各个单词，如<span style="display: none;">51Testing软件测试网9Nh#f
y+{'X^1W</span><br />
WHITE = 0XFFFFFF<span style="display: none;">51Testing
软件测试网EiS!|E"i</span><br />
THIS_IS_A_CONSTANT = 1<br />
<font style="font-size: 16pt;" face="宋体"><br />
变量</font><span style="display: none;">51Testing
软件测试网I	Vk6w,|</span><br />
<br />
&nbsp;&nbsp;&nbsp; 变量名全部小写，由下划线连接各个单词，如<br />
color = WHITE<span style="display: none;">51Testing软件测试网*v-]1m,g"js
[Xu'@</span><br />
this_is_a_variable = 1<br />
<span style="display: none;">R$K
so
M~D.C0</span>&nbsp;&nbsp;&nbsp; 不论是类成员变量还是全局变量，均不使用 m 或 g
前缀。私有类成员使用单一下划线前缀标识，多定义公开成员，少定义私有成员。<span style="display: none;">51Testing
软件测试网tlFe.v;Ym!Wg?</span><br />
&nbsp;&nbsp;&nbsp; 变量名不应带有类型信息，因为 Python 是动态类型语言。如
iValue、names_list、dict_obj 等都是不好的命名。<span style="display: none;">51Testing
软件测试网N"z:p2};J&amp;`</span><br />
<font style="font-size: 16pt;" face="宋体"><br />
函
数</font><br />
<span style="display: none;">
Cloqn0</span><br />
&nbsp;&nbsp;&nbsp; 函数名的命名规则与变量名相同。<br />
<span style="display: none;">
U4lu b"0</span><font face="宋体">类</font><br />
<span style="display: none;">-s2w9yym;{
P0</span>&nbsp;&nbsp;&nbsp; 类名单词首字母大写，不使用下划线连接单词，也不加入 C、T 等前缀。如：<br />
class ThisIsAClass(object):<br />
&nbsp;&nbsp;&nbsp;
passs<br />
<font face="宋体">模块</font><span style="display: none;">51Testing软件测试网GK!P
}:D"x</span><br />
&nbsp;&nbsp;&nbsp; 模块名全部小写，对于包内使用的模块，可以加一个下划线前缀，如<br />
module.py<br />
_internal_module.py<br />
<font style="font-size: 16pt;" face="宋体"><br />
包</font><span style="display: none;">51Testing软件测试网5zK
b.S
})W#H
M+_</span><br />
<br />
&nbsp;&nbsp;&nbsp; 包的命名规范与模块相同。<br />
<font style="font-size: 14pt;" face="宋体"><br />
缩写</font><br />
<br />
&nbsp;&nbsp;&nbsp; 命名应当尽量使用全拼写的单词，缩写的情况有如下两种：<br />
1）&nbsp;常用的缩写，如
XML、ID等，在命名时也应只大写首字母，如<br />
class
XmlParser(object):pass<span style="display: none;">51Testing软件测试网8xYp
qM5?</span><br />
2）&nbsp;命名中含有长单词，对某个单词进行缩写。这时应使用约定成俗的缩写方式，如去除元音、包含辅音的首字符等方式，
例如：<br />
<span style="display: none;">2E@E+y
BD}p,d0</span>function 缩写为 fn<span style="display: none;">51Testing软件
测试网e9F+^z{q7T	A</span><br />
text 缩写为 txt<span style="display: none;">51Testing
软件测试网A'U~OwjE*i</span><br />
object 缩写为 obj<br />
count 缩写为 cnt<br />
number 缩写为 num，等。<br />
<font face="宋体">特定命名方
式</font><br />
&nbsp;&nbsp;&nbsp;
主要是指 __xxx__
形式的系统保留字命名法。项目中也可以使用这种命名，它的意义在于这种形式的变量是只读的，这种形式的类成员函数尽量不要重载。如<br />
class
Base(object):<br />
&nbsp;&nbsp;&nbsp;
def __init__(self, id, parent = None):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.__id__ = id<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.__parent__ = parent<span style="display: none;">51Testing软件测试网
3v3M�km#]L]*Lm2D</span><br />
&nbsp;&nbsp;&nbsp; def __message__(self, msgid):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #
&#8230;略<br />
其中
__id__、__parent__ 和 __message__ 都采用了系统保留字命名法。<span style="display: none;">51Testing软件测试网2V
D_ nf�X"</span><br />
<font style="font-size: 16pt;" face="宋体"><strong><br />
语
句</strong></font><span style="display: none;">51Testing软件测试网&amp;v`�Y
z7za o</span><br />
<font style="font-size: 14pt;" face="宋体"><br />
import</font><br />
&nbsp;&nbsp;&nbsp; import
语句有以下几个原则需要遵守：<span style="display: none;">51Testing软件测试网N}D~@E
J0F:}v.i</span><br />
1）&nbsp;import 的次序，先 import Python 内置模块，再 import 第三方模块，最后
import 自己开发的项目中的<a href="javascript:;" onclick="javascript:tagshow(event, '%C6%E4%CB%FC');" target="_self"><u><strong>其
它</strong></u></a>模块；这几种模块中用空行分隔开来。<span style="display: none;">51Testing
软件测试网r"l
~J3E%B6BVH</span><br />
2）&nbsp;一条 import 语句 import 一个模块。<br />
3）&nbsp;当从模块中 import
多个对象且超过一行时，使用如下断行法（此语法 py2.5 以上版本才支持）：<span style="display: none;">51Testing
软件测试网)^he$i+RMV'B</span><br />
from module import (obj1, obj2, obj3,
obj4,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
obj5, obj6)<br />
4）&nbsp;不
要使用 from module import *，除非是 import 常量定义模块或其它你确保不会出现命名空间冲突的模块。<br />
<span style="display: none;">";wf4a4D
WaS$c@~0</span><font face="宋体">赋值</font><br />
&nbsp;&nbsp;&nbsp; 对于赋值语言，主要是不要做无谓的对齐，如：<br />
<span style="display: none;">
Y0t&amp;V6U(Qxx�vHQv1c0</span>a&nbsp;&nbsp;&nbsp;= 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 这是一个行注释<br />
variable&nbsp;&nbsp;=
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 另一个行注释<br />
fn&nbsp;&nbsp;&nbsp;=
callback_function&nbsp;&nbsp;# 还是行注释<span style="display: none;">51Testing软件测试网+l
q rq
Z-n&amp;_-`*[l	P5[</span><br />
没有必要做这种对齐，原因有两点：一是这种对齐会打乱编程时的注意力，大脑要同时处理两件
事（编程和对齐）；二是以后阅读和维护都很困难，因为人眼的横向视野很窄，把三个字段看成一行很困难，而且维护时要增加一个更长的变量名也会破坏对齐。直
接这样写为佳：<br />
a
= 1 # 这是一个行注释<br />
variable
= 2 # 另一个行注释<br />
fn =
callback_function # 还是行注释<br />
<span style="display: none;">6X!_I%J+L
?K0</span><font face="宋体">分枝和循环</font><br />
&nbsp;&nbsp;&nbsp;
对于分枝和循环，有如下几点需要注意的：<br />
1)&nbsp;不
要写成一行，如：<br />
if
!flg: pass 和 for i in xrange(10): print i都不是好代码，应写成<br />
if !flg:<br />
&nbsp;&nbsp;&nbsp; pass<br />
for i
in xrange(10):<br />
&nbsp;&nbsp;&nbsp;
print i<br />
注：
本文档中出现写成一行的例子是因为排版的原因，不得作为编码中不断行的依据。<br />
2)&nbsp;条
件表达式的编写应该足够 pythonic，如以下形式的条件表达式是拙劣的：<span style="display: none;">51Testing
软件测试网,F!EH
~Wr]</span><br />
if len(alist) != 0: do_something()<br />
if alist !=
[]: do_something()<br />
if
s != &#8220;&#8221;: do_something()<br />
<span style="display: none;">8z-Y[
F2F0</span>if var != None: do_something()<br />
if var != False: do_something()<br />
上面的语句应该写成：<br />
if seq:
do_somethin() # 注意，这里命名也更改了<br />
if
var: do_something()<br />
3)&nbsp;用
得着的时候多使用循环语句的 else 分句，以简化代码。<span style="display: none;">51Testing软件测试网
q	_?}6o4t-S2sZ</span><br />
<font face="宋体"><strong style="font-size: 16pt;"><br />
已有代码</strong></font><span style="display: none;">51Testing软件测试网
O8|c
px*N2evNKD</span><br />
<br />
&nbsp;&nbsp;&nbsp;
对于项目中已有的代码，可能因为历史遗留原因不符合本规范，应当看作可以容忍的特例，允许存在；但不应在新的代码中延续旧的风格。<br />
&nbsp;&nbsp;&nbsp;
对于第三方模块，可能不符合本规范，也应看作可以容忍的特例，允许存在；但不应在新的代码中使用第三方模块的风格。<br />
&nbsp;&nbsp;&nbsp;
tab 与空格混用的缩进是不可容忍的，在运行项目时应使用 &#8211;t 或 &#8211;tt
选项排查这种可能性存在。出现混用的情况时，如果是公司开发的基础类库代码，应当通知类库维护人员修改；第三方模块则可以通过提交 patch
等方式敦促开发者修正问题。<br />
<font face="宋体"><strong style="font-size: 16pt;"><br />
已有风格</strong></font><br />
<br />
&nbsp;&nbsp;&nbsp;
开发人员往往在加入项目之前已经形成自有的编码风格，加入项目后应以本规范为准编写代码。特别是匈牙利命名法，因为带有类型信息，并不适合 Python
编程，不应在 Python 项目中应用。
<img src ="http://www.blogjava.net/sealyu/aggbug/320937.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2010-05-14 02:59 <a href="http://www.blogjava.net/sealyu/archive/2010/05/14/320937.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django Google Maps form widget(转)</title><link>http://www.blogjava.net/sealyu/archive/2010/05/11/320542.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 10 May 2010 19:42:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2010/05/11/320542.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/320542.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2010/05/11/320542.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/320542.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/320542.html</trackback:ping><description><![CDATA[<p>We&#8217;re building a Django application at work, and I had to implement a
way for a user to select a location on a map, in order to be able to
save the exact coordinates and the zoom level the user chose.<br />
I looked around to see if something similar had been developed, but I
did not find anything so simple and useful as what I later developed, so
for the sake of open source-ness I&#8217;m sharing it here with you.<br />
It is implemented as a standard Django form widget so just do as any
other Django widget, for example:</p>
<div>
<div>
<pre style="font-family: monospace;"><span style="color: #ff7700; font-weight: bold;">class</span> PersonForm<span style="color: black;">(</span>forms.<span style="color: black;">Form</span><span style="color: black;">)</span>:<br />
name = forms.<span style="color: black;">CharField</span><span style="color: black;">(</span><span style="color: black;">)</span><br />
website = forms.<span style="color: black;">URLField</span><span style="color: black;">(</span><span style="color: black;">)</span><br />
location = forms.<span style="color: black;">CharField</span><span style="color: black;">(</span>widget=GMapInput<span style="color: black;">(</span>attrs=<span style="color: black;">{</span><span style="color: #483d8b;">'width'</span>:<span style="color: #483d8b;">'600'</span>, <span style="color: #483d8b;">'height'</span>:<span style="color: #483d8b;">'400'</span><span style="color: black;">}</span><span style="color: black;">)</span><span style="color: black;">)</span></pre>
</div>
</div>
<p>Here&#8217;s the code for the GMapInput class (sorry for the docs in
Spanish, but I&#8217;m in Mexico):</p>
<div>
<div>
<pre style="font-family: monospace;"><span style="color: #ff7700; font-weight: bold;">from</span> django.<span style="color: black;">forms</span>.<span style="color: black;">widgets</span> <span style="color: #ff7700; font-weight: bold;">import</span> Input<br />
<span style="color: #ff7700; font-weight: bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">safestring</span> <span style="color: #ff7700; font-weight: bold;">import</span> mark_safe<br />
&nbsp;<br />
<span style="color: #ff7700; font-weight: bold;">class</span> GMapInput<span style="color: black;">(</span>Input<span style="color: black;">)</span>:<br />
<span style="color: #483d8b;">"""<br />
Widget para seleccionar un punto en un mapa de Google<br />
"""</span><br />
&nbsp;<br />
<span style="color: #ff7700; font-weight: bold;">def</span> render<span style="color: black;">(</span><span style="color: #008000;">self</span>, name, value, attrs=<span style="color: #008000;">None</span><span style="color: black;">)</span>:<br />
<span style="color: #483d8b;">"""<br />
Atributos extras:<br />
- width: ancho del mapa en pixeles<br />
- height: alto del mapa en pixeles<br />
- center: latitud,longitud del punto central del mapa<br />
- zoom: zoom inicial del mapa, 1 - 17<br />
"""</span><br />
&nbsp;<br />
final_attrs = <span style="color: #008000;">self</span>.<span style="color: black;">build_attrs</span><span style="color: black;">(</span>attrs<span style="color: black;">)</span><br />
width = final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'width'</span><span style="color: black;">]</span> <span style="color: #ff7700; font-weight: bold;">if</span> <span style="color: #483d8b;">'width'</span> <span style="color: #ff7700; font-weight: bold;">in</span> final_attrs <span style="color: #ff7700; font-weight: bold;">else</span> <span style="color: #483d8b;">'500'</span><br />
height = final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'height'</span><span style="color: black;">]</span> <span style="color: #ff7700; font-weight: bold;">if</span> <span style="color: #483d8b;">'height'</span> <span style="color: #ff7700; font-weight: bold;">in</span> final_attrs <span style="color: #ff7700; font-weight: bold;">else</span> <span style="color: #483d8b;">'300'</span><br />
center = final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'center'</span><span style="color: black;">]</span> <span style="color: #ff7700; font-weight: bold;">if</span> <span style="color: #483d8b;">'center'</span> <span style="color: #ff7700; font-weight: bold;">in</span> final_attrs <span style="color: #ff7700; font-weight: bold;">else</span> <span style="color: #483d8b;">'21.983801,-100.964355'</span> <span style="color: #808080; font-style: italic;"># Centro de M&#233;xico</span><br />
zoom = final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'zoom'</span><span style="color: black;">]</span> <span style="color: #ff7700; font-weight: bold;">if</span> <span style="color: #483d8b;">'zoom'</span> <span style="color: #ff7700; font-weight: bold;">in</span> final_attrs <span style="color: #ff7700; font-weight: bold;">else</span> <span style="color: #483d8b;">'4'</span> <span style="color: #808080; font-style: italic;"># Zoom amplio, se ve todo un pa&#237;s</span><br />
&nbsp;<br />
widget = u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'&lt;div style="margin-left:7em; padding-left:30px;"&gt;<br />
&lt;input type="hidden" value="%(value)s" name="%(name)s" id="%(id)s" /&gt;<br />
&lt;div id="%(id)s_map" style="width: %(width)spx; height: %(height)spx"&gt;&lt;/div&gt;&lt;/div&gt;<br />
&lt;script type="text/javascript"&gt;<br />
var %(id)s_map = new GMap2(document.getElementById("%(id)s_map"));<br />
%(id)s_map.addControl(new GLargeMapControl3D());<br />
&nbsp;<br />
var %(id)s_marker;<br />
function %(id)s_updateField() {<br />
document.getElementById("%(id)s").value = %(id)s_marker.getLatLng().toUrlValue() + "|" + %(id)s_map.getZoom();<br />
%(id)s_map.panTo(%(id)s_marker.getLatLng(), true);<br />
}<br />
'</span><span style="color: #483d8b;">''</span> <span style="color: #66cc66;">%</span> <span style="color: black;">{</span> <span style="color: #483d8b;">'value'</span>: value, <span style="color: #483d8b;">'name'</span>: name, <span style="color: #483d8b;">'id'</span>: final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'id'</span><span style="color: black;">]</span>, <span style="color: #483d8b;">'width'</span>: width, <span style="color: #483d8b;">'height'</span>: height <span style="color: black;">}</span><br />
&nbsp;<br />
<span style="color: #ff7700; font-weight: bold;">if</span> value <span style="color: #ff7700; font-weight: bold;">is</span> <span style="color: #008000;">None</span> <span style="color: #ff7700; font-weight: bold;">or</span> value == <span style="color: #483d8b;">''</span>:<br />
widget = widget + u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'<br />
%(id)s_map.setCenter(new GLatLng(%(center)s), %(zoom)s);<br />
var %(id)s_clickListener = GEvent.addListener(%(id)s_map, "click", function(overlay, latlng) {<br />
if(latlng) {<br />
%(id)s_marker = new GMarker(latlng, {draggable: true});<br />
%(id)s_map.addOverlay(%(id)s_marker);<br />
%(id)s_updateField();<br />
&nbsp;<br />
GEvent.addListener(%(id)s_marker, "dragend", %(id)s_updateField);<br />
GEvent.addListener(%(id)s_map, "zoomend", %(id)s_updateField);<br />
GEvent.addListener(%(id)s_map, "dblclick", function (overlay, latlng) { %(id)s_marker.setLatLng(latlng); %(id)s_updateField(); });<br />
GEvent.removeListener(%(id)s_clickListener);<br />
}<br />
});<br />
&lt;/script&gt;'</span><span style="color: #483d8b;">''</span> <span style="color: #66cc66;">%</span> <span style="color: black;">{</span> <span style="color: #483d8b;">'id'</span>: final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'id'</span><span style="color: black;">]</span>, <span style="color: #483d8b;">'center'</span>: center, <span style="color: #483d8b;">'zoom'</span>: zoom <span style="color: black;">}</span><br />
<span style="color: #ff7700; font-weight: bold;">else</span>:<br />
values = value.<span style="color: black;">partition</span><span style="color: black;">(</span><span style="color: #483d8b;">'|'</span><span style="color: black;">)</span><br />
&nbsp;<br />
widget = widget + u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'<br />
%(id)s_map.setCenter(new GLatLng(%(coords)s), %(zoom)s);<br />
%(id)s_marker = new GMarker(new GLatLng(%(coords)s), {draggable: true});<br />
%(id)s_map.addOverlay(%(id)s_marker);<br />
&nbsp;<br />
GEvent.addListener(%(id)s_marker, "dragend", %(id)s_updateField);<br />
GEvent.addListener(%(id)s_map, "zoomend", %(id)s_updateField);<br />
GEvent.addListener(%(id)s_map, "dblclick", function (overlay, latlng) { %(id)s_marker.setLatLng(latlng); %(id)s_updateField(); });<br />
'</span><span style="color: #483d8b;">''</span> <span style="color: #66cc66;">%</span> <span style="color: black;">{</span> <span style="color: #483d8b;">'id'</span>: final_attrs<span style="color: black;">[</span><span style="color: #483d8b;">'id'</span><span style="color: black;">]</span>, <span style="color: #483d8b;">'coords'</span>: values<span style="color: black;">[</span><span style="color: #ff4500;">0</span><span style="color: black;">]</span>, <span style="color: #483d8b;">'zoom'</span>: values<span style="color: black;">[</span><span style="color: #ff4500;">2</span><span style="color: black;">]</span> <span style="color: black;">}</span><br />
&nbsp;<br />
<span style="color: #ff7700; font-weight: bold;">return</span> mark_safe<span style="color: black;">(</span>widget<span style="color: black;">)</span></pre>
</div>
</div>
<p>Don&#8217;t forget to add the &lt;script&gt; tag linking to the Google Maps
API.</p>
<p>It is yet far from perfect, but I still hope it helps someone out
there.</p>
<img src ="http://www.blogjava.net/sealyu/aggbug/320542.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2010-05-11 03:42 <a href="http://www.blogjava.net/sealyu/archive/2010/05/11/320542.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>支持Django的主机列表（转）</title><link>http://www.blogjava.net/sealyu/archive/2010/05/11/320539.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 10 May 2010 17:14:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2010/05/11/320539.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/320539.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2010/05/11/320539.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/320539.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/320539.html</trackback:ping><description><![CDATA[服务提供商
<br />
<br />
webfaction.com&nbsp;
<br />
dreamhost.com
<br />
theplanet.com
<br />
nextlevelinternet.com
<br />
textdrive.com
<br />
rackspace.net
<br />
datotel.com
<br />
ovh.net
<br />
serverbeach.com
<br />
secureserver.net
<br />
nac.net
<br />
eli.net
<br />
oneandone.net
<br />
gypsyhosting.com
<br />
xlquest.net
<br />
vpslink.com
<br />
recrutae.net
<br />
dallas-datacenter.com
<br />
carohosting.com
<br />
rimuhosting.com
<img src ="http://www.blogjava.net/sealyu/aggbug/320539.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2010-05-11 01:14 <a href="http://www.blogjava.net/sealyu/archive/2010/05/11/320539.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>发布一个DJANGO的论坛系统LBFORUM（开源、带演示）(转)</title><link>http://www.blogjava.net/sealyu/archive/2010/03/29/316844.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 29 Mar 2010 08:30:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2010/03/29/316844.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/316844.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2010/03/29/316844.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/316844.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/316844.html</trackback:ping><description><![CDATA[<meta http-equiv="content-type" content="text/html; charset=utf-8" /><span style="border-collapse: separate; color: #000000; font-family: 'AR PL UKai CN'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: #333333; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 14px; line-height: 19px;">
<h4 style="margin: 0px; padding: 0px; text-decoration: none;">简介</h4>
<p style="margin: 0px; padding: 0px 0px 10px; text-decoration: none;">LBForum 用django开发的论坛系统，演示地址为：<a href="http://vik.haoluobo.com/lbforum/" style="margin: 0px; padding: 0px; text-decoration: none; cursor: pointer; color: #666699;">http://vik.haoluobo.com/lbforum/</a>&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
项目的地址为：<a href="http://github.com/vicalloy/LBForum" style="margin: 0px; padding: 0px; text-decoration: none; cursor: pointer; color: #666699;">http://github.com/vicalloy/LBForum</a>&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
界面部分抄的 FluxBB(一个开源的PHP论坛&nbsp;<a href="http://fluxbb.org/" style="margin: 0px; padding: 0px; text-decoration: none; cursor: pointer; color: #336699;">http://fluxbb.org/</a>&nbsp;)。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
虽然Django写的论坛也不少，不过还真没什么好用的。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
大多Django论坛都是独立的app，而且不少还缺模板，想我这样有经验的Django用户要跑起来都觉得麻烦，其他普通用户就更别说了。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
LBForum主要注重部署的方便性和易用性，功能方面目前还比较简单。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
LBForum一开始就是以整站的形式提供，所以以LBForum做为基础项目进行二次开发是很容易的。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
同时LBForum的开发尽量遵照Django可复用app原则，因此即使需要将LBForum做为独立的app集成到其他项目也并不会太难。</p>
<h4 style="margin: 0px; padding: 0px; text-decoration: none;">主要功能</h4>
<p style="margin: 0px; padding: 0px 0px 10px; text-decoration: none;">目前功能还比较简单，而且还有些小问题有待修正。</p>
<ol style="margin: 0px; padding: 0px; text-decoration: none;">
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">论坛分类，分版块</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">发帖，回帖</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">BBCode支持</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">置顶贴</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">使用django admin提供论坛管理功能</li>
</ol>
<h4 style="margin: 0px; padding: 0px; text-decoration: none;">用开发服务器把LBForum跑起来</h4>
<ol style="margin: 0px; padding: 0px; text-decoration: none;">
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">先把代码down下来。LBForum托管在github上，<a href="http://github.com/vicalloy/LBForum" style="margin: 0px; padding: 0px; text-decoration: none; cursor: pointer; color: #666699;">http://github.com/vicalloy/LBForum</a>&nbsp;。如果你没有安装git，你可以直接用界面右上方的download&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
    source功能下载代码。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">运行"scripts"create_lbforum_env.py初始化lbforum的python虚拟环境。该脚本会自动创建一个python的虚拟环境并使用easy_install安装对应的依赖包，同时将一些依赖包解压到对应的目录中。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
    注：django使用的是svn版本，所以机器上必须要安装有SVN，不然脚本会运行失败。如果因为由于svn的问题导致脚本运行失败，可以运行lbforum_env.bat进入lbforum环境，手动安装django的svn版本。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">环境初始化好后，运行lbforum_env.bat进入lbforum环境</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">运行%mg% syncdb初始化数据库</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">运行%mg% runserver启动django开发服务器</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">进入admin，创建论坛分类和版块</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">进入版块发帖</li>
</ol>
<h4 style="margin: 0px; padding: 0px; text-decoration: none;">LBForum的目录结构说明</h4>
<p style="margin: 0px; padding: 0px 0px 10px; text-decoration: none;">|+lbforum_env/#lbforum运行的python虚拟环境，运行create_lbforum_env.py后自动创建&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|+requirements/#lbforum用的第三方库和app，运行的时候会将该目录加到python路径&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|~scripts/#工程相关脚本&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |-create_lbforum_env.py#初始化python虚拟环境，并自动安装easy_install/django依赖库&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |-helper.py#提供其他脚本所需的辅助函数&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| `-lbforum_env.bat*#启动lbforum运行的虚拟环境及，并为lbforum的manage.py提供快捷方式%mg%，比如初始化数据库%mg%&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
syncdb&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|~sites/#站点配置/模板/静态文件&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| `~default/#默认站点&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|&nbsp;&nbsp; |+static/#静态资源文件，如css等&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|&nbsp;&nbsp; |+templates/#Django模板目录&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|&nbsp;&nbsp; |+templates_plus/#Django模板目录，用户将自己重写过的目标放到该目录&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|&nbsp;&nbsp; `-&#8230;&#8230;&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|~src/#django的app目录&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |+account/#account相关app。具体站点通常会对用户中心进行定制，所以该app在实际应用中很可能需要针对实际情况进行修改。&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |+djangohelper/#一些django的辅助函数等，&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |+lbforum/#lbforum的主app，论坛功能都在改app中&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |+lbregistration/#registration app的lbforum扩展，主要去掉邮件地址认证功能&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| |+onlineuser/#显示在线用户的app（可复用的django app，可脱离lbforum单独使用）<br style="margin: 0px; padding: 0px; text-decoration: none;" />
| `+simpleavatar/#头像功能的app（可复用的django app，可脱离lbforum单独使用，依赖djangohelper）&nbsp;<br style="margin: 0px; padding: 0px; text-decoration: none;" />
|+tools/#工程用到的辅助工具，目前只有一个virtualenv的脚本</p>
<h4 style="margin: 0px; padding: 0px; text-decoration: none;">注：</h4>
<ol style="margin: 0px; padding: 0px; text-decoration: none;">
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">由于计划在以后做i18n，所以目前只提供英文界面</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">django的错误提示是显示在字段后面，fluxbb的错误全部都显示在表单前面。由于模板没有调好，所以目前按照fluxbb的方式显示错误，所以错误显示有些不太正常。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">bbcode的输入框本想做成自适应大小的，不过也调得有些问题，所以现在输入框的大小固定。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">文档&#8230; ，感觉好难写-_-，目前文档不全（项目中没有带任何的文档），日后补上。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">应用程序的目录结构主要查看pinax</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">simpleavatar模块部分代码来自django-avatar</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">依赖包除用easy_install在线安装的外，尽量使用zip包的方式附带在项目中，减少安装依赖包的困难。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">远程部署脚本计划使用fabric，但fabric本身安装比较麻烦，所暂未处理。</li>
    <li style="margin: 7px 0px 8px 30px; padding: 0px; text-decoration: none;">项目最早放在googlecode，不过感觉github的功能更强些，所以移了过去。</li>
</ol>
</span></span>
<img src ="http://www.blogjava.net/sealyu/aggbug/316844.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2010-03-29 16:30 <a href="http://www.blogjava.net/sealyu/archive/2010/03/29/316844.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python IDE--Wing IDE的安装和破解（转）</title><link>http://www.blogjava.net/sealyu/archive/2009/09/28/296734.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 28 Sep 2009 02:09:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/28/296734.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/296734.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/28/296734.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/296734.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/296734.html</trackback:ping><description><![CDATA[<p>以下内容针对wingIDE 3系列，并且在3.1.6版本测试通过。</p>
<p>不过，wingIDE对中文的支持好像有问题，在我这里，中文全部显示成了方框。</p>
<p>激活的过程还有些麻烦：</p>
<p>1、将解压后的WingIDE.exe 和Python25.dll Copy到安装目录的Bin下；</p>
<p>2、运行wing3.X，出现license窗口，输入类似这样的字符CNxxx-xxxxx-xxxxx-xxxxx (x =1-9)；点击&#8220;continus&#8221;，如下图(图中最后多了一个数字3)；</p>
<p><a href="http://www.9say.com/wp-content/uploads/2008/12/20081213135629093.png" onclick=""><img size-medium="" wp-image-316="" title="wingide-crack-1" src="http://www.9say.com/wp-content/uploads/2008/12/20081213135629093-300x228.png" alt="" height="228" width="300" /></a></p>
<p>3、copy wingIDE License文件提示中的request No，如下图：</p>
<p><a href="http://www.9say.com/wp-content/uploads/2008/12/20081213135756781.png" onclick=""><img size-medium="" wp-image-317="" title="wingide-crack-2" src="http://www.9say.com/wp-content/uploads/2008/12/20081213135756781-300x276.png" alt="" height="276" width="300" /></a></p>
<p>4、打开cmd，cd到wingIDE安装目录下的bin目录，运行wingide.exe，按照提示输入CNxxx-xxxxx-xxxxx-xxxxx（步骤2中输入的序列号）和步骤3中的request code，并按照提示输入你使用的操作系统，如下图：</p>
<p><a href="http://www.9say.com/wp-content/uploads/2008/12/20081213140059046.png" onclick=""><img size-medium="" wp-image-318="" title="20081213140059046" src="http://www.9say.com/wp-content/uploads/2008/12/20081213140059046-300x214.png" alt="" height="214" width="300" /></a></p>
<p>5、将步骤4中生成的activation code:填入步骤3的提示框中，就成功破解了wingIDE</p>
<p>wingIDE professional下载地址：</p>
<p><a href="http://wingware.com/downloads/wingide/" onclick="javascript:pageTracker._trackPageview('/outbound/article/wingware.com');" target="_blank">http://wingware.com/downloads/wingide/</a></p>
<p>wingIDE professional破解文件：</p>
<p><a href="http://www.9say.com/wp-content/uploads/2008/12/wingide-crack.7z" onclick="">wingide-crack</a></p>
<img src ="http://www.blogjava.net/sealyu/aggbug/296734.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-28 10:09 <a href="http://www.blogjava.net/sealyu/archive/2009/09/28/296734.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>django中的一个选择时间的widget</title><link>http://www.blogjava.net/sealyu/archive/2009/09/28/296714.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 28 Sep 2009 00:54:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/28/296714.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/296714.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/28/296714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/296714.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/296714.html</trackback:ping><description><![CDATA[<p>This snippet defines a Widget that is very similar to the <strong>SelectDateWidget</strong> located in django.forms.extras.widgets. The main difference however is that it works with Times instead of Dates.
</p>
<p>The SelectTimeWidget supports both 24-hr and 12-hr formats, and
flexible time increments for hours, minutes and seconds. Sample usage
is illustrated below:
</p>
<pre><code># Specify a basic 24-hr time Widget (the default)<br />
t = forms.TimeField(widget=SelectTimeWidget())<br />
<br />
# Force minutes and seconds to be displayed in increments of 10<br />
t = forms.TimeField(widget=SelectTimeWidget(minute_step=10, second_step=10))<br />
<br />
# Use a 12-hr time format, which will display a 4th select <br />
# element containing a.m. and p.m. options)<br />
t = forms.TimeField(widget=SelectTimeWidget(twelve_hr=True))<br />
</code></pre>
<p>UPDATE:  Changed the time pattern to a more precise regular expression, and added the sample usage above. <br />
</p>
<p><br />
</p>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;re<br />
</span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;django.forms.widgets&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;Widget,&nbsp;Select,&nbsp;MultiWidget<br />
</span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;django.forms.extras.widgets&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;SelectDateWidget<br />
</span><span style="color: #0000ff;">from</span><span style="color: #000000;">&nbsp;django.utils.safestring&nbsp;</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;mark_safe<br />
<br />
</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;Attempt&nbsp;to&nbsp;match&nbsp;many&nbsp;time&nbsp;formats:</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">&nbsp;Example:&nbsp;"12:34:56&nbsp;P.M."&nbsp;&nbsp;matches:</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">&nbsp;('12',&nbsp;'34',&nbsp;':56',&nbsp;'56',&nbsp;'P.M.',&nbsp;'P',&nbsp;'.',&nbsp;'M',&nbsp;'.')</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">&nbsp;('12',&nbsp;'34',&nbsp;':56',&nbsp;'56',&nbsp;'P.M.')</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">&nbsp;Note&nbsp;that&nbsp;the&nbsp;colon&nbsp;":"&nbsp;before&nbsp;seconds&nbsp;is&nbsp;optional,&nbsp;but&nbsp;only&nbsp;if&nbsp;seconds&nbsp;are&nbsp;omitted</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">time_pattern&nbsp;=&nbsp;r'(\d\d?):(\d\d)(:(\d\d))?&nbsp;*((a{1}|A{1}|p{1}|P{1})(\.)?(m{1}|M{1})(\.)?)?$'</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">time_pattern&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;r</span><span style="color: #800000;">'</span><span style="color: #800000;">(\d\d?):(\d\d)(:(\d\d))?&nbsp;*([aApP]\.?[mM]\.?)?$</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;w/&nbsp;Magus's&nbsp;suggestions</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
RE_TIME&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;re.compile(time_pattern)<br />
</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;The&nbsp;following&nbsp;are&nbsp;just&nbsp;more&nbsp;readable&nbsp;ways&nbsp;to&nbsp;access&nbsp;re.matched&nbsp;groups:</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">HOURS&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0<br />
MINUTES&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />
SECONDS&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;"><br />
MERIDIEM&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;SelectTimeWidget(Widget):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">"""</span><span style="color: #800000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;Widget&nbsp;that&nbsp;splits&nbsp;time&nbsp;input&nbsp;into&nbsp;&lt;select&gt;&nbsp;elements.<br />
&nbsp;&nbsp;&nbsp;&nbsp;Allows&nbsp;form&nbsp;to&nbsp;show&nbsp;as&nbsp;24hr:&nbsp;&lt;hour&gt;:&lt;minute&gt;:&lt;second&gt;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;as&nbsp;12hr:&nbsp;&lt;hour&gt;:&lt;minute&gt;:&lt;second&gt;&nbsp;&lt;am|pm&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;Also&nbsp;allows&nbsp;user-defined&nbsp;increments&nbsp;for&nbsp;minutes/seconds<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">"""</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;hour_field&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s_hour</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;minute_field&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s_minute</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;second_field&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s_second</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;meridiem_field&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s_meridiem</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;twelve_hr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;False&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;Default&nbsp;to&nbsp;24hr.</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">__init__</span><span style="color: #000000;">(self,&nbsp;attrs</span><span style="color: #000000;">=</span><span style="color: #000000;">None,&nbsp;hour_step</span><span style="color: #000000;">=</span><span style="color: #000000;">None,&nbsp;minute_step</span><span style="color: #000000;">=</span><span style="color: #000000;">None,&nbsp;second_step</span><span style="color: #000000;">=</span><span style="color: #000000;">None,&nbsp;twelve_hr</span><span style="color: #000000;">=</span><span style="color: #000000;">False):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">'''</span><span style="color: #800000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_step,&nbsp;minute_step,&nbsp;second_step&nbsp;are&nbsp;optional&nbsp;step&nbsp;values&nbsp;for<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;the&nbsp;range&nbsp;of&nbsp;values&nbsp;for&nbsp;the&nbsp;associated&nbsp;select&nbsp;element<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;twelve_hr:&nbsp;If&nbsp;True,&nbsp;forces&nbsp;the&nbsp;output&nbsp;to&nbsp;be&nbsp;in&nbsp;12-hr&nbsp;format&nbsp;(rather&nbsp;than&nbsp;24-hr)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">'''</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.attrs&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;attrs&nbsp;</span><span style="color: #0000ff;">or</span><span style="color: #000000;">&nbsp;{}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;twelve_hr:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.twelve_hr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;True&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;Do&nbsp;12hr&nbsp;(rather&nbsp;than&nbsp;24hr)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;Default&nbsp;to&nbsp;Morning&nbsp;(A.M.)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;hour_step&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;twelve_hr:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hours&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">13</span><span style="color: #000000;">,hour_step)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">elif</span><span style="color: #000000;">&nbsp;hour_step:&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;24hr,&nbsp;with&nbsp;stepping.</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hours&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(0,</span><span style="color: #000000;">24</span><span style="color: #000000;">,hour_step)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">elif</span><span style="color: #000000;">&nbsp;twelve_hr:&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;12hr,&nbsp;no&nbsp;stepping</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hours&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">13</span><span style="color: #000000;">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;24hr,&nbsp;no&nbsp;stepping</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hours&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(0,</span><span style="color: #000000;">24</span><span style="color: #000000;">)&nbsp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;minute_step:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.minutes&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(0,</span><span style="color: #000000;">60</span><span style="color: #000000;">,minute_step)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.minutes&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(0,</span><span style="color: #000000;">60</span><span style="color: #000000;">)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;second_step:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.seconds&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(0,</span><span style="color: #000000;">60</span><span style="color: #000000;">,second_step)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.seconds&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;range(0,</span><span style="color: #000000;">60</span><span style="color: #000000;">)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;render(self,&nbsp;name,&nbsp;value,&nbsp;attrs</span><span style="color: #000000;">=</span><span style="color: #000000;">None):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">:&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;try&nbsp;to&nbsp;get&nbsp;time&nbsp;values&nbsp;from&nbsp;a&nbsp;datetime.time&nbsp;object&nbsp;(value)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_val,&nbsp;minute_val,&nbsp;second_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value.hour,&nbsp;value.minute,&nbsp;value.second<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;self.twelve_hr:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;hour_val&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">p.m.</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">except</span><span style="color: #000000;">&nbsp;AttributeError:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;minute_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;second_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;isinstance(value,&nbsp;basestring):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;match&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;RE_TIME.match(value)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;match:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_groups&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;match.groups();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;int(time_groups[HOURS])&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">24</span><span style="color: #000000;">&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;force&nbsp;to&nbsp;range(0-24)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;int(time_groups[MINUTES])&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;time_groups[SECONDS]&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;None:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;second_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;second_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;int(time_groups[SECONDS])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;check&nbsp;to&nbsp;see&nbsp;if&nbsp;meridiem&nbsp;was&nbsp;passed&nbsp;in</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;time_groups[MERIDIEM]&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;None:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;time_groups[MERIDIEM]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;otherwise,&nbsp;set&nbsp;the&nbsp;meridiem&nbsp;based&nbsp;on&nbsp;the&nbsp;time</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;self.twelve_hr:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;hour_val&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">p.m.</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.meridiem_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;None<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;If&nbsp;we're&nbsp;doing&nbsp;a&nbsp;12-hr&nbsp;clock,&nbsp;there&nbsp;will&nbsp;be&nbsp;a&nbsp;meridiem&nbsp;value,&nbsp;so&nbsp;make&nbsp;sure&nbsp;the</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;hours&nbsp;get&nbsp;printed&nbsp;correctly</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;self.twelve_hr&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;self.meridiem_val:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;self.meridiem_val.lower().startswith(</span><span style="color: #800000;">'</span><span style="color: #800000;">p</span><span style="color: #800000;">'</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;hour_val&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;hour_val&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">24</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;hour_val&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">elif</span><span style="color: #000000;">&nbsp;hour_val&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;0:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;self.attrs:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id_&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;self.attrs[</span><span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id_&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">id_%s</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;NOTE:&nbsp;for&nbsp;times&nbsp;to&nbsp;get&nbsp;displayed&nbsp;correctly,&nbsp;the&nbsp;values&nbsp;MUST&nbsp;be&nbsp;converted&nbsp;to&nbsp;unicode</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;When&nbsp;Select&nbsp;builds&nbsp;a&nbsp;list&nbsp;of&nbsp;options,&nbsp;it&nbsp;checks&nbsp;against&nbsp;Unicode&nbsp;values</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;u</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;hour_val<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;u</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;minute_val<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;second_val&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;u</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;second_val<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour_choices&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[(</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">%</span><span style="color: #000000;">i,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">%</span><span style="color: #000000;">i)&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;self.hours]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local_attrs&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;self.build_attrs(id</span><span style="color: #000000;">=</span><span style="color: #000000;">self.hour_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;id_)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_html&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Select(choices</span><span style="color: #000000;">=</span><span style="color: #000000;">hour_choices).render(self.hour_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;hour_val,&nbsp;local_attrs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append(select_html)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute_choices&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[(</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">%</span><span style="color: #000000;">i,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">%</span><span style="color: #000000;">i)&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;self.minutes]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local_attrs[</span><span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;self.minute_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;id_<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_html&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Select(choices</span><span style="color: #000000;">=</span><span style="color: #000000;">minute_choices).render(self.minute_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;minute_val,&nbsp;local_attrs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append(select_html)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;second_choices&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[(</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">%</span><span style="color: #000000;">i,&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">%.2d</span><span style="color: #800000;">"</span><span style="color: #000000;">%</span><span style="color: #000000;">i)&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;self.seconds]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local_attrs[</span><span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;self.second_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;id_<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_html&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Select(choices</span><span style="color: #000000;">=</span><span style="color: #000000;">second_choices).render(self.second_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;second_val,&nbsp;local_attrs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append(select_html)<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;self.twelve_hr:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;&nbsp;If&nbsp;we&nbsp;were&nbsp;given&nbsp;an&nbsp;initial&nbsp;value,&nbsp;make&nbsp;sure&nbsp;the&nbsp;correct&nbsp;meridiem&nbsp;get's&nbsp;selected.</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;self.meridiem_val&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;None&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;&nbsp;self.meridiem_val.startswith(</span><span style="color: #800000;">'</span><span style="color: #800000;">p</span><span style="color: #800000;">'</span><span style="color: #000000;">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meridiem_choices&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[(</span><span style="color: #800000;">'</span><span style="color: #800000;">p.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #800000;">'</span><span style="color: #800000;">p.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">),&nbsp;(</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">)]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meridiem_choices&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[(</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #800000;">'</span><span style="color: #800000;">a.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">),&nbsp;(</span><span style="color: #800000;">'</span><span style="color: #800000;">p.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">,</span><span style="color: #800000;">'</span><span style="color: #800000;">p.m.</span><span style="color: #800000;">'</span><span style="color: #000000;">)]<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local_attrs[</span><span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;local_attrs[</span><span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;self.meridiem_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;id_<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select_html&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Select(choices</span><span style="color: #000000;">=</span><span style="color: #000000;">meridiem_choices).render(self.meridiem_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;self.meridiem_val,&nbsp;local_attrs)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append(select_html)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;mark_safe(u</span><span style="color: #800000;">'</span><span style="color: #800000;">\n</span><span style="color: #800000;">'</span><span style="color: #000000;">.join(output))<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;id_for_label(self,&nbsp;id_):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s_hour</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;id_<br />
&nbsp;&nbsp;&nbsp;&nbsp;id_for_label&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;classmethod(id_for_label)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;value_from_datadict(self,&nbsp;data,&nbsp;files,&nbsp;name):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;if&nbsp;there's&nbsp;not&nbsp;h:m:s&nbsp;data,&nbsp;assume&nbsp;zero:</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data.get(self.hour_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;0)&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;hour</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data.get(self.minute_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;0)&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;minute&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data.get(self.second_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;0)&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;second</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meridiem&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data.get(self.meridiem_field&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;name,&nbsp;None)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">NOTE:&nbsp;if&nbsp;meridiem&nbsp;IS&nbsp;None,&nbsp;assume&nbsp;24-hr</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;meridiem&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">not</span><span style="color: #000000;">&nbsp;None:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;meridiem.lower().startswith(</span><span style="color: #800000;">'</span><span style="color: #800000;">p</span><span style="color: #800000;">'</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;int(h)&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(int(h)</span><span style="color: #000000;">+</span><span style="color: #000000;">12</span><span style="color: #000000;">)</span><span style="color: #000000;">%</span><span style="color: #000000;">24</span><span style="color: #000000;">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">elif</span><span style="color: #000000;">&nbsp;meridiem.lower().startswith(</span><span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;int(h)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(int(h)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;0&nbsp;</span><span style="color: #0000ff;">or</span><span style="color: #000000;">&nbsp;h)&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;m&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;s:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #800000;">%s:%s:%s</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;(h,&nbsp;m,&nbsp;s)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;data.get(name,&nbsp;None)<br />
</span></div>
<img src ="http://www.blogjava.net/sealyu/aggbug/296714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-28 08:54 <a href="http://www.blogjava.net/sealyu/archive/2009/09/28/296714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>django form的widgets类型列表</title><link>http://www.blogjava.net/sealyu/archive/2009/09/27/296644.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Sun, 27 Sep 2009 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/27/296644.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/296644.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/27/296644.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/296644.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/296644.html</trackback:ping><description><![CDATA[<h1>Widgets<a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#module-django.forms.widgets" title="Permalink to this headline">&#182;</a></h1>
<p>A widget is Django&#8217;s representation of a HTML input element. The widget
handles the rendering of the HTML, and the extraction of data from a GET/POST
dictionary that corresponds to the widget.</p>
<p>Django provides a representation of all the basic HTML widgets, plus some
commonly used groups of widgets:</p>
<dl><dt id="django.forms.TextInput">
class <tt>TextInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.TextInput" title="Permalink to this definition">&#182;</a></dt><dd>Text input: <tt literal="">&lt;input type='text' ...&gt;</tt></dd></dl>
<dl><dt id="django.forms.PasswordInput">
class <tt>PasswordInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.PasswordInput" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Password input: <tt literal="">&lt;input type='password' ...&gt;</tt></p>
<p>Takes one optional argument:</p>
<dl><dt id="django.forms.PasswordInput.render_value">
<tt>render_value</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.PasswordInput.render_value" title="Permalink to this definition">&#182;</a></dt><dd>Determines whether the widget will have a value filled in when the
form is re-displayed after a validation error (default is <tt docutils="" literal="">True</tt>).</dd></dl>
</dd></dl>
<dl><dt id="django.forms.HiddenInput">
class <tt>HiddenInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.HiddenInput" title="Permalink to this definition">&#182;</a></dt><dd>Hidden input: <tt literal="">&lt;input type='hidden' ...&gt;</tt></dd></dl>
<dl><dt id="django.forms.MultipleHiddenInput">
class <tt>MultipleHiddenInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.MultipleHiddenInput" title="Permalink to this definition">&#182;</a></dt><dd>Multiple <tt literal="">&lt;input type='hidden' ...&gt;</tt> widgets.</dd></dl>
<dl><dt id="django.forms.FileInput">
class <tt>FileInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.FileInput" title="Permalink to this definition">&#182;</a></dt><dd>File upload input: <tt literal="">&lt;input type='file' ...&gt;</tt></dd></dl>
<dl><dt id="django.forms.DateInput">
class <tt>DateInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.DateInput" title="Permalink to this definition">&#182;</a></dt><dd>
<div>
New in Django 1.1: <a external="" href="http://docs.djangoproject.com/en/dev/releases/1.1/#releases-1-1"><em>Please, see the release notes</em></a></div>
<p>Date input as a simple text box: <tt literal="">&lt;input type='text' ...&gt;</tt></p>
<p>Takes one optional argument:</p>
<dl><dt id="django.forms.DateInput.format">
<tt>format</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.DateInput.format" title="Permalink to this definition">&#182;</a></dt><dd>The format in which this field&#8217;s initial value will be displayed.</dd></dl>
<p>If no <tt literal="">format</tt> argument is provided, the default format is <tt literal="">'%Y-%m-%d'</tt>.</p>
</dd></dl>
<dl><dt id="django.forms.DateTimeInput">
class <tt>DateTimeInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.DateTimeInput" title="Permalink to this definition">&#182;</a></dt><dd>
<div>
New in Django 1.0: <a external="" href="http://docs.djangoproject.com/en/dev/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Date/time input as a simple text box: <tt literal="">&lt;input type='text' ...&gt;</tt></p>
<p>Takes one optional argument:</p>
<dl><dt id="django.forms.DateTimeInput.format">
<tt>format</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.DateTimeInput.format" title="Permalink to this definition">&#182;</a></dt><dd>The format in which this field&#8217;s initial value will be displayed.</dd></dl>
<p>If no <tt literal="">format</tt> argument is provided, the default format is <tt literal="">'%Y-%m-%d
%H:%M:%S'</tt>.</p>
</dd></dl>
<dl><dt id="django.forms.TimeInput">
class <tt>TimeInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.TimeInput" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Time input as a simple text box: <tt literal="">&lt;input type='text' ...&gt;</tt></p>
<p>Takes one optional argument:</p>
<dl><dt id="django.forms.TimeInput.format">
<tt>format</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.TimeInput.format" title="Permalink to this definition">&#182;</a></dt><dd>The format in which this field&#8217;s initial value will be displayed.</dd></dl>
<p>If no <tt literal="">format</tt> argument is provided, the default format is <tt literal="">'%H:%M:%S'</tt>.</p>
<div>
Changed in Django 1.1: The <tt literal="">format</tt> argument was not supported in Django 1.0.</div>
</dd></dl>
<dl><dt id="django.forms.Textarea">
class <tt>Textarea</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Textarea" title="Permalink to this definition">&#182;</a></dt><dd>Text area: <tt literal="">&lt;textarea&gt;...&lt;/textarea&gt;</tt></dd></dl>
<dl><dt id="django.forms.CheckboxInput">
class <tt>CheckboxInput</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.CheckboxInput" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Checkbox: <tt literal="">&lt;input type='checkbox' ...&gt;</tt></p>
<p>Takes one optional argument:</p>
<dl><dt id="django.forms.CheckboxInput.check_test">
<tt>check_test</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.CheckboxInput.check_test" title="Permalink to this definition">&#182;</a></dt><dd>A callable that takes the value of the CheckBoxInput
and returns <tt docutils="" literal="">True</tt> if the checkbox should be checked for
that value.</dd></dl>
</dd></dl>
<dl><dt id="django.forms.Select">
class <tt>Select</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Select" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Select widget: <tt literal="">&lt;select&gt;&lt;option ...&gt;...&lt;/select&gt;</tt></p>
<p>Requires that your field provides <tt docutils="" literal="">choices</tt>.</p>
</dd></dl>
<dl><dt id="django.forms.NullBooleanSelect">
class <tt>NullBooleanSelect</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.NullBooleanSelect" title="Permalink to this definition">&#182;</a></dt><dd>Select widget with options &#8216;Unknown&#8217;, &#8216;Yes&#8217; and &#8216;No&#8217;</dd></dl>
<dl><dt id="django.forms.SelectMultiple">
class <tt>SelectMultiple</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.SelectMultiple" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Select widget allowing multiple selection: <tt literal="">&lt;select
multiple='multiple'&gt;...&lt;/select&gt;</tt></p>
<p>Requires that your field provides <tt docutils="" literal="">choices</tt>.</p>
</dd></dl>
<dl><dt id="django.forms.RadioSelect">
class <tt>RadioSelect</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.RadioSelect" title="Permalink to this definition">&#182;</a></dt><dd>
<p>A list of radio buttons:</p>
<div>
<pre>&lt;ul&gt;<br />
&lt;li&gt;&lt;input type='radio' ...&gt;&lt;/li&gt;<br />
...<br />
&lt;/ul&gt;</pre>
</div>
<p>Requires that your field provides <tt docutils="" literal="">choices</tt>.</p>
</dd></dl>
<dl><dt id="django.forms.CheckboxSelectMultiple">
class <tt>CheckboxSelectMultiple</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.CheckboxSelectMultiple" title="Permalink to this definition">&#182;</a></dt><dd>
<p>A list of checkboxes:</p>
<div>
<pre>&lt;ul&gt;<br />
&lt;li&gt;&lt;input type='checkbox' ...&gt;&lt;/li&gt;<br />
...<br />
&lt;/ul&gt;</pre>
</div>
</dd></dl>
<dl><dt id="django.forms.MultiWidget">
class <tt>MultiWidget</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.MultiWidget" title="Permalink to this definition">&#182;</a></dt><dd>Wrapper around multiple other widgets</dd></dl>
<dl><dt id="django.forms.SplitDateTimeWidget">
class <tt>SplitDateTimeWidget</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.SplitDateTimeWidget" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Wrapper around two widgets: <tt literal="">DateInput</tt> for the date, and <tt literal="">TimeInput</tt>
for the time.</p>
<p>Takes two optional arguments, <tt literal="">date_format</tt> and <tt literal="">time_format</tt>, which
work just like the <tt literal="">format</tt> argument for <tt literal="">DateInput</tt> and <tt literal="">TimeInput</tt>.</p>
<div>
Changed in Django 1.1: The <tt literal="">date_format</tt> and <tt literal="">time_format</tt> arguments were not supported in Django 1.0.</div>
</dd></dl>
<dl><dt id="django.forms.SelectDateWidget">
class <tt>SelectDateWidget</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.SelectDateWidget" title="Permalink to this definition">&#182;</a></dt><dd>
<p>Wrapper around three select widgets: one each for month, day, and year.
Note that this widget lives in a separate file from the standard widgets.</p>
<div>
<div>
<pre>from django.forms.extras.widgets import SelectDateWidget<br />
<br />
date = forms.DateField(widget=SelectDateWidget())<br />
</pre>
</div>
</div>
</dd></dl>
<div id="s-specifying-widgets">
<h2>Specifying widgets<a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#specifying-widgets" title="Permalink to this headline">&#182;</a></h2>
<dl><dt id="django.forms.Form.widget">
<tt>Form.</tt><tt>widget</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Form.widget" title="Permalink to this definition">&#182;</a></dt><dd><br />
</dd></dl>
<p>Whenever you specify a field on a form, Django will use a default widget
that is appropriate to the type of data that is to be displayed. To find
which widget is used on which field, see the documentation for the
built-in Field classes.</p>
<p>However, if you want to use a different widget for a field, you can -
just use the 'widget' argument on the field definition. For example:</p>
<div>
<div>
<pre>from django import forms<br />
<br />
class CommentForm(forms.Form):<br />
name = forms.CharField()<br />
url = forms.URLField()<br />
comment = forms.CharField(widget=forms.Textarea)<br />
</pre>
</div>
</div>
<p>This would specify a form with a comment that uses a larger Textarea widget,
rather than the default TextInput widget.</p>
</div>
<div id="s-customizing-widget-instances">
<h2>Customizing widget instances<a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#customizing-widget-instances" title="Permalink to this headline">&#182;</a></h2>
<p>When Django renders a widget as HTML, it only renders the bare minimum
HTML - Django doesn't add a class definition, or any other widget-specific
attributes. This means that all 'TextInput' widgets will appear the same
on your web page.</p>
<p>If you want to make one widget look different to another, you need to
specify additional attributes for each widget. When you specify a
widget, you can provide a list of attributes that will be added to the
rendered HTML for the widget.</p>
<p>For example, take the following simple form:</p>
<div>
<div>
<pre>class CommentForm(forms.Form):<br />
name = forms.CharField()<br />
url = forms.URLField()<br />
comment = forms.CharField()<br />
</pre>
</div>
</div>
<p>This form will include three default TextInput widgets, with default rendering -
no CSS class, no extra attributes. This means that the input boxes provided for
each widget will be rendered exactly the same:</p>
<div>
<div>
<pre>&gt;&gt;&gt; f = CommentForm(auto_id=False)<br />
&gt;&gt;&gt; f.as_table()<br />
&lt;tr&gt;&lt;th&gt;Name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="name" /&gt;&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;th&gt;Url:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="url"/&gt;&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;th&gt;Comment:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="comment" /&gt;&lt;/td&gt;&lt;/tr&gt;<br />
</pre>
</div>
</div>
<p>On a real web page, you probably don't want every widget to look the same. You
might want a larger input element for the comment, and you might want the 'name'
widget to have some special CSS class. To do this, you use the <tt literal="">attrs</tt>
argument when creating the widget:</p>
<dl><dt id="django.forms.Widget.attrs">
<tt>Widget.</tt><tt>attrs</tt><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs" title="Permalink to this definition">&#182;</a></dt><dd><br />
</dd></dl>
<p>For example:</p>
<div>
<div>
<pre>class CommentForm(forms.Form):<br />
name = forms.CharField(<br />
widget=forms.TextInput(attrs={'class':'special'}))<br />
url = forms.URLField()<br />
comment = forms.CharField(<br />
widget=forms.TextInput(attrs={'size':'40'}))<br />
</pre>
</div>
</div>
<p>Django will then include the extra attributes in the rendered output:</p>
<div>
<div>
<pre>&gt;&gt;&gt; f = CommentForm(auto_id=False)<br />
&gt;&gt;&gt; f.as_table()<br />
&lt;tr&gt;&lt;th&gt;Name:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="name" class="special"/&gt;&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;th&gt;Url:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="url"/&gt;&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;th&gt;Comment:&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="comment" size="40"/&gt;&lt;/td&gt;&lt;/tr&gt;<br />
</pre>
</div>
</div>
</div>
<img src ="http://www.blogjava.net/sealyu/aggbug/296644.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-27 15:35 <a href="http://www.blogjava.net/sealyu/archive/2009/09/27/296644.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>django jquery ajax 的一个小例子</title><link>http://www.blogjava.net/sealyu/archive/2009/09/24/296229.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 24 Sep 2009 00:51:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/24/296229.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/296229.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/24/296229.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/296229.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/296229.html</trackback:ping><description><![CDATA[<div>
<h1>Using Django templates with jQuery AJAX</h1>
<div id="single-date" class="date">2009 January 6</div>
</div>
<div clear="">
<div>tags: <a href="http://www.nomadjourney.com/tag/ajax/" rel="tag">ajax</a>, <a href="http://www.nomadjourney.com/tag/ajax-search/" rel="tag">ajax search</a>, <a href="http://www.nomadjourney.com/tag/django/" rel="tag">Django</a>, <a href="http://www.nomadjourney.com/tag/django-templates/" rel="tag">Django templates</a>, <a href="http://www.nomadjourney.com/tag/jquery/" rel="tag">jQuery</a></div>
<div>by Nizam</div>
</div>
<div clear="">
<p>I
recently discovered a neat way of displaying data retrieved using
jquery AJAX in concert with Django&#8217;s template engine. You can create a
view in Django which simply uses the <a href="http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response">render_to_response</a> shortcut function to render the results server-side and then just use <a href="http://docs.jquery.com/Ajax/load#urldatacallback">jquery.load</a> to dynamically fetch the results.</p>
<p>Eventhough, returning some raw JSON data is much more efficient as far as bandwidth is concerned, method is a lot simpler.</p>
<p>I have been using jquery for over a year now. I have found that its
built-in DOM manipulation features are a bit limited, especially for
manipulating tables (e.g., adding rows dynamically). This method is
much cleaner than doing all that DOM manipulation.</p>
<p>Here is all the jQuery code to handle the search and AJAX spinner display:</p>
<div id="highlighter_628681" class="syntaxhighlighter ">
<div>
<div><a viewsource="" style="width: 16px; height: 16px;" title="view source" href="http://www.nomadjourney.com/2009/01/using-django-templates-with-jquery-ajax/#viewSource">view source</a><a printsource="" style="width: 16px; height: 16px;" title="print" href="http://www.nomadjourney.com/2009/01/using-django-templates-with-jquery-ajax/#printSource">print</a><a about="" style="width: 16px; height: 16px;" title="?" href="http://www.nomadjourney.com/2009/01/using-django-templates-with-jquery-ajax/#about">?</a></div>
</div>
<div>
<div alt1=""><code>01.</code><span style="margin-left: 0px ! important;"><code>$( document ).ready( </code><code>function</code><code>() {</code></span></div>
<div alt2=""><code>02.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 28px ! important;"><code>$( </code><code>'#searchSubmit'</code> <code>).click( </code><code>function</code><code>() {</code></span></div>
<div alt1=""><code>03.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 56px ! important;"><code>q = $( </code><code>'#q'</code> <code>).val();</code></span></div>
<div alt2=""><code>04.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 56px ! important;"><code>$( </code><code>'#results'</code> <code>).html( </code><code>'&amp;nbsp;'</code> <code>).load(</code></span></div>
<div alt1=""><code>05.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 140px ! important;"><code>'{% url demo_user_search %}?q='</code> <code>+ q );</code></span></div>
<div alt2=""><code>06.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 28px ! important;"><code>});</code></span></div>
<div alt1=""><code>07.</code><span style="margin-left: 0px ! important;"><code>});</code></span></div>
<div alt2=""><code>08.</code>&nbsp;</div>
<div alt1=""><code>09.</code><span style="margin-left: 0px ! important;"><code>$( document ).ajaxStart( </code><code>function</code><code>() {</code></span></div>
<div alt2=""><code>10.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 28px ! important;"><code>$( </code><code>'#spinner'</code> <code>).show();</code></span></div>
<div alt1=""><code>11.</code><span style="margin-left: 0px ! important;"><code>}).ajaxStop( </code><code>function</code><code>() {</code></span></div>
<div alt2=""><code>12.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 28px ! important;"><code>$( </code><code>'#spinner'</code> <code>).hide();</code></span></div>
<div alt1=""><code>13.</code><span style="margin-left: 0px ! important;"><code>});</code></span></div>
</div>
</div>
<p>Here is the Django view function that does the heavy lifting on the server-side:</p>
<div id="highlighter_37479" class="syntaxhighlighter ">
<div>
<div><a viewsource="" style="width: 16px; height: 16px;" title="view source" href="http://www.nomadjourney.com/2009/01/using-django-templates-with-jquery-ajax/#viewSource">view source</a><a printsource="" style="width: 16px; height: 16px;" title="print" href="http://www.nomadjourney.com/2009/01/using-django-templates-with-jquery-ajax/#printSource">print</a><a about="" style="width: 16px; height: 16px;" title="?" href="http://www.nomadjourney.com/2009/01/using-django-templates-with-jquery-ajax/#about">?</a></div>
</div>
<div>
<div alt1=""><code>01.</code><span style="margin-left: 0px ! important;"><code>def</code> <code>ajax_user_search( request ):</code></span></div>
<div alt2=""><code>02.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 28px ! important;"><code>if</code> <code>request.is_ajax():</code></span></div>
<div alt1=""><code>03.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 56px ! important;"><code>q </code><code>=</code> <code>request.GET.get( </code><code>'q'</code> <code>)</code></span></div>
<div alt2=""><code>04.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 56px ! important;"><code>if</code> <code>q </code><code>is</code> <code>not</code> <code>None</code><code>:</code></span></div>
<div alt1=""><code>05.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 84px ! important;"><code>results </code><code>=</code> <code>User.objects.filter(</code></span></div>
<div alt2=""><code>06.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 112px ! important;"><code>Q( first_name__contains </code><code>=</code> <code>q ) |</code></span></div>
<div alt1=""><code>07.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 112px ! important;"><code>Q( last_name__contains </code><code>=</code> <code>q ) |</code></span></div>
<div alt2=""><code>08.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 112px ! important;"><code>Q( username__contains </code><code>=</code> <code>q ) ).order_by( </code><code>'username'</code> <code>)</code></span></div>
<div alt1=""><code>09.</code>&nbsp;</div>
<div alt2=""><code>10.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 84px ! important;"><code>template </code><code>=</code> <code>'results.html'</code></span></div>
<div alt1=""><code>11.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 84px ! important;"><code>data </code><code>=</code> <code>{</code></span></div>
<div alt2=""><code>12.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 112px ! important;"><code>'results'</code><code>: results,</code></span></div>
<div alt1=""><code>13.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 84px ! important;"><code>}</code></span></div>
<div alt2=""><code>14.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 84px ! important;"><code>return</code> <code>render_to_response( template, data,</code></span></div>
<div alt1=""><code>15.</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><span style="margin-left: 112px ! important;"><code>context_instance </code><code>=</code> <code>RequestContext( request ) )</code></span></div>
</div>
</div>
<p>Here are some screenshots of the results:</p>
<div id="attachment_59" class="wp-caption aligncenter" style="width: 602px;"><a href="http://www.nomadjourney.com/wp-content/uploads/2009/01/2_ajax1.png"><img src="http://www.nomadjourney.com/wp-content/uploads/2009/01/2_ajax1.png" alt="AJAX operation in progress" title="AJAX operation in progress" class="size-full wp-image-59" height="143" width="592" /></a>
<p>AJAX operation in progress</p>
</div>
<div id="attachment_60" class="wp-caption aligncenter" style="width: 602px;"><a href="http://www.nomadjourney.com/wp-content/uploads/2009/01/3_results1.png"><img src="http://www.nomadjourney.com/wp-content/uploads/2009/01/3_results1.png" alt="Returned results" title="Returned results" class="size-full wp-image-60" height="269" width="592" /></a>
<p>Returned results</p>
</div>
<div id="attachment_58" class="wp-caption aligncenter" style="width: 602px;"><a href="http://www.nomadjourney.com/wp-content/uploads/2009/01/1_noresults1.png"><img src="http://www.nomadjourney.com/wp-content/uploads/2009/01/1_noresults1.png" alt="No results" title="No results" class="size-full wp-image-58" height="151" width="592" /></a>
<p>No results</p>
</div>
<p>The sample Django project is included for your perusal and is released under the MIT license. I used the excellent <a href="http://www.aptana.com/">Aptana Studio</a> IDE when working on this demo so it can be imported straight into it as an Aptana project.</p>
<p><a href="http://www.nomadjourney.com/wp-content/uploads/2009/01/ajax_user_list.zip">Download ajax_user_list.zip</a></p>
</div>
<img src ="http://www.blogjava.net/sealyu/aggbug/296229.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-24 08:51 <a href="http://www.blogjava.net/sealyu/archive/2009/09/24/296229.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>django form的field类型列表</title><link>http://www.blogjava.net/sealyu/archive/2009/09/23/296173.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Wed, 23 Sep 2009 07:18:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/23/296173.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/296173.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/23/296173.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/296173.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/296173.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: This document is for Django's SVN release, which can besignificantly different from previous releases. Get old docs here:Django 1.0Form fields&#182;class Field(**kwargs)&#182;When yo...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2009/09/23/296173.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/296173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-23 15:18 <a href="http://www.blogjava.net/sealyu/archive/2009/09/23/296173.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>django modal的field类型列表</title><link>http://www.blogjava.net/sealyu/archive/2009/09/11/294748.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 11 Sep 2009 08:10:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/11/294748.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/294748.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/11/294748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/294748.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/294748.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 从官网转下来，以便查阅：Model field reference&#182;This document contains all the gory details about all the field options andfield types Django&#8217;s got to offer.See alsoIf the built-in fields don...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2009/09/11/294748.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/294748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-11 16:10 <a href="http://www.blogjava.net/sealyu/archive/2009/09/11/294748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>django生成po文件时出现的xgettext的错误 （转） </title><link>http://www.blogjava.net/sealyu/archive/2009/09/10/294535.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 10 Sep 2009 02:18:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/09/10/294535.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/294535.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/09/10/294535.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/294535.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/294535.html</trackback:ping><description><![CDATA[<p>通过摸索和查阅django官网文档，我今天已经解决windows上执行django-admin.py makemessages -l zh_CN 后出现'xgettext'不是内部或外部命令，也不是可运行的程序或批处理文件的错误 </p>
<p>原因和解决办法是： </p>
<p>分析django的源代码中关于国际化方面的(makemessages)就知道，上述命令最终调用的是os.cmd之类调用命令行的东西，而
windows的path路径列表中没有xgettext.exe,这是因为windows上没有gettext，所以要安装
gettext（python有gettext，但是好像不行，它没有xgettext） <br />
下载地址是：<a href="http://sourceforge.net/projects/gettext">http://sourceforge.net/projects/gettext</a> <br />
下载这三个： <br />
gettext-runtime-X.bin.woe32.zip&nbsp; <br />
gettext-tools-X.bin.woe32.zip&nbsp; <br />
libiconv-X.bin.woe32.zip&nbsp; </p>
<p>解压这3个zip文件到同一个文件夹，比如 C:"Program Files"gettext-utils&nbsp; <br />
然后添加到系统变量的PATH中: <br />
控制面板 &gt; 系统 &gt; 高级 &gt; 环境变量 <br />
在系统变量列表中, 选中Path, 然后单击编辑&nbsp; <br />
添加 ;C:"Program Files"gettext-utils"bin 到结尾后一直点确定就行了 </p>
<p>&nbsp;&nbsp; <br />
那3个文件全部选中后，直接用winrar解压，选择一个目录后解压即可 </p>
<img src ="http://www.blogjava.net/sealyu/aggbug/294535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-09-10 10:18 <a href="http://www.blogjava.net/sealyu/archive/2009/09/10/294535.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Intro to Unintrusive JavaScript with Django</title><link>http://www.blogjava.net/sealyu/archive/2009/08/29/293132.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Sat, 29 Aug 2009 14:53:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/08/29/293132.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/293132.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/08/29/293132.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/293132.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/293132.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: by Will Larson September 21, 2008 JQuery django javascript There are quite a number of tutorials looking at using Ajax with Django. Despite that, it is still a topic that leaves many confuse...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2009/08/29/293132.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/293132.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-08-29 22:53 <a href="http://www.blogjava.net/sealyu/archive/2009/08/29/293132.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django和Ajax教程（转）</title><link>http://www.blogjava.net/sealyu/archive/2009/08/29/293131.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Sat, 29 Aug 2009 14:51:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/08/29/293131.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/293131.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/08/29/293131.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/293131.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/293131.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 第一部分：Django与Ajax：介绍和安装&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 近来我开始向正在用Django开发的一个项目中添加Ajax技术，开始有点坎坷。有关于这方面可用的文档资料几乎找不到。这里所写的文章就算是我为改变这种资料贫乏的现状所做的一点微薄的努力吧。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2009/08/29/293131.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/293131.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-08-29 22:51 <a href="http://www.blogjava.net/sealyu/archive/2009/08/29/293131.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django资源大全(转)</title><link>http://www.blogjava.net/sealyu/archive/2009/08/21/292164.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 21 Aug 2009 13:54:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/08/21/292164.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/292164.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/08/21/292164.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/292164.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/292164.html</trackback:ping><description><![CDATA[最近经常在这个版面看到Django相关扩展的介绍，而其一个扩展写一个帖子，觉得没太必要吧。
<br />
<br />
以前整理的django资源列表，从我的wiki上转过来的。
<br />
<br />
要找django资源，官网的wiki也是一个不错的地方。
<br />
<br />
http://code.djangoproject.com/wiki/DjangoResources
<br />
资源
<br />
<br />
&nbsp;&nbsp;&nbsp; * django官网
<br />
&nbsp;&nbsp;&nbsp; * django文档
<br />
&nbsp;&nbsp;&nbsp; * Django Step by Step 曾是最佳的django入门教程，只是这个教程是针对0.95的，现在不少地方已经有所变动。
<br />
&nbsp;&nbsp;&nbsp; * django可复用app设计
<br />
&nbsp;&nbsp;&nbsp; * django最佳实践 django可复用app设计 的一个更好的分支。个人为人这个文档是每个django开发人员必读的。
<br />
&nbsp;&nbsp;&nbsp; * django最佳实践 中文翻译
<br />
&nbsp;&nbsp;&nbsp; * django book 一本免费的django电子书
<br />
&nbsp;&nbsp;&nbsp; * django book 中文翻译
<br />
&nbsp;&nbsp;&nbsp; * djangosnippets 一个关于django的代码片段网站，在里面可以找到一些应用的django代码片段。站点本身是用django写的，且开发源码。
<br />
&nbsp;&nbsp;&nbsp; * djangosites 这个网站里介绍了很多实用django搭建的站点。而且可以从这里找到很多带源代码的django站点。不过我个人觉得里面没有多少值得参考的站点代码。
<br />
<br />
使用django搭建的站点
<br />
<br />
&nbsp;&nbsp;&nbsp; * django官网 django官网本身使用django搭建，而其提供了代码。django官网的大多功能由整合的trac实现，所以网站的django代码没几行。
<br />
&nbsp;&nbsp;&nbsp; * 海报网 据说这是目前流量最大的django站点。据我的了解，这个网站的流量也确实大的有些超乎想象，该网站应当有接近CSDN的流量。不过这个网站将大量的页面进行了静态化，如果以这个网站的标准来评价django的性能应当不够客观。
<br />
&nbsp;&nbsp;&nbsp; * 好看簿 国内的另一个django站点，目前也有不错的流量。作为该网站的用户，我感觉网站的响应速度并不怎么快。看来好看簿在服务器优化方面还需要更多的努力。
<br />
<br />
django的开源项目
<br />
<br />
&nbsp;&nbsp;&nbsp; * pinax 这是我看到的最有价值的django开源项目。pinax可以看做是django的一个脚手架。她提供了快速开始一个新django项目的方法，同时对大量第三方app的使用方法进行了演示。django的app质量参差不齐，如果你想挑选app，那你可以看看pinax里都集成了哪些app。pinax 里集成了的app通常都不至于太烂。此外pinax自身也带了一些有用的app，比如blog等。
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果你想以最快的速度了解pinax，可以去 http://cloud27.com/ 看看。这是一个用pinax搭建的SNS网站。
<br />
&nbsp;&nbsp;&nbsp; * Satchmo 网店系统。看她的介绍，似乎已经有不少人在用这东西了。
<br />
&nbsp;&nbsp;&nbsp; * LFS(Lightning Fast Shop) 网店系统，就Demo来看似乎是倾向于房屋交易平台。陆陆续续的也有部分商业网站开始使用该系统了，比如这个。
<br />
&nbsp;&nbsp;&nbsp; * reviewboard 非常有前途的一个code review工具。最开始是VMware在用，来后给开源了。
<br />
<br />
django的可重用app
<br />
CMS
<br />
<br />
&nbsp;&nbsp;&nbsp; * Django CMS Apps Comparison Django CMS Apps的比较
<br />
<br />
Forum
<br />
<br />
&nbsp;&nbsp;&nbsp; * Django Forum Apps Comparison Django Forum Apps的比较
<br />
<br />
调试
<br />
<br />
&nbsp;&nbsp;&nbsp; * django-debug-toolbar 为django站点增加调试功能，支持查看django生成的sql语句，及sql的执行时间等，功能强大。不过由于该组件使用了jquery，似乎会使用部分使用了jquery的站点无法正常工作。
<br />
&nbsp;&nbsp;&nbsp; * django-db-log 将django的异常保存到数据库。
<br />
&nbsp;&nbsp;&nbsp; * django-logging 增加sql日志。直接在站点的网页上查看当前页面所使用sql语句以及执行时间。功能比django-debug-toolbar要弱不少，不过要是django-debug-toolbar把你的页面毁了，那就试试这个吧。
<br />
<br />
数据库升级
<br />
<br />
在项目开发过程中表结构的变动总是难免，django目前还不支持表结构的自动更新，不过相关的第三方app倒不少。这些app各有各的特色，具体如何选择就要看个人的喜好了。
<br />
<br />
&nbsp;&nbsp;&nbsp; * SchemaEvolution 介绍django数据库升级的wiki页面，相关的app都可以在这个页面找到。
<br />
<br />
未分类
<br />
<br />
&nbsp;&nbsp;&nbsp; * django-registration 注册功能，支持帐户的邮件激活。
<br />
&nbsp;&nbsp;&nbsp; * django-tagging 为站点增加tagging功能
<br />
&nbsp;&nbsp;&nbsp; * django-voting 投票功能
<br />
&nbsp;&nbsp;&nbsp; * django-ajax-validation 使用ajax方式对表单进行校验，需要jquery的支持。这个组件只能校验，如果需要增加ajax提交表单的功能，还需要自己手动做些修改。
<br />
&nbsp;&nbsp;&nbsp; * django-announcements 发送站内公告，该公告只显示一次。可以设置公共的发送对象（所有人，指定用户，登陆用户）。
<br />
&nbsp;&nbsp;&nbsp; * django-avatar 为用户增加设置个人头像的功能。支持 http://www.gravatar.com/ 和用户上传头像。
<br />
&nbsp;&nbsp;&nbsp; * django-dbtemplates 将django的template保存在数据库，并通过cache来加速template的加载。这样可以很方便的通过admin来编辑template。我想这东西应当可以用来实现一些简单的CMS功能，不过似乎用处不是太大。
<br />
&nbsp;&nbsp;&nbsp; * django-microblogging 类似Twitter的微型博客
<br />
&nbsp;&nbsp;&nbsp; * django-notification 消息分发组件，用来实现类似好友最新动态的功能。
<br />
&nbsp;&nbsp;&nbsp; * django-pagination 一组翻页相关的utils，包括用于实现翻页的tag等。
<br />
&nbsp;&nbsp;&nbsp; * django-robots 通过django的admin来管理robots.txt。不过我觉得robots.txt这东西为什么还需要用admin来管理，还不如直接编辑来的方便。
<br />
&nbsp;&nbsp;&nbsp; * django-messages 实现站内消息的功能。目前该组件的功能的功能还比较弱，虽然作者在进行重构，但似乎进展缓慢。如果有需要我可能会自行对该组件进行一些增强。
<br />
&nbsp;&nbsp;&nbsp; * django-threadedcomments 评论组件，可以给任何的models增加评论功能。
<br />
&nbsp;&nbsp;&nbsp; * django-extensions 一些全局的 management extensions。其中包括create_app的增强，使用werkzeug做开发服务器等功能。
<br />
&nbsp;&nbsp;&nbsp; * django-uni-form 以div方式渲染forms
<br />
&nbsp;&nbsp;&nbsp; * django-timezones 时区设置组件，允许用户设置自己所在的时区。
<br />
&nbsp;&nbsp;&nbsp; * django-bookmarks 书签管理组件
<br />
&nbsp;&nbsp;&nbsp; * django-email-confirmation 在用户设置邮件地址后，给用户的邮箱发送一封确认邮件。用户点击邮件里的link来确认邮件地址的有效性。
<br />
&nbsp;&nbsp;&nbsp; * django-mailer 邮件发送的增强组件，会在邮件发送失败时记录失败日志。
<br />
&nbsp;&nbsp;&nbsp; * django-friends 朋友组件，支持邀请用户成为好友等。
<br />
&nbsp;&nbsp;&nbsp; * django-locations 似乎是一个基于地区的社交网络，需要yaohoo map的支持。
<br />
&nbsp;&nbsp;&nbsp; * django-oembed 方面媒体（视频等）内容嵌入，自动将媒体的连接转换成正确的html代码。
<br />
&nbsp;&nbsp;&nbsp; * django-swaps 易物平台，比如用PSP换NDS啥的。
<br />
&nbsp;&nbsp;&nbsp; * django-wikiapp wiki组件，支持多种wiki语法。不过目前该组件的功能还是比较简单，比如不支持附件。
<br />
&nbsp;&nbsp;&nbsp; * django-mptt 用于生成树形列表的app。如果你认为它会自动给你生成一个完整的树，那你就错了，她只是一组工具。我感觉这东西并不是太好用，是否有什么优点，暂时不太清楚。
<br />
&nbsp;&nbsp;&nbsp; * django-photologue 图片管理，上传图片，缩略图显示等。
<br />
&nbsp;&nbsp;&nbsp; * django-filebrowser django-admin的一个扩展，可以直接通过admin对服务器上的文件进行管理。我没用过这东西，不过看介绍，感觉非常棒。
<br />
&nbsp;&nbsp;&nbsp; * goflow Django上的一个工作流引擎，设计灵感来源于OpenFlow。没用过这东西，对工作流也不太了解，所以也不知道这东西好不好。不过这似乎是django上唯一的一个工作流app。
<img src ="http://www.blogjava.net/sealyu/aggbug/292164.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-08-21 21:54 <a href="http://www.blogjava.net/sealyu/archive/2009/08/21/292164.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The flatpages app of Django</title><link>http://www.blogjava.net/sealyu/archive/2009/07/28/288799.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Tue, 28 Jul 2009 12:28:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/07/28/288799.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/288799.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/07/28/288799.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/288799.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/288799.html</trackback:ping><description><![CDATA[<h1>The flatpages app<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#module-django.contrib.flatpages" title="Permalink to this headline">&#182;</a></h1>
<p>Django comes with an optional &#8220;flatpages&#8221; application. It lets you store simple
&#8220;flat&#8221; HTML content in a database and handles the management for you via
Django&#8217;s admin interface and a Python API.</p>
<p>A flatpage is a simple object with a URL, title and content. Use it for
one-off, special-case pages, such as &#8220;About&#8221; or &#8220;Privacy Policy&#8221; pages, that
you want to store in a database but for which you don&#8217;t want to develop a
custom Django application.</p>
<p>A flatpage can use a custom template or a default, systemwide flatpage
template. It can be associated with one, or multiple, sites.</p>
<div>
New in Django 1.0: <a external="" href="http://docs.djangoproject.com/en/dev/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>The content field may optionally be left blank if you prefer to put your
content in a custom template.</p>
<p>Here are some examples of flatpages on Django-powered sites:</p>
<ul>
    <li><a external="" href="http://www.chicagocrime.org/about/">http://www.chicagocrime.org/about/</a></li>
    <li><a external="" href="http://www.everyblock.com/about/">http://www.everyblock.com/about/</a></li>
    <li><a external="" href="http://www.lawrence.com/about/contact/">http://www.lawrence.com/about/contact/</a></li>
</ul>
<div id="s-installation">
<h2>Installation<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#installation" title="Permalink to this headline">&#182;</a></h2>
<p>To install the flatpages app, follow these steps:</p>
<ol>
    <li>
    <p>Install the <a title="Lets you operate multiple web sites from the same database and Django project" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/contrib/sites/#module-django.contrib.sites"><tt docutils="" literal="">sites framework</tt></a> by adding
    <tt literal="">'django.contrib.sites'</tt> to your <a external="" href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-INSTALLED_APPS"><tt docutils="" literal="">INSTALLED_APPS</tt></a> setting,
    if it&#8217;s not already in there.</p>
    <p>Also make sure you&#8217;ve correctly set <a external="" href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-SITE_ID"><tt docutils="" literal="">SITE_ID</tt></a> to the ID of the
    site the settings file represents. This will usually be <tt literal="">1</tt> (i.e.
    <tt literal="">SITE_ID = 1</tt>, but if you&#8217;re not using the sites framework to manage
    multiple sites, it could be the ID of a different site.</p>
    </li>
    <li>
    <p>Add <tt literal="">'django.contrib.flatpages'</tt> to your <a external="" href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-INSTALLED_APPS"><tt docutils="" literal="">INSTALLED_APPS</tt></a>
    setting.</p>
    </li>
    <li>
    <p>Add <tt literal="">'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'</tt>
    to your <a external="" href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-MIDDLEWARE_CLASSES"><tt docutils="" literal="">MIDDLEWARE_CLASSES</tt></a> setting.</p>
    </li>
    <li>
    <p>Run the command <tt docutils="" literal="">manage.py syncdb</tt>.</p>
    </li>
</ol>
</div>
<div id="s-how-it-works">
<h2>How it works<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#how-it-works" title="Permalink to this headline">&#182;</a></h2>
<p><tt literal="">manage.py syncdb</tt> creates two tables in your database: <tt literal="">django_flatpage</tt>
and <tt literal="">django_flatpage_sites</tt>. <tt literal="">django_flatpage</tt> is a simple lookup table
that simply maps a URL to a title and bunch of text content.
<tt literal="">django_flatpage_sites</tt> associates a flatpage with a site.</p>
<p>The <tt docutils="" literal="">FlatpageFallbackMiddleware</tt>
does all of the work. Each time any Django application raises a 404 error, this
middleware checks the flatpages database for the requested URL as a last resort.
Specifically, it checks for a flatpage with the given URL with a site ID that
corresponds to the <a external="" href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-SITE_ID"><tt docutils="" literal="">SITE_ID</tt></a> setting.</p>
<p>If it finds a match, it follows this algorithm:</p>
<ul>
    <li>If the flatpage has a custom template, it loads that template. Otherwise,
    it loads the template <tt literal="">flatpages/default.html</tt>.</li>
    <li>It passes that template a single context variable, <tt docutils="" literal="">flatpage</tt>, which
    is the flatpage object. It uses
    <tt docutils="" literal="">RequestContext</tt> in rendering the
    template.</li>
</ul>
<p>If it doesn&#8217;t find a match, the request continues to be processed as usual.</p>
<p>The middleware only gets activated for 404s &#8211; not for 500s or responses of any
other status code.</p>
<p>Note that the order of <a external="" href="http://docs.djangoproject.com/en/dev/ref/settings/#setting-MIDDLEWARE_CLASSES"><tt docutils="" literal="">MIDDLEWARE_CLASSES</tt></a> matters. Generally, you can
put <tt docutils="" literal="">FlatpageFallbackMiddleware</tt> at
the end of the list, because it&#8217;s a last resort.</p>
<p>For more on middleware, read the <a external="" href="http://docs.djangoproject.com/en/dev/topics/http/middleware/#topics-http-middleware"><em>middleware docs</em></a>.</p>
<div admonition="">
<p admonition-title="">Ensure that your 404 template works</p>
<p>Note that the
<tt docutils="" literal="">FlatpageFallbackMiddleware</tt>
only steps in once another view has successfully produced a 404 response.
If another view or middleware class attempts to produce a 404 but ends up
raising an exception instead (such as a <tt literal="">TemplateDoesNotExist</tt>
exception if your site does not have an appropriate template to
use for HTTP 404 responses), the response will become an HTTP 500
(&#8220;Internal Server Error&#8221;) and the
<tt docutils="" literal="">FlatpageFallbackMiddleware</tt>
will not attempt to serve a flat page.</p>
</div>
</div>
<div id="s-how-to-add-change-and-delete-flatpages">
<h2>How to add, change and delete flatpages<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#how-to-add-change-and-delete-flatpages" title="Permalink to this headline">&#182;</a></h2>
<div id="s-via-the-admin-interface">
<h3>Via the admin interface<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#via-the-admin-interface" title="Permalink to this headline">&#182;</a></h3>
<p>If you&#8217;ve activated the automatic Django admin interface, you should see a
&#8220;Flatpages&#8221; section on the admin index page. Edit flatpages as you edit any
other object in the system.</p>
</div>
<div id="s-via-the-python-api">
<h3>Via the Python API<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#via-the-python-api" title="Permalink to this headline">&#182;</a></h3>
<dl><dt id="django.contrib.flatpages.models.FlatPage">
class <tt>models.</tt><tt>FlatPage</tt><a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#django.contrib.flatpages.models.FlatPage" title="Permalink to this definition">&#182;</a></dt><dd>Flatpages are represented by a standard
<a external="" href="http://docs.djangoproject.com/en/dev/topics/db/models/#topics-db-models"><em>Django model</em></a>,
which lives in <a external="" href="http://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py">django/contrib/flatpages/models.py</a>. You can access
flatpage objects via the <a external="" href="http://docs.djangoproject.com/en/dev/topics/db/queries/#topics-db-queries"><em>Django database API</em></a>.</dd></dl>
</div>
</div>
<div id="s-flatpage-templates">
<h2>Flatpage templates<a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#flatpage-templates" title="Permalink to this headline">&#182;</a></h2>
<p>By default, flatpages are rendered via the template
<tt literal="">flatpages/default.html</tt>, but you can override that for a particular
flatpage.</p>
<p>Creating the <tt literal="">flatpages/default.html</tt> template is your responsibility;
in your template directory, just create a <tt literal="">flatpages</tt> directory
containing a file <tt literal="">default.html</tt>.</p>
<p>Flatpage templates are passed a single context variable, <tt docutils="" literal="">flatpage</tt>,
which is the flatpage object.</p>
<p>Here&#8217;s a sample <tt literal="">flatpages/default.html</tt> template:</p>
<div>
<div>
<pre>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"<br />
"http://www.w3.org/TR/REC-html40/loose.dtd"&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;{{ flatpage.title }}&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
{{ flatpage.content }}<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</pre>
</div>
</div>
<p>Since you're already entering raw HTML into the admin page for a flatpage,
both <tt literal="">flatpage.title</tt> and <tt literal="">flatpage.content</tt> are marked as <strong>not</strong>
requiring <a external="" href="http://docs.djangoproject.com/en/dev/topics/templates/#automatic-html-escaping"><em>automatic HTML escaping</em></a> in the
template.</p>
</div>
<img src ="http://www.blogjava.net/sealyu/aggbug/288799.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-07-28 20:28 <a href="http://www.blogjava.net/sealyu/archive/2009/07/28/288799.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django标签之divisibleby(转)</title><link>http://www.blogjava.net/sealyu/archive/2009/05/28/278311.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 28 May 2009 02:58:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/05/28/278311.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/278311.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/05/28/278311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/278311.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/278311.html</trackback:ping><description><![CDATA[Example:
<br />
<div>
<div>
<div>Python代码 <embed src="http://androider.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%0A%7B%25%20if%20value%7Cdivisibleby%3A%222%22%20%25%7D%0A%20%20%20%20Even!%0A%7B%25%20else%20%25%7D%0A%20%20%20%20Odd!%0A%7B%25%20else%20%25%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14"></div>
</div>
<ol start="1">
    <li>{%&nbsp;if&nbsp;value|divisibleby:"2"&nbsp;%}&nbsp;&nbsp;</li>
    <li>&nbsp;&nbsp;&nbsp;&nbsp;Even!&nbsp;&nbsp;</li>
    <li>{%&nbsp;else&nbsp;%}&nbsp;&nbsp;</li>
    <li>&nbsp;&nbsp;&nbsp;&nbsp;Odd!&nbsp;&nbsp;</li>
    <li>{%&nbsp;else&nbsp;%}&nbsp;&nbsp;</li>
</ol>
</div>
<pre style="display: none;" name="code" class="python">{% if value|divisibleby:"2" %}
Even!
{% else %}
Odd!
{% else %}
</pre>
<br />
Returns True if the value is divisible by the argument.
<img src ="http://www.blogjava.net/sealyu/aggbug/278311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-05-28 10:58 <a href="http://www.blogjava.net/sealyu/archive/2009/05/28/278311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django中的循环</title><link>http://www.blogjava.net/sealyu/archive/2009/05/28/278307.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 28 May 2009 02:38:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/05/28/278307.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/278307.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/05/28/278307.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/278307.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/278307.html</trackback:ping><description><![CDATA[<p id="cn172">Django不支持退出循环操作。如果我们想退出循环，可以改变正在迭代的变量，让其仅仅包含需要迭代的项目。同理，Django也不支持continue语句，我们无法让当前迭代操作跳回到循环头部。（请参看本章稍后的理念和限制小节，了解下决定这个设计的背后原因）</p>
<p id="cn173"><tt literal="">{% for %}</tt> 标签在循环中设置了一个特殊的 <tt literal="">forloop</tt> 模板变量。这个变量能提供一些当前循环进展的信息：</p>
<blockquote>
<p id="cn174"><tt literal="">forloop.counter</tt> 总是一个表示当前循环的执行次数的整数计数器。这个计数器是从1开始的，所以在第一次循环时 <tt literal="">forloop.counter</tt> 将会被设置为1。例子如下：</p>
</blockquote>
<pre literal-block="" id="cn175">{% for item in todo_list %}<br />
&lt;p&gt;{{ forloop.counter }}: {{ item }}&lt;/p&gt;<br />
{% endfor %}<br />
</pre>
<p id="cn177"><tt literal="">forloop.counter0</tt> 类似于 <tt literal="">forloop.counter</tt> ，但是它是从0计数的。第一次执行循环时这个变量会被设置为0。</p>
<p id="cn178"><tt literal="">forloop.revcounter</tt> 是表示循环中剩余项的整型变量。在循环初次执行时 <tt literal="">forloop.revcounter</tt> 将被设置为序列中项的总数。最后一次循环执行中，这个变量将被置1。</p>
<p id="cn179"><tt literal="">forloop.revcounter0</tt> 类似于 <tt literal="">forloop.revcounter</tt> ，但它以0做为结束索引。在第一次执行循环时，该变量会被置为序列的项的个数减1。在最后一次迭代时，该变量为0。</p>
<p id="cn180"><tt literal="">forloop.first</tt> 是一个布尔值。在第一次执行循环时该变量为True，在下面的情形中这个变量是很有用的。</p>
<pre literal-block="" id="cn181">{% for object in objects %}<br />
{% if forloop.first %}&lt;li class="first"&gt;{% else %}&lt;li&gt;{% endif %}<br />
{{ object }}<br />
&lt;/li&gt;<br />
{% endfor %}<br />
</pre>
<p id="cn183"><tt literal="">forloop.last</tt> 是一个布尔值；在最后一次执行循环时被置为True。一个常见的用法是在一系列的链接之间放置管道符（|）</p>
<pre literal-block="" id="cn184">{% for link in links %}{{ link }}{% if not forloop.last %} | {% endif %}{% endfor %}<br />
<br />
The above template code might output something like this::<br />
<br />
Link1 | Link2 | Link3 | Link4<br />
<br />
<strong>3.</strong><br />
</pre>
<p id="cn186"><tt literal="">forloop.parentloop</tt> 是一个指向当前循环的上一级循环的 <tt literal="">forloop</tt> 对象的引用（在嵌套循环的情况下）。例子在此：</p>
<pre literal-block="" id="cn187">{% for country in countries %}<br />
&lt;table&gt;<br />
{% for city in country.city_list %}<br />
&lt;tr&gt;<br />
&lt;td&gt;Country #{{ forloop.parentloop.counter }}&lt;/td&gt;<br />
&lt;td&gt;City #{{ forloop.counter }}&lt;/td&gt;<br />
&lt;td&gt;{{ city }}&lt;/td&gt;<br />
&lt;/tr&gt;<br />
{% endfor %}<br />
&lt;/table&gt;<br />
{% endfor %}<br />
</pre>
<p id="cn189"><tt literal="">forloop</tt> 变量仅仅能够在循环中使用，在模板解析器碰到 <tt literal="">{% endfor %}</tt> 标签时， <tt literal="">forloop</tt> 就不可访问了。</p>
<p id="cn190">Context和forloop变量</p>
在一个 <tt literal="">{% for %}</tt> 块中，已存在的变量会被移除，以避免 <tt literal="">forloop</tt> 变量被覆盖。Django会把这个变量移动到 <tt literal="">forloop.parentloop</tt> 中。通常我们不用担心这个问题，但是一旦我们在模板中定义了 <tt literal="">forloop</tt> 这个变量（当然我们反对这样做），在 <tt literal="">{% for %}</tt> 块中它会在 <tt literal="">forloop.parentloop</tt> 被重新命名。<br />
<br />
for<br />
轮询数组中的每一元素。例如显示一个给出的运动员的序列``athlete_list``:<br />
&lt;ul&gt;<br />
{% for athlete in athlete_list %}<br />
&lt;li&gt;{{ athlete.name }}&lt;/li&gt;<br />
{% endfor %}<br />
&lt;/ul&gt;<br />
你同样可以反过来用``{% for obj in list reversed %}``轮询一个列表。<br />
``for``循环设置了许多循环中有用的变量(见表F-1)。<br />
Inline literal start-string without end-string.<br />
表 F-1. {% for %}循环中的可用变量<br />
变量名描述<br />
forloop.counter 循环的当前重复值 (标示为1)。<br />
forloop.counter0 循环的当前重复值 (标示为0)。<br />
forloop.revcounter 循环到最后的重复数 (标示为1)。<br />
forloop.revcounter0 循环到最后的重复数 (标示为0)。<br />
forloop.first 第一次循环则为``True``。<br />
forloop.last 最后一次循环则为``True`` 。<br />
forloop.parentloop 用于嵌套循环,该循环位于当前循环之上。<br />
<br />
<br />
<img src ="http://www.blogjava.net/sealyu/aggbug/278307.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-05-28 10:38 <a href="http://www.blogjava.net/sealyu/archive/2009/05/28/278307.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How to use Django with FastCGI, SCGI, or AJP¶</title><link>http://www.blogjava.net/sealyu/archive/2009/04/10/264942.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 10 Apr 2009 15:39:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/04/10/264942.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/264942.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/04/10/264942.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/264942.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/264942.html</trackback:ping><description><![CDATA[<p>Although the current preferred setup for running Django is <a class="reference external" href="http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/#howto-deployment-modwsgi"><em>Apache with
mod_wsgi</em></a>, many people use shared hosting, on
which protocols such as FastCGI, SCGI or AJP are the only viable options. In
some setups, these protocols may provide better performance than <a class="reference external" href="http://code.google.com/p/modwsgi/">mod_wsgi</a>.</p>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">This document primarily focuses on FastCGI. Other protocols, such as SCGI
and AJP, are also supported, through the <tt class="docutils literal"><span class="pre">flup</span></tt> Python package. See the
<a class="reference internal" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#protocols">Protocols</a> section below for specifics about SCGI and AJP.</p>
</div>
<p>Essentially, FastCGI is an efficient way of letting an external application
serve pages to a Web server. The Web server delegates the incoming Web requests
(via a socket) to FastCGI, which executes the code and passes the response back
to the Web server, which, in turn, passes it back to the client&#8217;s Web browser.</p>
<p>Like mod_python, FastCGI allows code to stay in memory, allowing requests to be
served with no startup time. Unlike <a class="reference external" href="http://www.modpython.org/">mod_python</a> (or <a class="reference external" href="http://perl.apache.org/">mod_perl</a>), a FastCGI
process doesn&#8217;t run inside the Web server process, but in a separate,
persistent process.</p>
<div class="admonition-why-run-code-in-a-separate-process admonition">
<p class="first admonition-title">Why run code in a separate process?</p>
<p>The traditional <tt class="docutils literal"><span class="pre">mod_*</span></tt> arrangements in Apache embed various scripting
languages (most notably PHP, Python and Perl) inside the process space of
your Web server. Although this lowers startup time &#8211; because code doesn&#8217;t
have to be read off disk for every request &#8211; it comes at the cost of
memory use. For mod_python, for example, every Apache process gets its own
Python interpreter, which uses up a considerable amount of RAM.</p>
<p class="last">Due to the nature of FastCGI, it&#8217;s even possible to have processes that run
under a different user account than the Web server process. That&#8217;s a nice
security benefit on shared systems, because it means you can secure your
code from other users.</p>
</div>
<div class="section" id="s-prerequisite-flup">
<h2>Prerequisite: flup<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#prerequisite-flup" title="Permalink to this headline">&#182;</a></h2>
<p>Before you can start using FastCGI with Django, you&#8217;ll need to install <a class="reference external" href="http://www.saddi.com/software/flup/">flup</a>, a
Python library for dealing with FastCGI. Version 0.5 or newer should work fine.</p>
</div>
<div class="section" id="s-starting-your-fastcgi-server">
<h2>Starting your FastCGI server<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#starting-your-fastcgi-server" title="Permalink to this headline">&#182;</a></h2>
<p>FastCGI operates on a client-server model, and in most cases you&#8217;ll be starting
the FastCGI process on your own. Your Web server (be it Apache, lighttpd, or
otherwise) only contacts your Django-FastCGI process when the server needs a
dynamic page to be loaded. Because the daemon is already running with the code
in memory, it&#8217;s able to serve the response very quickly.</p>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">If you&#8217;re on a shared hosting system, you&#8217;ll probably be forced to use
Web server-managed FastCGI processes. See the section below on running
Django with Web server-managed processes for more information.</p>
</div>
<p>A Web server can connect to a FastCGI server in one of two ways: It can use
either a Unix domain socket (a &#8220;named pipe&#8221; on Win32 systems), or it can use a
TCP socket. What you choose is a manner of preference; a TCP socket is usually
easier due to permissions issues.</p>
<p>To start your server, first change into the directory of your project (wherever
your <a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/django-admin/#ref-django-admin"><em>manage.py</em></a> is), and then run the
<tt class="xref docutils literal"><span class="pre">runfcgi</span></tt> command:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>./manage.py runfcgi <span class="o">[</span>options<span class="o">]</span><br />
</pre>
</div>
</div>
<p>If you specify <tt class="docutils literal"><span class="pre">help</span></tt> as the only option after <tt class="xref docutils literal"><span class="pre">runfcgi</span></tt>, it'll
display a list of all the available options.</p>
<p>You'll need to specify either a <tt class="docutils literal"><span class="pre">socket</span></tt>, a <tt class="docutils literal"><span class="pre">protocol</span></tt> or both <tt class="docutils literal"><span class="pre">host</span></tt> and
<tt class="docutils literal"><span class="pre">port</span></tt>. Then, when you set up your Web server, you'll just need to point it at
the host/port or socket you specified when starting the FastCGI server. See the
<a class="reference internal" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#examples">examples</a>, below.</p>
<div class="section" id="s-protocols">
<h3>Protocols<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#protocols" title="Permalink to this headline">&#182;</a></h3>
<p>Django supports all the protocols that <a class="reference external" href="http://www.saddi.com/software/flup/">flup</a> does, namely <a class="reference external" href="http://www.fastcgi.com/">fastcgi</a>, <a class="reference external" href="http://python.ca/scgi/protocol.txt">SCGI</a> and
<a class="reference external" href="http://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html">AJP1.3</a> (the Apache JServ Protocol, version 1.3). Select your preferred
protocol by using the <tt class="docutils literal"><span class="pre">protocol=&lt;protocol_name&gt;</span></tt> option with <tt class="docutils literal"><span class="pre">./manage.py</span>
<span class="pre">runfcgi</span></tt> -- where <tt class="docutils literal"><span class="pre">&lt;protocol_name&gt;</span></tt> may be one of: <tt class="docutils literal"><span class="pre">fcgi</span></tt> (the default),
<tt class="docutils literal"><span class="pre">scgi</span></tt> or <tt class="docutils literal"><span class="pre">ajp</span></tt>. For example:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>./manage.py runfcgi <span class="nv">protocol</span><span class="o">=</span>scgi<br />
</pre>
</div>
</div>
</div>
<div class="section" id="s-examples">
<h3>Examples<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#examples" title="Permalink to this headline">&#182;</a></h3>
<p>Running a threaded server on a TCP port:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>./manage.py runfcgi <span class="nv">method</span><span class="o">=</span>threaded <span class="nv">host</span><span class="o">=</span>127.0.0.1 <span class="nv">port</span><span class="o">=</span>3033<br />
</pre>
</div>
</div>
<p>Running a preforked server on a Unix domain socket:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>./manage.py runfcgi <span class="nv">method</span><span class="o">=</span>prefork <span class="nv">socket</span><span class="o">=</span>/home/user/mysite.sock <span class="nv">pidfile</span><span class="o">=</span>django.pid<br />
</pre>
</div>
</div>
<p>Run without daemonizing (backgrounding) the process (good for debugging):</p>
<div class="highlight-bash">
<div class="highlight">
<pre>./manage.py runfcgi <span class="nv">daemonize</span><span class="o">=</span><span class="nb">false </span><span class="nv">socket</span><span class="o">=</span>/tmp/mysite.sock <span class="nv">maxrequests</span><span class="o">=</span>1<br />
</pre>
</div>
</div>
</div>
<div class="section" id="s-stopping-the-fastcgi-daemon">
<h3>Stopping the FastCGI daemon<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#stopping-the-fastcgi-daemon" title="Permalink to this headline">&#182;</a></h3>
<p>If you have the process running in the foreground, it's easy enough to stop it:
Simply hitting <tt class="docutils literal"><span class="pre">Ctrl-C</span></tt> will stop and quit the FastCGI server. However, when
you're dealing with background processes, you'll need to resort to the Unix
<tt class="docutils literal"><span class="pre">kill</span></tt> command.</p>
<p>If you specify the <tt class="docutils literal"><span class="pre">pidfile</span></tt> option to <tt class="xref docutils literal"><span class="pre">runfcgi</span></tt>, you can kill the
running FastCGI daemon like this:</p>
<div class="highlight-bash">
<div class="highlight">
<pre><span class="nb">kill</span> <span class="sb">`</span>cat <span class="nv">$PIDFILE</span><span class="sb">`</span><br />
</pre>
</div>
</div>
<p>...where <tt class="docutils literal"><span class="pre">$PIDFILE</span></tt> is the <tt class="docutils literal"><span class="pre">pidfile</span></tt> you specified.</p>
<p>To easily restart your FastCGI daemon on Unix, try this small shell script:</p>
<div class="highlight-bash">
<div class="highlight">
<pre><span class="c">#!/bin/bash</span><br />
<br />
<span class="c"># Replace these three settings.</span><br />
<span class="nv">PROJDIR</span><span class="o">=</span><span class="s2">"/home/user/myproject"</span><br />
<span class="nv">PIDFILE</span><span class="o">=</span><span class="s2">"$PROJDIR/mysite.pid"</span><br />
<span class="nv">SOCKET</span><span class="o">=</span><span class="s2">"$PROJDIR/mysite.sock"</span><br />
<br />
<span class="nb">cd</span> <span class="nv">$PROJDIR</span><br />
<span class="k">if</span> <span class="o">[</span> -f <span class="nv">$PIDFILE</span> <span class="o">]</span>; <span class="k">then</span><br />
<span class="k">    </span><span class="nb">kill</span> <span class="sb">`</span>cat -- <span class="nv">$PIDFILE</span><span class="sb">`</span><br />
rm -f -- <span class="nv">$PIDFILE</span><br />
<span class="k">fi</span><br />
<br />
<span class="nb">exec</span> /usr/bin/env - <span class="se">\</span><br />
<span class="nv">PYTHONPATH</span><span class="o">=</span><span class="s2">"../python:.."</span> <span class="se">\</span><br />
./manage.py runfcgi <span class="nv">socket</span><span class="o">=</span><span class="nv">$SOCKET</span> <span class="nv">pidfile</span><span class="o">=</span><span class="nv">$PIDFILE</span><br />
</pre>
</div>
</div>
</div>
</div>
<div class="section" id="s-apache-setup">
<h2>Apache setup<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#apache-setup" title="Permalink to this headline">&#182;</a></h2>
<p>To use Django with Apache and FastCGI, you'll need Apache installed and
configured, with <a class="reference external" href="http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html">mod_fastcgi</a> installed and enabled. Consult the Apache
documentation for instructions.</p>
<p>Once you've got that set up, point Apache at your Django FastCGI instance by
editing the <tt class="docutils literal"><span class="pre">httpd.conf</span></tt> (Apache configuration) file. You'll need to do two
things:</p>
<ul class="simple">
    <li>Use the <tt class="docutils literal"><span class="pre">FastCGIExternalServer</span></tt> directive to specify the location of
    your FastCGI server.</li>
    <li>Use <tt class="docutils literal"><span class="pre">mod_rewrite</span></tt> to point URLs at FastCGI as appropriate.</li>
</ul>
<div class="section" id="s-specifying-the-location-of-the-fastcgi-server">
<h3>Specifying the location of the FastCGI server<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#specifying-the-location-of-the-fastcgi-server" title="Permalink to this headline">&#182;</a></h3>
<p>The <tt class="docutils literal"><span class="pre">FastCGIExternalServer</span></tt> directive tells Apache how to find your FastCGI
server. As the <a class="reference external" href="http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer">FastCGIExternalServer docs</a> explain, you can specify either a
<tt class="docutils literal"><span class="pre">socket</span></tt> or a <tt class="docutils literal"><span class="pre">host</span></tt>. Here are examples of both:</p>
<div class="highlight-apache">
<div class="highlight">
<pre><span class="c"># Connect to FastCGI via a socket / named pipe.</span><br />
<span class="nb">FastCGIExternalServer</span> <span class="sx">/home/user/public_html/mysite.fcgi</span> -socket <span class="sx">/home/user/mysite.sock</span><br />
<br />
<span class="c"># Connect to FastCGI via a TCP host/port.</span><br />
<span class="nb">FastCGIExternalServer</span> <span class="sx">/home/user/public_html/mysite.fcgi</span> -host <span class="m">127.0.0.1</span>:3033<br />
</pre>
</div>
</div>
<p>In either case, the file <tt class="docutils literal"><span class="pre">/home/user/public_html/mysite.fcgi</span></tt> doesn't
actually have to exist. It's just a URL used by the Web server internally -- a
hook for signifying which requests at a URL should be handled by FastCGI. (More
on this in the next section.)</p>
</div>
<div class="section" id="s-using-mod-rewrite-to-point-urls-at-fastcgi">
<h3>Using mod_rewrite to point URLs at FastCGI<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#using-mod-rewrite-to-point-urls-at-fastcgi" title="Permalink to this headline">&#182;</a></h3>
<p>The second step is telling Apache to use FastCGI for URLs that match a certain
pattern. To do this, use the <a class="reference external" href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html">mod_rewrite</a> module and rewrite URLs to
<tt class="docutils literal"><span class="pre">mysite.fcgi</span></tt> (or whatever you specified in the <tt class="docutils literal"><span class="pre">FastCGIExternalServer</span></tt>
directive, as explained in the previous section).</p>
<p>In this example, we tell Apache to use FastCGI to handle any request that
doesn't represent a file on the filesystem and doesn't start with <tt class="docutils literal"><span class="pre">/media/</span></tt>.
This is probably the most common case, if you're using Django's admin site:</p>
<div class="highlight-apache">
<div class="highlight">
<pre><span class="nt">&lt;VirtualHost</span> <span class="s">12.34.56.78</span><span class="nt">&gt;</span><br />
<span class="nb">ServerName</span> example.com<br />
<span class="nb">DocumentRoot</span> <span class="sx">/home/user/public_html</span><br />
<span class="nb">Alias</span> <span class="sx">/media</span> <span class="sx">/home/user/python/django/contrib/admin/media</span><br />
<span class="nb">RewriteEngine</span> <span class="k">On</span><br />
<span class="nb">RewriteRule</span> ^/(media.*)$ /$1 [QSA,L,PT]<br />
<span class="nb">RewriteCond</span> %{REQUEST_FILENAME} !-f<br />
<span class="nb">RewriteRule</span> ^/(.*)$ <span class="sx">/mysite.fcgi/</span>$1 [QSA,L]<br />
<span class="nt">&lt;/VirtualHost&gt;</span><br />
</pre>
</div>
</div>
<p>Django will automatically use the pre-rewrite version of the URL when
constructing URLs with the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></tt> template tag (and similar methods).</p>
</div>
</div>
<div class="section" id="s-lighttpd-setup">
<h2>lighttpd setup<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#lighttpd-setup" title="Permalink to this headline">&#182;</a></h2>
<p><a class="reference external" href="http://www.lighttpd.net/">lighttpd</a> is a lightweight Web server commonly used for serving static files. It
supports FastCGI natively and, thus, is a good choice for serving both static
and dynamic pages, if your site doesn't have any Apache-specific needs.</p>
<p>Make sure <tt class="docutils literal"><span class="pre">mod_fastcgi</span></tt> is in your modules list, somewhere after
<tt class="docutils literal"><span class="pre">mod_rewrite</span></tt> and <tt class="docutils literal"><span class="pre">mod_access</span></tt>, but not after <tt class="docutils literal"><span class="pre">mod_accesslog</span></tt>. You'll
probably want <tt class="docutils literal"><span class="pre">mod_alias</span></tt> as well, for serving admin media.</p>
<p>Add the following to your lighttpd config file:</p>
<div class="highlight-lua">
<div class="highlight">
<pre><span class="n">server</span><span class="p">.</span><span class="n">document</span><span class="o">-</span><span class="n">root</span> <span class="o">=</span> <span class="s2">"</span><span class="s">/home/user/public_html"</span><br />
<span class="n">fastcgi</span><span class="p">.</span><span class="n">server</span> <span class="o">=</span> <span class="p">(</span><br />
<span class="s2">"</span><span class="s">/mysite.fcgi"</span> <span class="o">=&gt;</span> <span class="p">(</span><br />
<span class="s2">"</span><span class="s">main"</span> <span class="o">=&gt;</span> <span class="p">(</span><br />
<span class="o">#</span> <span class="n">Use</span> <span class="n">host</span> <span class="o">/</span> <span class="n">port</span> <span class="n">instead</span> <span class="n">of</span> <span class="n">socket</span> <span class="k">for</span> <span class="n">TCP</span> <span class="n">fastcgi</span><br />
<span class="o">#</span> <span class="s2">"</span><span class="s">host"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">127.0.0.1"</span><span class="p">,</span><br />
<span class="o">#</span> <span class="s2">"</span><span class="s">port"</span> <span class="o">=&gt;</span> <span class="mi">3033</span><span class="p">,</span><br />
<span class="s2">"</span><span class="s">socket"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">/home/user/mysite.sock"</span><span class="p">,</span><br />
<span class="s2">"</span><span class="s">check-local"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">disable"</span><span class="p">,</span><br />
<span class="p">)</span><br />
<span class="p">),</span><br />
<span class="p">)</span><br />
<span class="n">alias</span><span class="p">.</span><span class="n">url</span> <span class="o">=</span> <span class="p">(</span><br />
<span class="s2">"</span><span class="s">/media"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">/home/user/django/contrib/admin/media/"</span><span class="p">,</span><br />
<span class="p">)</span><br />
<br />
<span class="n">url</span><span class="p">.</span><span class="n">rewrite</span><span class="o">-</span><span class="n">once</span> <span class="o">=</span> <span class="p">(</span><br />
<span class="s2">"</span><span class="s">^(/media.*)$"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">$1"</span><span class="p">,</span><br />
<span class="s2">"</span><span class="s">^/favicon\.ico$"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">/media/favicon.ico"</span><span class="p">,</span><br />
<span class="s2">"</span><span class="s">^(/.*)$"</span> <span class="o">=&gt;</span> <span class="s2">"</span><span class="s">/mysite.fcgi$1"</span><span class="p">,</span><br />
<span class="p">)</span><br />
</pre>
</div>
</div>
<div class="section" id="s-running-multiple-django-sites-on-one-lighttpd">
<h3>Running multiple Django sites on one lighttpd<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#running-multiple-django-sites-on-one-lighttpd" title="Permalink to this headline">&#182;</a></h3>
<p>lighttpd lets you use "conditional configuration" to allow configuration to be
customized per host. To specify multiple FastCGI sites, just add a conditional
block around your FastCGI config for each site:</p>
<div class="highlight-bash">
<div class="highlight">
<pre><span class="c"># If the hostname is 'www.example1.com'...</span><br />
<span class="nv">$HTTP</span><span class="o">[</span><span class="s2">"host"</span><span class="o">]</span> <span class="o">==</span> <span class="s2">"www.example1.com"</span> <span class="o">{</span><br />
server.document-root <span class="o">=</span> <span class="s2">"/foo/site1"</span><br />
fastcgi.server <span class="o">=</span> <span class="o">(</span><br />
...<br />
<span class="o">)</span><br />
...<br />
<span class="o">}</span><br />
<br />
<span class="c"># If the hostname is 'www.example2.com'...</span><br />
<span class="nv">$HTTP</span><span class="o">[</span><span class="s2">"host"</span><span class="o">]</span> <span class="o">==</span> <span class="s2">"www.example2.com"</span> <span class="o">{</span><br />
server.document-root <span class="o">=</span> <span class="s2">"/foo/site2"</span><br />
fastcgi.server <span class="o">=</span> <span class="o">(</span><br />
...<br />
<span class="o">)</span><br />
...<br />
<span class="o">}</span><br />
</pre>
</div>
</div>
<p>You can also run multiple Django installations on the same site simply by
specifying multiple entries in the <tt class="docutils literal"><span class="pre">fastcgi.server</span></tt> directive. Add one
FastCGI host for each.</p>
</div>
</div>
<div class="section" id="s-cherokee-setup">
<h2>Cherokee setup<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#cherokee-setup" title="Permalink to this headline">&#182;</a></h2>
<p>Cherokee is a very fast, flexible and easy to configure Web Server. It
supports the widespread technologies nowadays: FastCGI, SCGI, PHP, CGI, SSI,
TLS and SSL encrypted connections, Virtual hosts, Authentication, on the fly
encoding, Load Balancing, Apache compatible log files, Data Base Balancer,
Reverse HTTP Proxy and much more.</p>
<p>The Cherokee project provides a documentation to <a class="reference external" href="http://www.cherokee-project.com/doc/cookbook_django.html">setting up Django</a> with Cherokee.</p>
</div>
<div class="section" id="s-running-django-on-a-shared-hosting-provider-with-apache">
<h2>Running Django on a shared-hosting provider with Apache<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#running-django-on-a-shared-hosting-provider-with-apache" title="Permalink to this headline">&#182;</a></h2>
<p>Many shared-hosting providers don't allow you to run your own server daemons or
edit the <tt class="docutils literal"><span class="pre">httpd.conf</span></tt> file. In these cases, it's still possible to run Django
using Web server-spawned processes.</p>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">If you're using Web server-spawned processes, as explained in this section,
there's no need for you to start the FastCGI server on your own. Apache
will spawn a number of processes, scaling as it needs to.</p>
</div>
<p>In your Web root directory, add this to a file named <tt class="docutils literal"><span class="pre">.htaccess</span></tt>:</p>
<div class="highlight-apache">
<div class="highlight">
<pre><span class="nb">AddHandler</span> fastcgi-script .fcgi<br />
<span class="nb">RewriteEngine</span> <span class="k">On</span><br />
<span class="nb">RewriteCond</span> %{REQUEST_FILENAME} !-f<br />
<span class="nb">RewriteRule</span> ^(.*)$ mysite.fcgi/$1 [QSA,L]<br />
</pre>
</div>
</div>
<p>Then, create a small script that tells Apache how to spawn your FastCGI
program. Create a file <tt class="docutils literal"><span class="pre">mysite.fcgi</span></tt> and place it in your Web directory, and
be sure to make it executable:</p>
<div class="highlight-python">
<div class="highlight">
<pre><span class="c">#!/usr/bin/python</span><br />
<span class="k">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span><br />
<br />
<span class="c"># Add a custom Python path.</span><br />
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span> <span class="s">"/home/user/python"</span><span class="p">)</span><br />
<br />
<span class="c"># Switch to the directory of your project. (Optional.)</span><br />
<span class="c"># os.chdir("/home/user/myproject")</span><br />
<br />
<span class="c"># Set the DJANGO_SETTINGS_MODULE environment variable.</span><br />
<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">'DJANGO_SETTINGS_MODULE'</span><span class="p">]</span> <span class="o">=</span> <span class="s">"myproject.settings"</span><br />
<br />
<span class="k">from</span> <span class="nn">django.core.servers.fastcgi</span> <span class="k">import</span> <span class="n">runfastcgi</span><br />
<span class="n">runfastcgi</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s">"threaded"</span><span class="p">,</span> <span class="n">daemonize</span><span class="o">=</span><span class="s">"false"</span><span class="p">)</span><br />
</pre>
</div>
</div>
<div class="section" id="s-restarting-the-spawned-server">
<h3>Restarting the spawned server<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#restarting-the-spawned-server" title="Permalink to this headline">&#182;</a></h3>
<p>If you change any Python code on your site, you'll need to tell FastCGI the
code has changed. But there's no need to restart Apache in this case. Rather,
just reupload <tt class="docutils literal"><span class="pre">mysite.fcgi</span></tt>, or edit the file, so that the timestamp on the
file will change. When Apache sees the file has been updated, it will restart
your Django application for you.</p>
<p>If you have access to a command shell on a Unix system, you can accomplish this
easily by using the <tt class="docutils literal"><span class="pre">touch</span></tt> command:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>touch mysite.fcgi<br />
</pre>
</div>
</div>
</div>
</div>
<div class="section" id="s-serving-admin-media-files">
<h2>Serving admin media files<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#serving-admin-media-files" title="Permalink to this headline">&#182;</a></h2>
<p>Regardless of the server and configuration you eventually decide to use, you
will also need to give some thought to how to serve the admin media files. The
advice given in the <a class="reference external" href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#serving-the-admin-files"><em>modpython</em></a> documentation
is also applicable in the setups detailed above.</p>
</div>
<div class="section" id="s-forcing-the-url-prefix-to-a-particular-value">
<h2>Forcing the URL prefix to a particular value<a class="headerlink" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#forcing-the-url-prefix-to-a-particular-value" title="Permalink to this headline">&#182;</a></h2>
<p>Because many of these fastcgi-based solutions require rewriting the URL at
some point inside the webserver, the path information that Django sees may not
resemble the original URL that was passed in. This is a problem if the Django
application is being served from under a particular prefix and you want your
URLs from the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></tt> tag to look like the prefix, rather than the
rewritten version, which might contain, for example, <tt class="docutils literal"><span class="pre">mysite.fcgi</span></tt>.</p>
<p>Django makes a good attempt to work out what the real script name prefix
should be. In particular, if the webserver sets the <tt class="docutils literal"><span class="pre">SCRIPT_URL</span></tt> (specific
to Apache's mod_rewrite), or <tt class="docutils literal"><span class="pre">REDIRECT_URL</span></tt> (set by a few servers, including
Apache + mod_rewrite in some situations), Django will work out the original
prefix automatically.</p>
<p>In the cases where Django cannot work out the prefix correctly and where you
want the original value to be used in URLs, you can set the
<tt class="docutils literal"><span class="pre">FORCE_SCRIPT_NAME</span></tt> setting in your main <tt class="docutils literal"><span class="pre">settings</span></tt> file. This sets the
script name uniformly for every URL served via that settings file. Thus you'll
need to use different settings files if you want different sets of URLs to
have different script names in this case, but that is a rare situation.</p>
<p>As an example of how to use it, if your Django configuration is serving all of
the URLs under <tt class="docutils literal"><span class="pre">'/'</span></tt> and you wanted to use this setting, you would set
<tt class="docutils literal"><span class="pre">FORCE_SCRIPT_NAME</span> <span class="pre">=</span> <span class="pre">''</span></tt> in your settings file.</p>
</div>
<div id="content-secondary">
<h2 id="comments">Questions/Feedback</h2>
<p>Having trouble? We'd like to help!</p>
<ul>
    <li>
    Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
    questions.
    </li>
    <li>
    Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
    django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
    Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
    channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
    logs</a> to see if its been asked before.
    </li>
    <li>
    If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
    open a ticket</a> and let us know! Please only use the ticket tracker for
    criticisms and improvements on the docs. For tech support, use the
    resources above.
    </li>
</ul>
原帖地址： http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/<br />
</div>
<img src ="http://www.blogjava.net/sealyu/aggbug/264942.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-04-10 23:39 <a href="http://www.blogjava.net/sealyu/archive/2009/04/10/264942.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Flex+PyAmf+Django+MySQL（转）</title><link>http://www.blogjava.net/sealyu/archive/2009/01/10/250804.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Sat, 10 Jan 2009 05:51:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/01/10/250804.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/250804.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/01/10/250804.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/250804.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/250804.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: v":* {behavior:url(#default#VML);}o":* {behavior:url(#default#VML);}w":* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);}Normal07.8 磅02falsefalsefalseMicro...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2009/01/10/250804.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/250804.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-01-10 13:51 <a href="http://www.blogjava.net/sealyu/archive/2009/01/10/250804.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>windows下Django及相关工具的安装和配置</title><link>http://www.blogjava.net/sealyu/archive/2009/01/09/250685.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 09 Jan 2009 06:12:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2009/01/09/250685.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/250685.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2009/01/09/250685.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/250685.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/250685.html</trackback:ping><description><![CDATA[作者：sealyu&nbsp; 日期：2009-01-09<br />
<br />
网上看到很多这方面的帖子，自己也安装了几次，结合自己的几次安装过程总结一篇备用。以后随着学习深入，逐步完善。<br />
主要涉及到Python、PIL、Apache插件、Mysql插件、Pydev、PyAMF等几个相关工具的安装和配置。（假定已安装好Apache和Mysql）<br />
<br />
<strong>1.安装Python</strong><br />
<a href="http://www.python.org/">http://www.python.org</a>上下载，（现在已经出了3.0版本了，不过跟2.x版本不兼容）点击安装。例如安装到D:\Python\这个目录。<br />
设置环境变量：新建PYTHON_HOME，设置为 D:\Python. Path中添加;%PYTHON_HOME%;%PYTHON_HOME%\Scripts;<br />
<br />
<strong>2.安装PIL\Mysql的python插件\Apache的python插件</strong><br />
<a href="http://www.pythonware.com/products/pil/">http://www.pythonware.com/products/pil/</a> 下载PIL的安装文件<br />
<a href="http://sourceforge.net/projects/mysql-python">http://sourceforge.net/projects/mysql-python</a> 下载mysql-python<br />
http://httpd.apache.org/modules/python-download.cgi 下载mod-python<br />
<br />
PIL是Python Imaging Library缩写，主要用来处理图片用的，这里安装它是为了在Django系统中用它来对上传的图片自动的生成缩略图的。安装方法也相当的简单，直接双击运行安装文件，然后一路next即可。<br />
<br />
mysql-python是Mysql的python插件，直接运行安装。<br />
这两个都将安装在%PYTHON_HOME%\Lib\site-packages下面。<br />
<br />
mod-python是Apache的python插件，如果你选择使用Apache作为服务器，将需要安装此文件，<br />
在下载mod_python的时候要注意与apache版本的匹配，安装过程需要你选择apache的安装目录。<br />
详细的配置可以看这个：<a id="viewpost1_TitleUrl" href="../../sealyu/archive/2008/11/06/239122.html">Django在使用mod_python+apache中配置VirtualHost</a> 或者 <a id="viewpost1_TitleUrl" href="../../sealyu/archive/2008/11/06/239110.html">Apache + mod_python + Django Step by Step</a><br />
<br />
<strong>3.安装Django</strong><br />
http://www.djangoproject.com/ 下载django安装文件。<br />
（现在是1.0.2版本，我曾试图用python2.6上安装django1.0，提示不匹配。所以现在我还在使用python2.5.）<br />
将安装文件解压到一个目录，例如 D:\Django , 打开cmd，cd进这个目录<br />
执行 python setup.py install 将会把Django安装到%PYTHON_HOME%\Lib\site-packages下面并同时设置好sys.path。<br />
设置环境变量，将%PYTHON_HOME\Libsite-packages\django添加到path中。<br />
<br />
<strong>4.安装Pydev（Python的eclipse插件）</strong><br />
<a href="http://sourceforge.net/projects/pydev/"><span style="font-size: 10.5pt; text-decoration: underline;">http://sourceforge.net/projects/pydev/ </span></a>下载安装文件<br />
<p style="text-align: justify;"><span style="font-size: 10.5pt;"><font face="宋体">下载后解压，将plugins目录下所有文件挪到Eclipse的相应目录下，将feature目录下所有文件挪到Eclipse的相应目录下，最后启动
Eclipse，在Help－》Software Upates－》Manage
Configuration，然后弹出的界面即可看到pydev的插件。</font> </span></p>
<p style="text-align: justify;"><span style="font-size: 10.5pt;"><font face="宋体">另一种安装插件的方法是：Help－》Software Upates－》Find and Install－》Search for new features to install-》New remote site－》随便起个name如pydev，url填</font> </span><a href="http://pydev.sf.net/updates/"><span style="font-size: 10.5pt; text-decoration: underline;">http://pydev.sf.net/updates/ </span></a><span style="font-size: 10.5pt;"><font face="宋体">－》然后照着提示下载安装即可。</font></span></p>
<p style="text-align: justify;"><span style="font-size: 10.5pt;"><font face="宋体">配置Pydev：</font></span></p>
<p style="text-align: justify;"><span style="font-size: 10.5pt;"><font face="宋体"><span style="font-size: 10.5pt;"><font face="宋体">在Eclipse IDE</font> </span><span style="font-size: 10.5pt;"><font face="宋体">下，</font> </span><span style="font-size: 10.5pt;"><font face="宋体">打开</font> </span><span style="font-weight: bold; font-size: 10.5pt;">Window-&gt;Preferences</span> <span style="font-size: 10.5pt;"><font face="宋体">对话框，从右侧的树形列表中选择&#8220;</font> </span><span style="font-weight: bold; font-size: 10.5pt;">PyDev&#8221;-&gt;&#8220;Interpreter Python&#8221;<font face="宋体">，</font> </span><span style="font-size: 10.5pt;"><font face="宋体">点击New按钮，从Python的安装路径下选择Python.exe，然后会弹出一个对话框让你勾选System PYTHONPATH。最后点Ok，退出。</font></span></font></span></p>
<br />
<br />
<br />
<img src ="http://www.blogjava.net/sealyu/aggbug/250685.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2009-01-09 14:12 <a href="http://www.blogjava.net/sealyu/archive/2009/01/09/250685.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 绝对简明手册(转自-啄木鸟社区)</title><link>http://www.blogjava.net/sealyu/archive/2008/12/29/248902.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 29 Dec 2008 06:50:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/12/29/248902.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/248902.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/12/29/248902.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/248902.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/248902.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 今天在啄木鸟社区看到这篇帖子，感觉挺简洁的，mark一下： Python 绝对简明手册 -- zsp007@gmail.com ::-- ZoomQuiet [2006-09-15 04:35:33] Contents    阅读须知    基本语法            if / elif / else        in        for ... i...&nbsp;&nbsp;<a href='http://www.blogjava.net/sealyu/archive/2008/12/29/248902.html'>阅读全文</a><img src ="http://www.blogjava.net/sealyu/aggbug/248902.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-12-29 14:50 <a href="http://www.blogjava.net/sealyu/archive/2008/12/29/248902.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PIL 中使用JPEG图片</title><link>http://www.blogjava.net/sealyu/archive/2008/12/11/245799.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 11 Dec 2008 14:26:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/12/11/245799.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/245799.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/12/11/245799.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/245799.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/245799.html</trackback:ping><description><![CDATA[要在PIL（Python Image LIbrary）中支持jpeg格式的图片，<br />
还需安装jpeg库文件，可到<a href="http://www.ijg.org/" target="_top">http://www.ijg.org</a>下载。<br />
安装jpeg库，输入以下命令进行安装：
<pre>tar xfz jpegsrc.v6b.tar.gz<br />
cd jpeg-6b<br />
./configure<br />
make<br />
make test<br />
make install<br />
make install-lib<br />
</pre>
<br />
<br />
<img src ="http://www.blogjava.net/sealyu/aggbug/245799.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-12-11 22:26 <a href="http://www.blogjava.net/sealyu/archive/2008/12/11/245799.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How to use Django with Apache and mod_python</title><link>http://www.blogjava.net/sealyu/archive/2008/11/07/239347.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 07 Nov 2008 15:44:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/07/239347.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/239347.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/07/239347.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/239347.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/239347.html</trackback:ping><description><![CDATA[<p><a external="" href="http://httpd.apache.org/">Apache</a> with <a external="" href="http://www.modpython.org/">mod_python</a> currently is the preferred setup for using Django
on a production server.</p>
<p>mod_python is similar to (and inspired by) <a external="" href="http://perl.apache.org/">mod_perl</a> : It embeds Python within
Apache and loads Python code into memory when the server starts. Code stays in
memory throughout the life of an Apache process, which leads to significant
performance gains over other server arrangements.</p>
<p>Django requires Apache 2.x and mod_python 3.x, and you should use Apache&#8217;s
<a external="" href="http://httpd.apache.org/docs/2.2/mod/prefork.html">prefork MPM</a>, as opposed to the <a external="" href="http://httpd.apache.org/docs/2.2/mod/worker.html">worker MPM</a>.</p>
<p>You may also be interested in <a external="" href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#howto-deployment-fastcgi"><em>How to use Django with FastCGI, SCGI or AJP</em></a> (which also covers SCGI and AJP).</p>
<div id="s-basic-configuration">
<h2 id="basic-configuration">Basic configuration<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#basic-configuration" title="Permalink to this headline">&#182;</a></h2>
<p>To configure Django with mod_python, first make sure you have Apache installed,
with the mod_python module activated.</p>
<p>Then edit your <tt literal="">httpd.conf</tt> file and add the following:</p>
<div>
<pre>&lt;Location "/mysite/"&gt;<br />
SetHandler python-program<br />
PythonHandler django.core.handlers.modpython<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.settings<br />
PythonOption django.root /mysite<br />
PythonDebug On<br />
&lt;/Location&gt;<br />
</pre>
</div>
<p>...and replace <tt literal="">mysite.settings</tt> with the Python import path to your Django
project's settings file.</p>
<p>This tells Apache: "Use mod_python for any URL at or under '/mysite/', using the
Django mod_python handler." It passes the value of <a external="" href="http://docs.djangoproject.com/en/dev/topics/settings/#django-settings-module"><em>DJANGO_SETTINGS_MODULE</em></a> so mod_python knows which settings to use.</p>
<div>
New in Django 1.0: The <tt literal="">PythonOption django.root ...</tt> is new in this version.</div>
<p>Because mod_python does not know we are
serving this site from underneath the <tt literal="">/mysite/</tt> prefix, this value needs to
be passed through to the mod_python handler in Django, via the <tt literal="">PythonOption
django.root ...</tt> line. The value set on that line (the last item) should
match the string given in the <tt literal="">&lt;Location ...&gt;</tt> directive. The effect of this
is that Django will automatically strip the <tt literal="">/mysite</tt> string from the front
of any URLs before matching them against your <tt literal="">URLConf</tt> patterns. If you
later move your site to live under <tt literal="">/mysite2</tt>, you will not have to change
anything except the <tt literal="">django.root</tt> option in the config file.</p>
<p>When using <tt literal="">django.root</tt> you should make sure that what's left, after the
prefix has been removed, begins with a slash. Your URLConf patterns that are
expecting an initial slash will then work correctly. In the above example,
since we want to send things like <tt literal="">/mysite/admin/</tt> to <tt literal="">/admin/</tt>, we need
to remove the string <tt literal="">/mysite</tt> from the beginning, so that is the
<tt literal="">django.root</tt> value. It would be an error to use <tt literal="">/mysite/</tt> (with a
trailing slash) in this case.</p>
<p>Note that we're using the <tt literal="">&lt;Location&gt;</tt> directive, not the <tt literal="">&lt;Directory&gt;</tt>
directive. The latter is used for pointing at places on your filesystem,
whereas <tt literal="">&lt;Location&gt;</tt> points at places in the URL structure of a Web site.
<tt literal="">&lt;Directory&gt;</tt> would be meaningless here.</p>
<p>Also, if your Django project is not on the default <tt literal="">PYTHONPATH</tt> for your
computer, you'll have to tell mod_python where your project can be found:</p>
<pre>&lt;Location "/mysite/"&gt;<br />
SetHandler python-program<br />
PythonHandler django.core.handlers.modpython<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.settings<br />
PythonOption django.root /mysite<br />
PythonDebug On<br />
<strong>PythonPath "['/path/to/project'] + sys.path"</strong><br />
&lt;/Location&gt;<br />
</pre>
<p>The value you use for <tt literal="">PythonPath</tt> should include the parent directories of
all the modules you are going to import in your application. It should also
include the parent directory of the <a external="" href="http://docs.djangoproject.com/en/dev/topics/settings/#django-settings-module"><em>DJANGO_SETTINGS_MODULE</em></a> location. This is exactly the same situation as
setting the Python path for interactive usage. Whenever you try to import
something, Python will run through all the directories in <tt literal="">sys.path</tt> in turn,
from first to last, and try to import from each directory until one succeeds.</p>
<p>An example might make this clearer. Suppose you have some applications under
<tt literal="">/usr/local/django-apps/</tt> (for example, <tt literal="">/usr/local/django-apps/weblog/</tt> and
so forth), your settings file is at <tt literal="">/var/www/mysite/settings.py</tt> and you have
specified <a external="" href="http://docs.djangoproject.com/en/dev/topics/settings/#django-settings-module"><em>DJANGO_SETTINGS_MODULE</em></a> as in the above
example. In this case, you would need to write your <tt literal="">PythonPath</tt> directive
as:</p>
<div>
<pre>PythonPath "['/usr/local/django-apps/', '/var/www'] + sys.path"<br />
</pre>
</div>
<p>With this path, <tt literal="">import weblog</tt> and <tt literal="">import mysite.settings</tt> will both
work. If you had <tt literal="">import blogroll</tt> in your code somewhere and <tt literal="">blogroll</tt>
lived under the <tt literal="">weblog/</tt> directory, you would <em>also</em> need to add
<tt literal="">/usr/local/django-apps/weblog/</tt> to your <tt literal="">PythonPath</tt>. Remember: the
<strong>parent directories</strong> of anything you import directly must be on the Python
path.</p>
<div note="">
<p admonition-title="">Note</p>
<p>If you're using Windows, we still recommended that you use forward
slashes in the pathnames, even though Windows normally uses the backslash
character as its native separator. Apache knows how to convert from the
forward slash format to the native format, so this approach is portable and
easier to read. (It avoids tricky problems with having to double-escape
backslashes.)</p>
<p>This is valid even on a Windows system:</p>
<div>
<pre>PythonPath "['c:/path/to/project'] + sys.path"<br />
</pre>
</div>
</div>
<p>You can also add directives such as <tt literal="">PythonAutoReload Off</tt> for performance.
See the <a external="" href="http://modpython.org/live/current/doc-html/directives.html">mod_python documentation</a> for a full list of options.</p>
<p>Note that you should set <tt literal="">PythonDebug Off</tt> on a production server. If you
leave <tt literal="">PythonDebug On</tt>, your users would see ugly (and revealing) Python
tracebacks if something goes wrong within mod_python.</p>
<p>Restart Apache, and any request to <tt literal="">/mysite/</tt> or below will be served by
Django. Note that Django's URLconfs won't trim the "/mysite/" -- they get passed
the full URL.</p>
<p>When deploying Django sites on mod_python, you'll need to restart Apache each
time you make changes to your Python code.</p>
</div>
<div id="s-multiple-django-installations-on-the-same-apache">
<h2 id="multiple-django-installations-on-the-same-apache">Multiple Django installations on the same Apache<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#multiple-django-installations-on-the-same-apache" title="Permalink to this headline">&#182;</a></h2>
<p>It's entirely possible to run multiple Django installations on the same Apache
instance. Just use <tt literal="">VirtualHost</tt> for that, like so:</p>
<div>
<pre>NameVirtualHost *<br />
<br />
&lt;VirtualHost *&gt;<br />
ServerName www.example.com<br />
# ...<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.settings<br />
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost *&gt;<br />
ServerName www2.example.com<br />
# ...<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings<br />
&lt;/VirtualHost&gt;<br />
</pre>
</div>
<p>If you need to put two Django installations within the same <tt literal="">VirtualHost</tt>
(or in different <tt literal="">VirtualHost</tt> blocks that share the same server name),
you'll need to take a special precaution to ensure mod_python's cache doesn't
mess things up. Use the <tt literal="">PythonInterpreter</tt> directive to give different
<tt literal="">&lt;Location&gt;</tt> directives separate interpreters:</p>
<div>
<pre>&lt;VirtualHost *&gt;<br />
ServerName www.example.com<br />
# ...<br />
&lt;Location "/something"&gt;<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.settings<br />
PythonInterpreter mysite<br />
&lt;/Location&gt;<br />
<br />
&lt;Location "/otherthing"&gt;<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings<br />
PythonInterpreter othersite<br />
&lt;/Location&gt;<br />
&lt;/VirtualHost&gt;<br />
</pre>
</div>
<p>The values of <tt literal="">PythonInterpreter</tt> don't really matter, as long as they're
different between the two <tt literal="">Location</tt> blocks.</p>
</div>
<div id="s-running-a-development-server-with-mod-python">
<h2 id="running-a-development-server-with-mod-python">Running a development server with mod_python<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#running-a-development-server-with-mod-python" title="Permalink to this headline">&#182;</a></h2>
<p>If you use mod_python for your development server, you can avoid the hassle of
having to restart the server each time you make code changes. Just set
<tt literal="">MaxRequestsPerChild 1</tt> in your <tt literal="">httpd.conf</tt> file to force Apache to reload
everything for each request. But don't do that on a production server, or we'll
revoke your Django privileges.</p>
<p>If you're the type of programmer who debugs using scattered <tt literal="">print</tt>
statements, note that <tt literal="">print</tt> statements have no effect in mod_python; they
don't appear in the Apache log, as one might expect. If you have the need to
print debugging information in a mod_python setup, either do this:</p>
<div>
<pre>assert False, the_value_i_want_to_see<br />
</pre>
</div>
<p>Or add the debugging information to the template of your page.</p>
</div>
<div id="s-id1">
<h2 id="id1">Serving media files<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#id1" title="Permalink to this headline">&#182;</a></h2>
<p>Django doesn't serve media files itself; it leaves that job to whichever Web
server you choose.</p>
<p>We recommend using a separate Web server -- i.e., one that's not also running
Django -- for serving media. Here are some good choices:</p>
<ul>
    <li><a external="" href="http://www.lighttpd.net/">lighttpd</a></li>
    <li><a external="" href="http://en.wikipedia.org/wiki/TUX_web_server">TUX</a></li>
    <li>A stripped-down version of <a external="" href="http://httpd.apache.org/">Apache</a></li>
</ul>
<p>If, however, you have no option but to serve media files on the same Apache
<tt literal="">VirtualHost</tt> as Django, here's how you can turn off mod_python for a
particular part of the site:</p>
<div>
<pre>&lt;Location "/media"&gt;<br />
SetHandler None<br />
&lt;/Location&gt;<br />
</pre>
</div>
<p>Just change <tt literal="">Location</tt> to the root URL of your media files. You can also use
<tt literal="">&lt;LocationMatch&gt;</tt> to match a regular expression.</p>
<p>This example sets up Django at the site root but explicitly disables Django for
the <tt literal="">media</tt> subdirectory and any URL that ends with <tt literal="">.jpg</tt>, <tt literal="">.gif</tt> or
<tt literal="">.png</tt>:</p>
<div>
<pre>&lt;Location "/"&gt;<br />
SetHandler python-program<br />
PythonHandler django.core.handlers.modpython<br />
SetEnv DJANGO_SETTINGS_MODULE mysite.settings<br />
&lt;/Location&gt;<br />
<br />
&lt;Location "/media"&gt;<br />
SetHandler None<br />
&lt;/Location&gt;<br />
<br />
&lt;LocationMatch "".(jpg|gif|png)$"&gt;<br />
SetHandler None<br />
&lt;/LocationMatch&gt;<br />
</pre>
</div>
</div>
<div id="s-id3">
<h2 id="id3">Serving the admin files<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#id3" title="Permalink to this headline">&#182;</a></h2>
<p>Note that the Django development server automagically serves admin media files,
but this is not the case when you use any other server arrangement. You're
responsible for setting up Apache, or whichever media server you're using, to
serve the admin files.</p>
<p>The admin files live in (<tt literal="">django/contrib/admin/media</tt>) of the Django
distribution.</p>
<p>Here are two recommended approaches:</p>
<ol simple="">
    <li>Create a symbolic link to the admin media files from within your
    document root. This way, all of your Django-related files -- code <strong>and</strong>
    templates -- stay in one place, and you'll still be able to <tt literal="">svn
    update</tt> your code to get the latest admin templates, if they change.</li>
    <li>Or, copy the admin media files so that they live within your Apache
    document root.</li>
</ol>
</div>
<div id="s-using-eggs-with-mod-python">
<h2 id="using-eggs-with-mod-python">Using "eggs" with mod_python<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#using-eggs-with-mod-python" title="Permalink to this headline">&#182;</a></h2>
<p>If you installed Django from a Python <a external="" href="http://peak.telecommunity.com/DevCenter/PythonEggs">egg</a> or are using eggs in your Django
project, some extra configuration is required. Create an extra file in your
project (or somewhere else) that contains something like the following:</p>
<div>
<pre>import os<br />
os.environ['PYTHON_EGG_CACHE'] = '/some/directory'<br />
</pre>
</div>
<p>Here, <tt literal="">/some/directory</tt> is a directory that the Apache webserver process can
write to. It will be used as the location for any unpacking of code the eggs
need to do.</p>
<p>Then you have to tell mod_python to import this file before doing anything
else. This is done using the <a external="" href="http://www.modpython.org/live/current/doc-html/dir-other-pimp.html">PythonImport</a> directive to mod_python. You need
to ensure that you have specified the <tt literal="">PythonInterpreter</tt> directive to
mod_python as described <a internal="" href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#multiple-django-installations-on-the-same-apache">above</a> (you need to do this even if you aren't
serving multiple installations in this case). Then add the <tt literal="">PythonImport</tt>
line in the main server configuration (i.e., outside the <tt literal="">Location</tt> or
<tt literal="">VirtualHost</tt> sections). For example:</p>
<div>
<pre>PythonInterpreter my_django<br />
PythonImport /path/to/my/project/file.py my_django<br />
</pre>
</div>
<p>Note that you can use an absolute path here (or a normal dotted import path),
as described in the <a external="" href="http://www.modpython.org/live/current/doc-html/dir-other-pimp.html">mod_python manual</a>. We use an absolute path in the
above example because if any Python path modifications are required to access
your project, they will not have been done at the time the <tt literal="">PythonImport</tt>
line is processed.</p>
</div>
<div id="s-error-handling">
<h2 id="error-handling">Error handling<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#error-handling" title="Permalink to this headline">&#182;</a></h2>
<p>When you use Apache/mod_python, errors will be caught by Django -- in other
words, they won't propagate to the Apache level and won't appear in the Apache
<tt literal="">error_log</tt>.</p>
<p>The exception for this is if something is really wonky in your Django setup. In
that case, you'll see an "Internal Server Error" page in your browser and the
full Python traceback in your Apache <tt literal="">error_log</tt> file. The <tt literal="">error_log</tt>
traceback is spread over multiple lines. (Yes, this is ugly and rather hard to
read, but it's how mod_python does things.)</p>
</div>
<div id="s-if-you-get-a-segmentation-fault">
<h2 id="if-you-get-a-segmentation-fault">If you get a segmentation fault<a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/?from=olddocs#if-you-get-a-segmentation-fault" title="Permalink to this headline">&#182;</a></h2>
<p>If Apache causes a segmentation fault, there are two probable causes, neither
of which has to do with Django itself.</p>
<ol simple="">
    <li>It may be because your Python code is importing the "pyexpat" module,
    which may conflict with the version embedded in Apache. For full
    information, see <a external="" href="http://www.dscpl.com.au/articles/modpython-006.html">Expat Causing Apache Crash</a>.</li>
    <li>It may be because you're running mod_python and mod_php in the same
    Apache instance, with MySQL as your database backend. In some cases,
    this causes a known mod_python issue due to version conflicts in PHP and
    the Python MySQL backend. There's full information in the
    <a external="" href="http://modpython.org/FAQ/faqw.py?req=show&amp;file=faq02.013.htp">mod_python FAQ entry</a>.</li>
</ol>
<p>If you continue to have problems setting up mod_python, a good thing to do is
get a barebones mod_python site working, without the Django framework. This is
an easy way to isolate mod_python-specific problems. <a external="" href="http://www.dscpl.com.au/articles/modpython-001.html">Getting mod_python Working</a>
details this procedure.</p>
<p>The next step should be to edit your test code and add an import of any
Django-specific code you're using -- your views, your models, your URLconf,
your RSS configuration, etc. Put these imports in your test handler function
and access your test URL in a browser. If this causes a crash, you've confirmed
it's the importing of Django code that causes the problem. Gradually reduce the
set of imports until it stops crashing, so as to find the specific module that
causes the problem. Drop down further into modules and look into their imports,
as necessary.</p>
</div>
<img src ="http://www.blogjava.net/sealyu/aggbug/239347.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-07 23:44 <a href="http://www.blogjava.net/sealyu/archive/2008/11/07/239347.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在RedHat AS4 上安装Mysql_python模块时遇到的问题</title><link>http://www.blogjava.net/sealyu/archive/2008/11/07/239288.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 07 Nov 2008 09:03:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/07/239288.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/239288.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/07/239288.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/239288.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/239288.html</trackback:ping><description><![CDATA[作者：sealyu 2008-11-7<br />
<br />
在RedHat AS4 上配置Apache+MySQL+Django 环境时，首先安装好了Apache和Python以及Django和Mysql，<br />
其中Mysql是编译安装的5.0版本。<br />
下载了MySQL-python-1.2.2.tar.gz之后，解压缩，cd进去，<br />
执行： python setup.py build时报错：<br />
<pre>File "/tmp/easy_install-nHSsgl/MySQL-python-1.2.2/setup_posix.py", line 26, in mysql_config<br />
EnvironmentError: mysql_config not found<br />
<br />
后来发现是由于Mysql编译安装后没有 mysql_config这个值，解决方法：<br />
打开 setup_posix.py, 将其中line：26手动改成系统中对应的Mysql选项（这里我的是/usr/local/mysql）：<br />
mysql_config = /usr/local/mysql/bin/mysql_config<br />
<br />
重新执行 ：python setup.py build，没有了刚才的错误，但是出现了另外一个错误：<br />
error： /usr/bin/ld: cannot find -lmysqlclient<br />
<br />
网上搜索了一下这个错误，发现有几种不同的情况，主要有以下几个原因：<br />
1.没有安装mysqlclient。解决方法：找到对应的版本进行安装。<br />
2.安装的mysqlclient的版本不匹配。对应链接： http://www.hao32.com/webserver/258.html<br />
3.已经安装了对应的mysqlclient但是找不到对应的链接。这是在一个国外的网站上看到的，具体网址已经找不到了，后来那位仁兄将对应的<br />
mysql_home/lib/mysql文件夹下面libmysqlclient对应的文件全部拷贝到/usr/local/lib下面才解决了问题。<br />
<br />
按照对应方案，问题解决。<br />
重新执行： <br />
python setup.py build<br />
python setup.py install<br />
安装完成。<br />
<br />
<br />
</pre>
<img src ="http://www.blogjava.net/sealyu/aggbug/239288.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-07 17:03 <a href="http://www.blogjava.net/sealyu/archive/2008/11/07/239288.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在linux上配置Django＋LIghthttpd ＋ fastcgi</title><link>http://www.blogjava.net/sealyu/archive/2008/11/06/239127.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 06 Nov 2008 14:36:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/06/239127.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/239127.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/06/239127.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/239127.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/239127.html</trackback:ping><description><![CDATA[<p>In today&#8217;s world, web development is all about turnaround.
Businesses want to maximize production outcome while minimizing
development and production time. Small, lean development teams are
increasingly becoming the normal large development departments. Enter
Django: a popular <a href="http://www.python.org%29/">Python</a> web
framework that invokes the RWAD (rapid web application development) and
DRY (don&#8217;t repeat yourself) principles with clean, pragmatic design.</p>
<p>This article is not about teaching you how to
program in Python, nor how to use the Django framework. It&#8217;s about
showing how to promote your Django applications onto an existing Apache
or Lighttpd environment. </p>
<p>We will conclude with a simple way that you can improve the
performance of your Django application by using caching to speed up
access time. This article also assumes that you are running Fedora as
your web application server, but all the packages mentioned in this
article are also available under the <a href="http://fedoraproject.org/wiki/EPEL">Extra Packages for Enterprise Linux repository </a>, which means these instructions should also be valid under Red Hat Enterprise Linux or CentOS servers.</p>
<h3>What you need</h3>
<p>You must have Django installed:</p>
<pre># yum install Django</pre>
<p>If you want to serve Django apps under Apache:</p>
<pre># yum install httpd<br />
# yum install mod_python</pre>
<p>If you want to serve Django apps under Lighttpd:</p>
<pre># yum install lighttpd<br />
# yum install lighttpd-fastcgi<br />
# yum install python-flup</pre>
<p>Installing memcached to &#8217;speed up&#8217; Django apps:</p>
<pre># yum install memcached<br />
# yum install python-memcached</pre>
<h3>Starting a new Django project</h3>
<p>1. Create a development workspace.</p>
<pre>$ mkdir -p $LOCATION_TO_YOUR_DEV_AREA<br />
$ cd $LOCATION_TO_YOUR_DEV_AREA</pre>
<p>2. Start a new base Django project. This creates the boiler plate project structure.</p>
<pre>$ django-admin.py startproject my_app</pre>
<p>3. Start the Django development web server on port 8080 (or whatever other port you&#8217;d like). </p>
<p>Note: The development web server is just for testing and verification. Do not use it as a production application server!</p>
<pre>$ python manage.py runserver 8080</pre>
<p>4. Run your Django project under Apache with mod_python by enabling mod_python on httpd.conf (/etc/httpd/conf/httpd.conf).</p>
<p>After installing mod_python, a file called python.conf should
already be placed in /etc/httpd/conf.d/, which enables mod_python on
your system.</p>
<p>5. Create virtual hosts by creating a new file at /etc/httpd/conf.d/myapp.conf.</p>
<pre><virtualhost * :80=""> <br />
<br />
DocumentRoot /var/www/html/<br />
ServerName your_domain_name<br />
ErrorLog logs/my_app-error.log<br />
CustomLog logs/my_app-access_log common <br />
<br />
<location my_app=""><br />
SetHandler python-program<br />
PythonHandler django.core.handlers.modpython<br />
SetEnv DJANGO_SETTINGS_MODULE my_app.settings<br />
PythonDebug On<br />
PythonPath &#8220;[&#8217;/var/www/django/&#8217;] + sys.path&#8221;<br />
</location>  <br />
<br />
</virtualhost> </pre>
<h3>Running your Django project under Lighthttpd with fastcgi</h3>
<p>The first thing you must do is start up your FastCGI server.</p>
<pre>./manage.py runfcgi method=prefork socket=/var/www/myapp.sock pidfile=django_myapp.pid</pre>
<p>Then modify your lighttpd.conf file to use the FastCGI server.</p>
<pre>server.document-root = "/var/www/django/"<br />
fastcgi.server = (<br />
"/my_app.fcgi" =&gt; (<br />
"main" =&gt; (<br />
# Use host / port instead of socket for TCP fastcgi<br />
# "host" =&gt; "127.0.0.1",<br />
# "port" =&gt; 3033,<br />
"socket" =&gt; "/var/www/my_app.sock",<br />
"check-local" =&gt; "disable",<br />
)<br />
),<br />
)<br />
alias.url = (<br />
"/media/" =&gt; "/var/www/django/media/",<br />
)<br />
<br />
url.rewrite-once = (<br />
"^(/media.*)$" =&gt; "$1",<br />
"^/favicon.ico$" =&gt; "/media/favicon.ico",<br />
"^(/.*)$" =&gt; "/my_app.fcgi$1",<br />
)</pre>
<p>Setting up caching in Django</p>
<p>Django has many different caching backends, including database,
memory, filesystem, and the ever popular memcached. According to <a href="http://www.danga.com/memcached/">http://www.danga.com/memcached/</a>,
memcached is &#8220;a high-performance, distributed memory object caching
system, generic in nature, but intended for use in speeding up dynamic
web applications by alleviating database load.&#8221; It&#8217;s used by high
traffic sites such as <a href="http://www.slashdot.org/">Slashdot</a> and <a href="http://www.wikipedia.com/">Wikipedia</a>. This makes it a prime candidate for caching in your cool new web app.</p>
<p>First, verify that memcached is running using the memcached&#8217;s init script.</p>
<pre>$ /etc/init.d/memcached status<br />
memcached (pid 6771) is running... </pre>
<p>If it&#8217;s not running, you can manually start it.</p>
<pre>$ /sbin/service memcached start</pre>
<p>If you want to make sure it will automatically start every time after a reboot:</p>
<pre>$ /sbin/chkconfig --level 35 memcached on </pre>
<p>Now that you have verified that memcached is running, you will want
to tell your Django application to use memcached as it&#8217;s caching
backend. You can do this by adding a CACHE_BACKEND entry to your
settings.py file.</p>
<pre>CACHE_BACKEND = 'memcached://127.0.0.1:11211/'</pre>
<p>The format is &#8220;backend://host:port/&#8221; or &#8220;backend:///path&#8221; depending
on the backend chosen. Since we are using memcached, we have the option
to run multiple daemons on different servers and share the cache across
multiple machines. If you want to do this all you must do is add in the
servers:port combinations in the CACHE_BACKEND and separate them by
semicolons. In this example we share the cache across three different
memcached servers:</p>
<pre>CACHE_BACKEND = 'memcached://127.0.0.1:11211;192.168.0.10:11211;192.168.0.11/'</pre>
<p>For more information on the different types of caching that can be performed in the Django framework, please refer to their <a href="http://www.djangoproject.com/documentation/cache/">official documentation</a>. </p>
<img src ="http://www.blogjava.net/sealyu/aggbug/239127.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-06 22:36 <a href="http://www.blogjava.net/sealyu/archive/2008/11/06/239127.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Django在使用mod_python+apache中配置VirtualHost</title><link>http://www.blogjava.net/sealyu/archive/2008/11/06/239122.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 06 Nov 2008 14:13:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/06/239122.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/239122.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/06/239122.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/239122.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/239122.html</trackback:ping><description><![CDATA[<p>With <a href="http://www.djangoproject.com/weblog/2006/jul/29/095/">Django 0.95 being introduced being introduced a few days ago,</a>
I figured a quick post on what we experienced installing Django might
help someone out. None of this covers new ground, but perhaps another
telling of the story will provide someone an extra hook to grab onto.<br />
<br />
This tutorial is going to cover installing Django with the following:</p>
<ul>
    <li>Apache 2</li>
    <li>MySQL</li>
    <li>mod_python</li>
    <li>Python (2.4)</li>
</ul>
<p>For the record, I'm running all of this off of OpenSuSE 10.1 - so obligatory <acronym title="Your Mileage May Vary">YMMV</acronym>. Please comment with suggestions or problems if so, though.</p>
<p>First off, make sure you've got Apache up and running. <tt>ps aux | grep httpd</tt> can tell ya for the most part. If you don't know how to do this much, though, I'm afraid you're reading the wrong tutorial. </p>
<h3>Step 1: Install mod_python</h3>
<p>This will vary pretty widely amongst distributions, but our goal was
to have a package manager do it. One of the below will probably work
for you:</p>
<ul>
    <li>SuSE: In YaST, search for mod_python in Software Management</li>
    <li>Ubuntu/Debian/apt-based: <tt>apt-get install libapache2-mod-python2.4</tt>
    </li>
    <li>Redhat/FC4: <tt>yum install mod_python</tt>
    </li>
</ul>
<h3>Step 2: Install Django</h3>
<p>So with mod_python installed, now we can get to Django. We have been
tracking along with Subversion up until now, but since we're getting
ready to put a large (for us) site into production, we are planning on
sticking with numbered releases, but it remains to be seen if we'll
change our minds on that one in order to take advantage of the
enhancements that continue to stream in. If you want to live closer to
the edge, you can get the <a href="http://www.djangoproject.com/documentation/install/#installing-the-development-version">development trunk through subversion.</a></p>
<div># Most recent version at this point in time is 0.95<br />
wget http://www.djangoproject.com/download/0.95/tarball/<br />
tar -xzvf Django-0.95.tar.gz<br />
cd Django-0.95<br />
python setup.py install</div>
<p>Note that the last command will automatically download and install 'setuptools' if you don't already have it installed.</p>
<h3>Step 3: Create a Project</h3>
<p>So assuming everything went well, (It did, didn't it?), we're ready
to start our first django project. Start your first project by going
into the directory you want to hold your django projects (preferably <em>outside</em> of your web document root), and typing:</p>
<div>django-admin.py startproject myProject</div>
<p>As the <a href="http://www.djangoproject.com/documentation/tutorial1/">djangoproject.com tutorial</a> says&#8230; </p>
<blockquote>
<p>
django-admin.py should be on your system path if you installed Django
via python setup.py. If it's not on your path, you can find it in <tt><em>site-packages</em>/django/bin</tt>,
where site-packages is a directory within your Python installation.
Consider symlinking to django-admin.py from some place on your path,
such as <tt>/usr/local/bin</tt>.
</p>
</blockquote>
<p>This will start you off with the basic settings and files needed for
your django project. But we're not done quite yet! We have to tell
apache what to do to handle mod_python requests.</p>
<h3>Step 4: Configure Apache</h3>
<p>To get apache to handle python requests, we first need to activate
mod_python. Again, this is one of those things that will vary pretty
widely through distributions, but essentially you'll need this command
in Apache's httpd.conf file:</p>
<div>LoadModule python_module modules/mod_python.so</div>
<p>(note: Purportedly on debian this is done for you automatically - so you will likely be able to skip this step)<br />
In OpenSuSE the best way to do this is to go into YaST-&gt; Sysconfig
Editor-&gt; Network-&gt; WWW-&gt; Apache2, and append 'python' onto the
<tt>APACHE_MODULES</tt>
setting. There may be other ways to do this more painlessly than file
editing in other distro's, as well. Let me know if there are and I will
update this section.</p>
<p>Now that apache knows about python, we have to tell it where to
-use- python. So go into your httpd.conf file (if you're using virtual
hosts, pick the appropriate virtual host config file) and add this
location directive:</p>
<div>
<div>&lt;location "/myProject"&gt;<br />
&nbsp; &nbsp; &nbsp;SetHandler python-program<br />
&nbsp; &nbsp; &nbsp;PythonHandler django.core.handlers.modpython<br />
&nbsp; &nbsp; &nbsp;SetEnv DJANGO_SETTINGS_MODULE myProject.settings<br />
&nbsp; &nbsp; &nbsp;PythonPath "['/path/to/your/django/projects'] + sys.path"<br />
&nbsp; &nbsp; &nbsp;PythonDebug On<br />
&nbsp; &nbsp; &nbsp;PythonInterpreter myProject<br />
&nbsp;&lt;/location&gt;</div>
</div>
<p>Now save, restart apache, and browse to your server + /myProject (i.e., http://www.yourhost.com/myProject).</p>
<p>Did it come up with a special looking 404 error? If so, you've got
Django installed, man! Better go follow the more informative and better
written <a href="http://www.djangoproject.com/documentation/tutorial1/">Django Tutorial</a> to see how to really get rolling with it - you're ready to start developing!</p>
<img src ="http://www.blogjava.net/sealyu/aggbug/239122.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-06 22:13 <a href="http://www.blogjava.net/sealyu/archive/2008/11/06/239122.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Apache + mod_python + Django Step by Step</title><link>http://www.blogjava.net/sealyu/archive/2008/11/06/239110.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 06 Nov 2008 12:57:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/06/239110.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/239110.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/06/239110.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/239110.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/239110.html</trackback:ping><description><![CDATA[鉴于网上关于 Python 框架 Django 的 Apache + mod_python + Django 环境详细安装加实例文档还是比较难找到，所以石头在这边写一个文档与初学 Django 的同志们以共勉：）<br />
<p><strong> 1、下载需要模块。</strong><br />
<br />
a、apache (httpd-2.0.54.tar.gz or later)<br />
b、mod_python (mod_python-3.3.1.tgz)<br />
c、Django (Django-0.96.tar.gz)<br />
<br />
默认已安装 Python (<a href="http://www.python.org/">http://www.python.org</a>)<br />
<br />
<strong>2、安装配置环境。</strong><br />
<br />
首先，正常安装 apache 和 mod_python (<a href="http://www.modpython.org/">http://www.modpython.org/</a>),注意要打开 apache 的 DSO 功能，执行：<br />
<br />
#...<br />
#tar -zxvf httpd-2.0.54.tar.gz<br />
#cd httpd-2.0.54<br />
#./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all<br />
#make install clean<br />
#...<br />
#tar -zxvf mod_python-3.3.1.tgz<br />
#cd mod_python-3.3.1<br />
#./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/bin/python<br />
#make install clean<br />
#...<br />
#tar -zxvf Django-0.96.tar.gz<br />
#cd Django-0.96<br />
#python setup.py install (<em>If
you installed Django using setup.py install, uninstalling is as simple
as deleting the django directory from your Python site-packages.</em>)<br />
#...<br />
<br />
这
样我们就安装好 Apache + mod_python + Django 了，你可以执行 apachectl start
测试一下能不能成功。然后我们开始配置环境，首先配置 httpd.conf 加入 LoadModule python_module
modules/mod_python.so (在安装 mod_python 的时候安装程序会自动把 mod_python.so 拷贝到
apache 的 modules 目录下)，接着配置虚拟主机。<br />
<br />
到这里你就可以利用 mod_python
来进行编程了，然后我们来配置一下 Django 并通过一个实例来让大家对这个现今最 HOT 的 python web
框架有一个大体的了解：首先，我们来学习一下用 django-admin.py 工具来简化你的工作 (当我们安装 Django
的时候，安装程序会自动把 django-admin.py 拷贝到 系统 PATH 下，所以我们可以直接使用它)。首先进入到我们的 python
程序目录 (我用的是：{DOCUMENT_ROOT}/python),执行：<br />
<br />
#django-admin.py startproject newtest<br />
<br />
这样就可以生成我们的测试工程了默认情况下会生成 {DOCUMENT_ROOT}/python/newtest 目录，该目录下会有如下文件：<br />
<br />
<strong>__init__.py</strong> (<em>表示这是一个 Python 的包</em>)<br />
<strong> manage.py</strong> (<em>提供简单化的 django-admin.py 命令，特别是可以自动进行 DJANGO_SETTINGS_MODULES 和 PYTHONPATH 的处理，而没有这个命令，处理上面环境变量是件麻烦的事情</em>)<br />
<strong> settings.py</strong> (<em>它是django的配置文件</em>)<br />
<strong> uls.py</strong> (<em>url映射处理文件， Karrigell 没有这种机制，它通过目录/文件/方法来自动对应，而 Django 的url映射是url对于某个模块方法的映射，目前不能自动完成</em>)<br />
<br />
然后我们在 apache 的虚拟主机配置文件里面加上：<br />
<br />
&lt;Location "/newtest/"&gt;<br />
SetHandler python-program<br />
PythonPath "sys.path+['{DOCUMENT_ROOT}/python']"<br />
PythonHandler django.core.handlers.modpython<br />
SetEnv DJANGO_SETTINGS_MODULE newtest.settings<br />
#PythonInterpreter mysite<br />
PythonDebug On<br />
&lt;/Location&gt;<br />
<br />
这
里要注意的是对于 PythonPath，必须设置成工程目录 ({DOCUMENT_ROOT}/python/newtest)
的上一级目录！这样我们就完成了 Django 和 apache 的整合了，赶快试一下吧，我们写一个 action 来测试一下 Django
的功能：</p>
<p><strong>3、编写测试程序。</strong><br />
<br />
<strong>1&gt;</strong> 首先，创建 ./list.py :<br />
<br />
#coding=utf-8<br />
from django.shortcuts import render_to_response<br />
<br />
address = [<br />
{'name':'张三', 'address':'地址一'},<br />
{'name':'李四', 'address':'地址二'}<br />
]<br />
<br />
def index(request):<br />
return render_to_response('list.html', {'address': address})<br />
<br />
<strong>2&gt;</strong> 然后，创建一个模版文件 ./templates/list.html :<br />
<br />
&lt;h2&gt;通讯录&lt;/h2&gt;<br />
&lt;table border="1"&gt;<br />
&lt;tr&gt;&lt;th&gt;姓名&lt;/th&gt;&lt;th&gt;地址&lt;/th&gt;&lt;/tr&gt;<br />
{% for user in address %}<br />
&lt;tr&gt;<br />
&lt;td&gt;{{ user.name }}&lt;/td&gt;<br />
&lt;td&gt;{{ user.address }}&lt;/td&gt;<br />
&lt;/tr&gt;<br />
{% endfor %}<br />
&lt;/table&gt;<br />
<br />
<strong>3&gt;</strong> 设置模版目录 (编辑 ./settings.py) :<br />
<br />
TEMPLATE_DIRS = (<br />
# Put strings here, like "/home/html/django_templates".<br />
# Always use forward slashes, even on Windows.<br />
'./templates',<br />
)<br />
<br />
<strong>4&gt;</strong> 修改 urls.py :<br />
<br />
from django.conf.urls.defaults import *<br />
<br />
urlpatterns = patterns('',<br />
# Example:<br />
# (r'^newtest/', include('newtest.foo.urls')),<br />
(r'^newtest/list/$', 'newtest.list.index'),<br />
<br />
# Uncomment this for admin:<br />
# (r'^admin/', include('django.contrib.admin.urls')),<br />
)<br />
<br />
5&gt; 重启 Apache 并访问相应 url (http://localhost/newtest/list/) 即可看到结果页面了：<br />
<br />
通讯录<br />
姓名    地址<br />
张三     地址一<br />
李四     地址二<br />
</p>
<p>到这里，你已经掌握了 Django 框架的基本内容了，就可以进一步学习 Django 的其他内容了，<strong>Enjoy It ：）</strong></p>
<img src ="http://www.blogjava.net/sealyu/aggbug/239110.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-06 20:57 <a href="http://www.blogjava.net/sealyu/archive/2008/11/06/239110.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redhat AS5 + Apache2 + Django 1.0 配置 (转)</title><link>http://www.blogjava.net/sealyu/archive/2008/11/06/239046.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 06 Nov 2008 07:59:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/06/239046.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/239046.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/06/239046.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/239046.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/239046.html</trackback:ping><description><![CDATA[<div>1. 安装 apach2 服务器。</div>
<div>这个很简单，从官网上下载源代码，httpd-2.2.9.tar.gz，解压缩后 configure , make ,make install. 会安装在/usr/local/目录下。<br />
使用命令/usr/local/apache2/bin/apachectl start&nbsp;&nbsp;&nbsp;&nbsp;  开启apache服务。<br />
使用命令/usr/local/apache2/bin/apachectl stop&nbsp;&nbsp;&nbsp;&nbsp;  停止apache服务。<br />
使用命令/usr/local/apache2/bin/apachectl restart  重新启动apache服务。</div>
<p>2. 安装python 2.5<br />
从官网上下载2.5版本，Python-2.5.2.tgz，解压缩后./configure, make , make install.就可以完成安装。默认安装目录为/usr/local/lib/python2.5/<br />
3. 安装mod_python.<br />
./configure &#8211;with-apxs=/usr/local/apache2/bin/apxs
&#8211;with-python=/usr/bin/python2.5，再make， make install
即可。安装完毕后会在目录/usr/local/apache2/modules下有文件mod_python.so，这个在后面要使用到。<br />
4. 安装django 1.0<br />
从<a href="http://www.djangoproject.com/download/" target="_top">官网</a>下
载1.0版本Django-1.0.tar.gz,解压缩，使用命令python setup.py install
安装，注意，如果机器里有以前的版本，一定要把以前的版本文件完全卸载活删除，django
不会覆盖以前的文件。默认安装目录为/usr/local/lib/python2.5/site-packages/django/<br />
5.安装sqlite<br />
这里使用到的数据库为sqlite3，所以要安装相应的包，这里 从官网下载pysqlite-2.3.5.tar.gz,安装很容易。<br />
6.配置文件.<br />
这里使用了最简单的配置方法，直接修改/usr/local/apache2/conf/httpd.conf<br />
(1) ,默认端口，80，可以不修改。<br />
(2) 在有LoadModule example字样的下面添加一下一行<br />
LoadModule python_module modules/mod_python.so<br />
(3) 配置ServerName，在有 #ServerName www.example.com:80字样的下面添加一下行：<br />
ServerName yourIP:80<br />
(4) 这是最重要的一步了。在有字样# This should be changed to whatever you set
DocumentRoot to.的后面，修改&lt;Directory&gt; &#8230; &lt;/Directory&gt;中间的内容为：<br />
<code><br />
&lt;Directory &#8220;/home/af/af&#8220;&gt;<br />
#<br />
# Possible values for the Options directive are &#8220;None&#8221;, &#8220;All&#8221;,<br />
# or any combination of:<br />
#&nbsp;&nbsp;  Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews<br />
#<br />
# Note that &#8220;MultiViews&#8221; must be named *explicitly* — &#8220;Options All&#8221;<br />
# doesn&#8217;t give it to you.<br />
#<br />
# The Options directive is both complicated and important.  Please see<br />
# http://httpd.apache.org/docs/2.2/mod/core.html#options<br />
# for more information.<br />
#<br />
#Options Indexes FollowSymLinks<br />
#PythonHandler django.core.handlers.modpython<br />
<span> PythonPath &#8220;['/home/af/af','/usr/local/lib/python2.5/site-packages'] + sys.path&#8221;<br />
PythonHandler django.core.handlers.modpython<br />
SetEnv DJANGO_SETTINGS_MODULE aftest.settings<br />
SetHandler python-program<br />
PythonDebug On</span><br />
#<br />
# AllowOverride controls what directives may be placed in .htaccess files.<br />
# It can be &#8220;All&#8221;, &#8220;None&#8221;, or any combination of the keywords:<br />
#&nbsp;&nbsp;  Options FileInfo AuthConfig Limit<br />
#<br />
#AllowOverride None</code></p>
<p>#<br />
# Controls who can get stuff from this server.<br />
#<br />
#Order allow,deny<br />
Allow from all</p>
<p>&lt;/Directory&gt;</p>
<p>其中蓝色部分为要修改和添加的部分，/home/af/af是Django 工程所在的目录，aftest为项目名称。<br />
(5)添加media。这里以安装Django自己提供的admin为例。<br />
在上一步的&lt;/Directory&gt;下面再添加<br />
<span>Alias /media /usr/local/lib/python2.5/site-packages/django/contrib/admin/media<br />
&lt;Location &#8220;/media&#8221;&gt;<br />
Options None<br />
SetHandler None<br />
Allow from all<br />
&lt;/Location&gt;</span><br />
即可。</p>
<p>配置完毕。</p>
<img src ="http://www.blogjava.net/sealyu/aggbug/239046.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-06 15:59 <a href="http://www.blogjava.net/sealyu/archive/2008/11/06/239046.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Using PIL with Django</title><link>http://www.blogjava.net/sealyu/archive/2008/11/06/238957.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 06 Nov 2008 01:32:00 GMT</pubDate><guid>http://www.blogjava.net/sealyu/archive/2008/11/06/238957.html</guid><wfw:comment>http://www.blogjava.net/sealyu/comments/238957.html</wfw:comment><comments>http://www.blogjava.net/sealyu/archive/2008/11/06/238957.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sealyu/comments/commentRss/238957.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sealyu/services/trackbacks/238957.html</trackback:ping><description><![CDATA[<p>Here&#8217;s a simple django view which creates a PIL image on the fly,
and returns it as a PNG image:
</p>
<pre>from django.utils.httpwrappers import HttpResponse<br />
from PIL import Image<br />
<br />
import random<br />
INK = "red", "blue", "green", "yellow"<br />
<br />
def image(request):<br />
<br />
# ... create/load image here ...<br />
image = Image.new("RGB", (800, 600), random.choice(INK))<br />
<br />
# serialize to HTTP response<br />
response = HttpResponse(mimetype="image/png")<br />
image.save(response, "PNG")<br />
return response</pre>
<div>&nbsp;</div>
<p>To draw things on the fly, you can use either PIL&#8217;s ImageDraw module,
or the <a href="http://effbot.org/zone/aggdraw-index.htm">aggdraw</a> module:
</p>
<pre wide="">from django.utils.httpwrappers import HttpResponse<br />
from PIL import Image<br />
from aggdraw import Draw, Pen, Brush, Font<br />
<br />
import random<br />
<br />
def graphics(request):<br />
<br />
image = Image.new("RGB", (800, 600), "white")<br />
draw = Draw(image)<br />
<br />
# ... draw graphics here ...<br />
for i in range(20):<br />
x0 = random.randint(0, image.size[0])<br />
y0 = random.randint(0, image.size[1])<br />
x1 = random.randint(0, image.size[0])<br />
y1 = random.randint(0, image.size[1])<br />
draw.rectangle((x0, y0, x1, y1), Pen(random.choice(INK), 5))<br />
<br />
draw.flush()<br />
<br />
response = HttpResponse(mimetype="image/png")<br />
image.save(response, "PNG")<br />
return response</pre>
<img src ="http://www.blogjava.net/sealyu/aggbug/238957.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sealyu/" target="_blank">seal</a> 2008-11-06 09:32 <a href="http://www.blogjava.net/sealyu/archive/2008/11/06/238957.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>