<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>#raynix# &#187; Django</title>
	<atom:link href="http://raynix.info/archives/tag/django/feed" rel="self" type="application/rss+xml" />
	<link>http://raynix.info</link>
	<description>The real world hurts, doesn't it? </description>
	<lastBuildDate>Thu, 09 Sep 2010 04:40:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Python＋Django笔记之三</title>
		<link>http://raynix.info/archives/463</link>
		<comments>http://raynix.info/archives/463#comments</comments>
		<pubDate>Thu, 12 Mar 2009 07:32:47 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=463</guid>
		<description><![CDATA[关于urls.py 假设test项目下有一个应用模块notes，那么： test/urls.py： from django.conf.urls.defaults import * urlpatterns = patterns(' ', (r'^notes/', include('notes.urls')), ... ) 这样所有匹配/notes/*的访问将转交给notes/urls.py处理。 test/notes/urls.py: ... urlpatterns = patterns(' ', url(r'^list/$', views.list_notes, name='note_list'), url(r'^(?P&#60;note_id&#62;\d+)/$', views.show_note, name='note_show'), ) 首先，由于需要反向解析（页面名称-&#62;URL），所以要使用url()，而不是原来的tuple。对应url(r'^list/$', views.list_notes, name='note_list'), 将匹配/notes/list/的访问指向给views.py中的list_notes()方法，并将'note_list'名称反向解析到/notes/list/的URL，以便在webapp中生成链接。 第二行中的(?P&#60;note_id&#62;\d+)是一个参数定义，其中note_id是参数名，\d+说明此参数是一个数字。其实起到的作用就是将/note/123/这样的url解释成一个调用show_note(request, note_id=123)。如果参数不是数字而是字词，那么\d+应该改为\w+，具体定义可以参考regex规则。 © raynix for #raynix#, 2009. &#124; Permalink &#124; 唉, 一个评论都没 &#124; Add to del.icio.us Post tags: Django, Python Feed enhanced [...]]]></description>
			<content:encoded><![CDATA[<p><strong>关于urls.py</strong></p>
<p>假设test项目下有一个应用模块notes，那么：</p>
<p>test/urls.py：</p>
<blockquote><p>from django.conf.urls.defaults import *</p>
<p>urlpatterns = patterns(' ',</p>
<p>(r'^notes/', include('notes.urls')),</p>
<p>...</p>
<p>)</p></blockquote>
<p>这样所有匹配/notes/*的访问将转交给notes/urls.py处理。</p>
<p>test/notes/urls.py:</p>
<blockquote><p>...</p>
<p>urlpatterns = patterns(' ',</p>
<p>url(r'^list/$', views.list_notes, name='note_list'),</p>
<p>url(r'^(?P&lt;note_id&gt;\d+)/$', views.show_note, name='note_show'),</p>
<p>)</p></blockquote>
<p>首先，由于需要反向解析（页面名称-&gt;URL），所以要使用url()，而不是原来的tuple。对应url(r'^list/$', views.list_notes, name='note_list'), 将匹配/notes/list/的访问指向给views.py中的list_notes()方法，并将'note_list'名称反向解析到/notes/list/的URL，以便在webapp中生成链接。</p>
<p>第二行中的(?P&lt;note_id&gt;\d+)是一个参数定义，其中note_id是参数名，\d+说明此参数是一个数字。其实起到的作用就是将/note/123/这样的url解释成一个调用show_note(request, note_id=123)。如果参数不是数字而是字词，那么\d+应该改为\w+，具体定义可以参考regex规则。</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/463">Permalink</a> |
<a href="http://raynix.info/archives/463#comments">唉, 一个评论都没</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/463&title=Python＋Django笔记之三">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/django" rel="tag">Django</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/463/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python＋Django笔记之二</title>
		<link>http://raynix.info/archives/446</link>
		<comments>http://raynix.info/archives/446#comments</comments>
		<pubDate>Wed, 25 Feb 2009 10:12:15 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[webapp]]></category>
		<category><![CDATA[开源]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=446</guid>
		<description><![CDATA[一个简单的select * 操作的Django方式： 首先配置urls.py from django.conf.urls.defaults import * from misc import views urlpatterns = patterns(' ', (r'^notes/$', views.list_notes), ) 其中r'^notes/$'是regex，用于匹配url；views.list_notes是views.py中的list_notes 方法。 然后写list_notes: def list_notes(request): notes = Note.objects.order_by('-created_date') conduit = { 'notes': notes } return render_to_response( 'misc/list.html', conduit, context_instance = RequestContext(request), ) 其中conduit这个词来自Mass Effect 最后就是把上面的misc/list.html写出来： {% extends "base.html" %} {% block title %}List Notes - {{ [...]]]></description>
			<content:encoded><![CDATA[<p>一个简单的select * 操作的Django方式：</p>
<p>首先配置urls.py</p>
<blockquote><p>from django.conf.urls.defaults import *<br />
from misc import views</p>
<p>urlpatterns = patterns(' ',<br />
(r'^notes/$', views.list_notes),<br />
)</p></blockquote>
<p>其中r'^notes/$'是regex，用于匹配url；views.list_notes是views.py中的list_notes 方法。</p>
<p>然后写list_notes:</p>
<blockquote><p>def list_notes(request):<br />
notes = Note.objects.order_by('-created_date')<br />
conduit = {<br />
'notes': notes<br />
}<br />
return render_to_response(<br />
'misc/list.html',<br />
conduit,<br />
context_instance = RequestContext(request),<br />
)</p></blockquote>
<p>其中conduit这个词来自Mass Effect <img src='http://raynix.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>最后就是把上面的misc/list.html写出来：</p>
<blockquote><p>{% extends "base.html" %}</p>
<p>{% block title %}List Notes - {{ block.super }}{% endblock %}</p>
<p>{% block body %}<br />
{% if notes %}<br />
&lt;ul&gt;<br />
{% for note in notes %}<br />
&lt;li&gt;{{ note.created_date }} : {{ note.message }}&lt;/li&gt;<br />
{% endfor %}<br />
&lt;/ul&gt;<br />
{% else %}<br />
&lt;p&gt;No notes yet.&lt;/p&gt;<br />
{% endif %}<br />
{% endblock %}</p></blockquote>
<p>这样用浏览器访问类似http://localhost:8000/app/notes的url时，全部notes应该就列出来了，并按时间倒序排列。</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/446">Permalink</a> |
<a href="http://raynix.info/archives/446#comments">唉, 一个评论都没</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/446&title=Python＋Django笔记之二">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/django" rel="tag">Django</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a>, <a href="http://raynix.info/archives/tag/webapp" rel="tag">webapp</a>, <a href="http://raynix.info/archives/tag/%e5%bc%80%e6%ba%90" rel="tag">开源</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/446/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python＋Django笔记之一</title>
		<link>http://raynix.info/archives/428</link>
		<comments>http://raynix.info/archives/428#comments</comments>
		<pubDate>Thu, 05 Feb 2009 10:08:37 +0000</pubDate>
		<dc:creator>raynix</dc:creator>
				<category><![CDATA[Python & Django]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[webapp]]></category>
		<category><![CDATA[开源软件]]></category>

		<guid isPermaLink="false">http://raynix.cn/?p=428</guid>
		<description><![CDATA[副标题：PGB: Project Galactica Board 启动 从无到有启动一个Django project（Django发音类似Jungle），以pgb为例： &#62;django-admin.py startproject pgb 设置数据库连接： &#62;vim pgb/settings.py DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. DATABASE_NAME = 'pgb'             # Or path to database file if using sqlite3. DATABASE_USER = 'root'             # Not used with sqlite3. DATABASE_PASSWORD = 'xxxxxx'         # Not used with sqlite3. 建立一个app（app可以看作项目的一个模块，一个app是一系列相关功能的集合，而且可以被多个project共用）： &#62;cd pgb &#62;python [...]]]></description>
			<content:encoded><![CDATA[<p>副标题：PGB: Project Galactica Board 启动</p>
<p>从无到有启动一个Django project（Django发音类似Jungle），以pgb为例：</p>
<blockquote><p>&gt;django-admin.py startproject pgb</p></blockquote>
<p>设置数据库连接：</p>
<blockquote><p>&gt;vim pgb/settings.py</p>
<p>DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.<br />
DATABASE_NAME = 'pgb'             # Or path to database file if using sqlite3.<br />
DATABASE_USER = 'root'             # Not used with sqlite3.<br />
DATABASE_PASSWORD = 'xxxxxx'         # Not used with sqlite3.</p></blockquote>
<p>建立一个app（app可以看作项目的一个模块，一个app是一系列相关功能的集合，而且可以被多个project共用）：</p>
<blockquote><p>&gt;cd pgb<br />
&gt;python manage.py startapp appname</p></blockquote>
<p>添加数据模型：编辑app/models.py，以Profile为例（缩进显示有问题，不过简单的class，凑合看吧），</p>
<blockquote><p>class Profile(models.Model):<br />
user = models.ForeignKey(User, unique=True)<br />
name = models.CharField(max_length=20)<br />
hp = models.IntegerField()<br />
ep = models.IntegerField()<br />
gp = models.IntegerField()<br />
level = models.IntegerField()<br />
title = models.CharField(max_length=100)<br />
guild = models.ForeignKey('Guild', related_name='members', null=True, blank=True)</p>
<p>def  __unicode__(self):<br />
return self.user.username + '.' + self.name</p>
<p>注：上面的'Guild'之所以要用引号，是为了解决环状引用的问题（<a href="http://spindrop.us/2008/02/03/django-circular-model-references/" target="_blank">circular model reference</a>），因为Python是只有runtime check的，前面class引用后面定义的class就会出错。<br />
注2：null=True 是针对DB SQL的NULL，blank=True是针对admin app的post validation。</p></blockquote>
<p>开启Django著名的admin：</p>
<ol>
<li>在urls.py中uncomment（这词有办法翻译么？）相关admin的几行</li>
<li>在app文件夹添加admin.py，内容类似如下：<br />
from django.contrib import admin<br />
from pgb.arena.models import Profileadmin.site.register(Profile)</li>
</ol>
<p>为DB生成Tables：</p>
<blockquote><p>&gt;python manage.py syncdb</p></blockquote>
<p>重新生成DB Tables，数据被销毁：</p>
<blockquote><p>&gt;python manage.py reset appname</p></blockquote>
<p>启动测试环境：</p>
<blockquote><p>&gt;python manage.py runserver [port]</p></blockquote>
<p>第一篇完。</p>
<hr />
<p><small>© raynix for <a href="http://raynix.info">#raynix#</a>, 2009. |
<a href="http://raynix.info/archives/428">Permalink</a> |
<a href="http://raynix.info/archives/428#comments">2 条评论</a> |
Add to
<a href="http://del.icio.us/post?url=http://raynix.info/archives/428&title=Python＋Django笔记之一">del.icio.us</a>
<br/>
Post tags: <a href="http://raynix.info/archives/tag/django" rel="tag">Django</a>, <a href="http://raynix.info/archives/tag/python" rel="tag">Python</a>, <a href="http://raynix.info/archives/tag/webapp" rel="tag">webapp</a>, <a href="http://raynix.info/archives/tag/%e5%bc%80%e6%ba%90%e8%bd%af%e4%bb%b6" rel="tag">开源软件</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://raynix.info/archives/428/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
