这里不是说的是 profile,关于 profile 的看官方
网上有继承 User 的那个更不可用
django 写的太死,扩展下还是很麻烦
还有,下面的这个会在 django 库的 contrib\auth 目录下创建 migrations 目录,如果你觉得这个影响你别的项目的话,那就在这个项目目录下新建 django 目录,把 django 库下的文件全部复制过来,目录结构如 项目目录/django/bin 这样
------------------------------------------------------
后记:
south 的 auth migrations 目录可以定义的,方法来自于 django-primate 这个app
#注意,这里你要在项目目录下建个 users 目录
SOUTH_MIGRATION_MODULES = {
'auth': 'users.migrations',
}
还有,更好的扩展 User model 使用 django-primate 这个库要好很多,参见 https://github.com/aino/django-primate
------------------------------------------------------
开工
F:\hyp\test>django-admin.py startproject my_django
F:\hyp\test>cd my_django
F:\hyp\test\my_django>manage.py startapp my_app
编辑 settings.py,INSTALLED_APPS 加入 my_app 和 south ,如下
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'my_app',
'south'
)
DATABASES 配置好引擎并建好数据库,如这里我们配置成sqlite3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'test.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
my_app/models.py 中加入,添加一个字段,添加一个实例方法,一个类方法
关于添加字段参见 http://djangosnippets.org/snippets/2132/
from django.db import models
from django.contrib.auth.models import User
some_field = models.CharField(max_length=32)
some_field.contribute_to_class(User, 'some_field')
def before_save(self):
self.username = 'test'
User.before_save = before_save
@classmethod
def test(cls):
print cls.__name__
User.test = test
创建 auth 的 migration,这样在 syncdb 的时候就会跳过 auth 这个app
F:\hyp\test\my_django>manage.py schemamigration auth --initial
Creating migrations directory at 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
Creating __init__.py in 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
+ Added model auth.Permission
+ Added unique constraint for ['content_type', 'codename'] on auth.Permission
+ Added model auth.Group
+ Added M2M table for permissions on auth.Group
+ Added model auth.User
+ Added M2M table for groups on auth.User
+ Added M2M table for user_permissions on auth.User
+ Added model auth.Message
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate auth
创建 my_app 的 migration
F:\hyp\test\my_django>manage.py schemamigration my_app --initial
Creating migrations directory at 'F:\hyp\test\my_django\my_app\migrations'...
Creating __init__.py in 'F:\hyp\test\my_django\my_app\migrations'...
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate my_app
执行 manage.py syncdb --all 创建表
F:\hyp\test\my_django>manage.py syncdb --all
Syncing...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table south_migrationhistory
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): no
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.staticfiles
> my_app
> south
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)
好了,基本上搞定了,下面我们来测试一下
F:\hyp\test\my_django>manage.py shell
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from my_app.models import *
>>> u = User()
>>> u.username
''
>>> u.before_save()
>>> u.username
'test'
>>> u.some_field
''
>>> User.test()
User
网上有继承 User 的那个更不可用
django 写的太死,扩展下还是很麻烦
还有,下面的这个会在 django 库的 contrib\auth 目录下创建 migrations 目录,如果你觉得这个影响你别的项目的话,那就在这个项目目录下新建 django 目录,把 django 库下的文件全部复制过来,目录结构如 项目目录/django/bin 这样
------------------------------------------------------
后记:
south 的 auth migrations 目录可以定义的,方法来自于 django-primate 这个app
#注意,这里你要在项目目录下建个 users 目录
SOUTH_MIGRATION_MODULES = {
'auth': 'users.migrations',
}
还有,更好的扩展 User model 使用 django-primate 这个库要好很多,参见 https://github.com/aino/django-primate
------------------------------------------------------
开工
F:\hyp\test>django-admin.py startproject my_django
F:\hyp\test>cd my_django
F:\hyp\test\my_django>manage.py startapp my_app
编辑 settings.py,INSTALLED_APPS 加入 my_app 和 south ,如下
Quotation
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'my_app',
'south'
)
DATABASES 配置好引擎并建好数据库,如这里我们配置成sqlite3
Quotation
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'test.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
my_app/models.py 中加入,添加一个字段,添加一个实例方法,一个类方法
关于添加字段参见 http://djangosnippets.org/snippets/2132/
from django.db import models
from django.contrib.auth.models import User
some_field = models.CharField(max_length=32)
some_field.contribute_to_class(User, 'some_field')
def before_save(self):
self.username = 'test'
User.before_save = before_save
@classmethod
def test(cls):
print cls.__name__
User.test = test
创建 auth 的 migration,这样在 syncdb 的时候就会跳过 auth 这个app
F:\hyp\test\my_django>manage.py schemamigration auth --initial
Creating migrations directory at 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
Creating __init__.py in 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
+ Added model auth.Permission
+ Added unique constraint for ['content_type', 'codename'] on auth.Permission
+ Added model auth.Group
+ Added M2M table for permissions on auth.Group
+ Added model auth.User
+ Added M2M table for groups on auth.User
+ Added M2M table for user_permissions on auth.User
+ Added model auth.Message
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate auth
创建 my_app 的 migration
F:\hyp\test\my_django>manage.py schemamigration my_app --initial
Creating migrations directory at 'F:\hyp\test\my_django\my_app\migrations'...
Creating __init__.py in 'F:\hyp\test\my_django\my_app\migrations'...
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate my_app
执行 manage.py syncdb --all 创建表
F:\hyp\test\my_django>manage.py syncdb --all
Syncing...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table south_migrationhistory
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): no
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.staticfiles
> my_app
> south
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)
好了,基本上搞定了,下面我们来测试一下
F:\hyp\test\my_django>manage.py shell
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from my_app.models import *
>>> u = User()
>>> u.username
''
>>> u.before_save()
>>> u.username
'test'
>>> u.some_field
''
>>> User.test()
User
May
26
2011
扩展django auth库使之记录 登录ip
14:47 , vkill
就是在登录的同时记录下登录ip
直接上代码
#models.py
from django.db import models
from django.contrib.auth.models import User
class user_logininfo(models.Model):
class Meta:
db_table = 'user_logininfo'
ip_address = models.IPAddressField('ip_address')
user = models.ForeignKey(User)
from django.contrib.auth.signals import user_logged_in
def create_login_info(sender, user, request, **kwargs):
user.user_logininfo_set.create(ip_address = request.META.get('REMOTE_ADDR'))
user_logged_in.connect(create_login_info)
user_logged_in 这个参见 django\contrib\auth\signals.py 里 user_logged_in = Signal(providing_args=['request', 'user'])
具体的看 user_logged_in 是怎么传递 request 进去的参见 django\contrib\auth\__init__.py 里 login 方法最后一行的 user_logged_in.send(sender=user.__class__, request=request, user=user)
直接上代码
#models.py
from django.db import models
from django.contrib.auth.models import User
class user_logininfo(models.Model):
class Meta:
db_table = 'user_logininfo'
ip_address = models.IPAddressField('ip_address')
user = models.ForeignKey(User)
from django.contrib.auth.signals import user_logged_in
def create_login_info(sender, user, request, **kwargs):
user.user_logininfo_set.create(ip_address = request.META.get('REMOTE_ADDR'))
user_logged_in.connect(create_login_info)
user_logged_in 这个参见 django\contrib\auth\signals.py 里 user_logged_in = Signal(providing_args=['request', 'user'])
具体的看 user_logged_in 是怎么传递 request 进去的参见 django\contrib\auth\__init__.py 里 login 方法最后一行的 user_logged_in.send(sender=user.__class__, request=request, user=user)
Apr
28
2011
Mar
26
2011
rails3 中新用户注册、普通用户修改资料、管理员管理用户资料 权限之model 继承实现
需求分析:
一般情况下我们User表中的基本字段如下:
nickname email_address salt crypted_password is_active is_admin
其中salt crypted_password 为model内部赋值,不对外提供访问权限
那么这里使用了salt加密密码,自然得有个虚拟字段 password
还有普通用户修改密码时也需要验证原始密码,自然得有个虚拟字段 old_password
需求分析:
一般情况下我们User表中的基本字段如下:
nickname email_address salt crypted_password is_active is_admin
其中salt crypted_password 为model内部赋值,不对外提供访问权限
那么这里使用了salt加密密码,自然得有个虚拟字段 password
还有普通用户修改密码时也需要验证原始密码,自然得有个虚拟字段 old_password
>> Read more
Mar
24
2011
环境:ruby 1.8.7、rails3.0.1、ckeditor-3.4.3.pre
错误情况:当我们把ckeditor放在admin后台使用的时候,上传的时候可能会出500错误的js弹出提示
ckeditor 的安装参见 自带的说明文件
具体的怎么放到admin后台使用,我是修改在admin目录下去的,最重要的是修改 /config/initializers/ckeditor.rb 中的一些设置
分析:
当出现这个错误的时候,我们打开log看会发现下面这段
错误情况:当我们把ckeditor放在admin后台使用的时候,上传的时候可能会出500错误的js弹出提示
ckeditor 的安装参见 自带的说明文件
具体的怎么放到admin后台使用,我是修改在admin目录下去的,最重要的是修改 /config/initializers/ckeditor.rb 中的一些设置
分析:
当出现这个错误的时候,我们打开log看会发现下面这段
>> Read more
Jan
4
2011
UsersController 下有7个 action ,分别为 index show new edit create update destroy
下面就三类用户对action的操作权限做限制
下面就三类用户对action的操作权限做限制
>> Read more
Aug
7
2010



