django使用心得

###########################################################################
例如,下面是一个呈现静态“关于”页面的URLconf:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    ('^about/$', direct_to_template, {
        'template': 'about.html'
    })
)
一眼看上去似乎有点不可思议,不需要编写代码的视图!它和第八章中的例子完全一样: direct_to_template 视图从参数中获取渲染视图所需的相关信息。

################################################################

用这些命令查看将要生成的内容。
If you're interested, also run the following commands:
python manage.py validate -- Checks for any errors in the construction of your models.
python manage.py sqlcustom polls -- Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
python manage.py sqlclear polls -- Outputs the necessary DROP TABLE statements for this app, according to which tables already exist in your database (if any).
python manage.py sqlindexes polls -- Outputs the CREATE INDEX statements for this app.
python manage.py sqlall polls -- A combination of all the SQL from the sql, sqlcustom, and sqlindexes commands.

######################################################################

Why __unicode__() and not __str__()?
If you're familiar with Python, you might be in the habit of adding __str__() methods to your classes, not __unicode__() methods. We use __unicode__() here
because Django models deal with Unicode by default. All data stored in your database is converted to Unicode when it's returned.
Django models have a default __str__() method that calls __unicode__() and converts the result to a UTF-8 bytestring. This means that unicode(p)
will return a Unicode string, and str(p) will return a normal string, with characters encoded as UTF-8.
If all of this is gibberish to you, just remember to add __unicode__() methods to your models. With any luck, things should Just Work for you.

###############################################################

开启admin

Make the poll app modifiable in the admin
But where's our poll app? It's not displayed on the admin index page.
Just one thing to do: We need to tell the admin that Poll objects have an admin interface.
To do this, create a file called admin.py in your polls directory, and edit it to look like this:
from django.contrib import admin
from polls.models import Poll
admin.site.register(Poll)
#################################################################

一对多的关系:
Blog和Entry是一对多的关系,访问方式:entry.blog和b.entry_set.filter(***)
多对多关系:
双方看起来都是一对多的关系。每个方向在访问的时候,都是类似一对多的关系:entry.authors.filter()和author.entry_set.filter()

##################################################################

在文档中,admin中添加投票选项,一次添加一个。可以设置一次添加多个。

But, really, this【add one choice per time】 is an inefficient way of adding Choice objects to the system.
It'd be better if you could add a bunch of Choices directly when you create the Poll object. Let's make that happen.
Remove the register() call for the Choice model. Then, edit the Poll registration code to read:

from django.contrib import admin
from polls.models import Choice, Poll

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

admin.site.register(Poll, PollAdmin)
This tells Django: "Choice objects are edited on the Poll admin page. By default, provide enough fields for 3 choices."

#####################################################################

这里是一个让django的html文件能使用js、css和图片等静态资源的方法http://www.cnblogs.com/slider/archive/2012/07/10/2584615.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值