This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render_to_response | |
import datetime | |
def current_datetime(request): | |
now = datetime.datetime.now() | |
return render_to_response('current_datetime.html', {'current_date': now}) | |
#get the query string | |
def contact(request): | |
errors = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#自定义校验规则 | |
from django import forms | |
from django.contrib.auth.models import User | |
class UserForm(forms.Form): | |
username = forms.CharField(min_length=6, max_length = 30) | |
email = forms.EmailField() | |
password = forms.CharField(widget = forms.PasswordInput, min_length = 6, max_length = 30) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
register = template.Library() | |
@register.filter(name='cut') | |
def cut(value, arg): | |
return value.replace(arg, '') | |
@register.tag(name="current_time") | |
def do_current_time(parser, token): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#write the raw sql | |
>>> from django.db import connection | |
>>> cursor = connection.cursor() | |
>>> cursor.execute(""" | |
... SELECT DISTINCT first_name | |
... FROM people_person | |
... WHERE last_name = %s""", ['Lennon']) | |
>>> row = cursor.fetchone() | |
>>> print row | |
['John'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
这样安装后,我们就可以在视图(view)的函数中处理user了。 在视图中存取users,主要用 request.user ;这个对象表示当前已登录的用户。 如果用户还没登录,这就是一个AnonymousUser对象(细节见下)。1 | |
你可以很容易地通过 is_authenticated() 方法来判断一个用户是否已经登录了:2 | |
if request.user.is_authenticated(): | |
# Do something for authenticated users. | |
else: | |
# Do something for anonymous users. | |
使用User对象 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.必须存在于一个应用内,并且应用必须包含在INSTALLED_APPS中 | |
2.必须包含一个templatetags文件夹,和models.py、views.py一个目录级别。其中必须有__init__.py文件 | |
。 | |
3.新建的标签或者过滤器必须以module的形式存在于templatetags文件夹中。module文件的名字就是你在 | |
template中装载的tags。所以不要和其他app中的标签或者过滤器冲突。 | |
例子: | |
文件目录如下: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALLED_APPS { | |
... | |
'django.contrib.messages', | |
... | |
} | |
MIDDLEWARE_CLASSES中 { | |
... | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.contrib.messages.middleware.MessageMiddleware', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#wallet_status { | |
height: 100px; | |
display: table; | |
} | |
#wallet_status span { | |
display: table-cell; | |
vertical-align: middle; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title>{{ page.title }}</title> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALLED_APPS = ( | |
'django.contrib.auth', | |
'django.contrib.sites', | |
'registration', | |
# ...other installed applications... | |
) | |
ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; you may, of course, use a different value. | |
email 激活地址example.com在admin页面的site中改变 |