好的,我已经建立了一个基本的Django项目并成功安装了django-registration app - http://www.michelepasin.org/blog/2011/01/14/setting-up-django-registration/
我想展开User
以包含生日,个人资料图片等信息。我创建了一个名为user_profile
的应用。这是注册应用中的signals.py
:
from django.dispatch import Signal
user_activated = Signal(providing_args=["user", "request"])
从我的新应用user_profile
内部,听取此信号的方式是什么?我想我应该创建一个signals.py
文件,但我应该在里面写什么?一个from registration.signals import user_activated
声明还有什么?我创建的这个新应用程序还有一个model.py,我想在激活新帐户时自动填充一些数据。
另一个次要问题:当我将URL与基于类的视图链接时,该类的哪个方法被触发?如果我在基于类的视图中有4个方法,django如何决定使用哪个方法?感谢
答案 0 :(得分:2)
好的,如果我理解你的问题,你就会在user_profile/models.py
文件的末尾加上这样的内容:
from django.contrib.auth.models import User
from django.db.models.signals import post_save
def create_user_profile(sender, instance, **kwargs):
"""
Function to create user profile.
sender is the model class that sends the signal,
while instance is an actual instance of that class
"""
# your own logic here, for example :
user = instance
profile = UserProfile()
profile.user = user # link the profile to the user
profile.save()
# connect the function to the signal, for User instances)
post_save.connect(create_user_profile, sender=User)
对于第二个问题,在基于类的视图执行期间调用了许多方法。为了使用基于类的视图,您的URLconf应如下所示:
from myapp import views
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'list_something', views.YourListView.as_view(), name="list_something"),
)
但是您不应该在视图中覆盖as_view()
方法。根据您尝试执行的操作,您还可以使用其他方法进行覆盖。如果您提供更多信息,我可以帮助您。
答案 1 :(得分:0)
感谢艾略特,这是我到目前为止所做的:
signals.py 现已删除。
<强> models.py:强>
import datime
from django.db import models
from django.contrib.auth.models import User
try:
from django.utils.timezone import now as datetime_now
except ImportError:
datetime_now = datetime.datetime.now
class UserProfileManager(models.Manager):
def create_user_profile(self, user):
user_profile = self.create(user = user)
return user_profile
class UserProfile(models.Model):
YEARS = tuple(zip(range(1900, datetime_now.year)), zip(range(1900, datetime_now.year)))
MONTHS = (
('January','January'),('February','February'),('March','March'),('April','April'),
('May','May'), ('June','June'),('July','July'),('August','August'),
('September','September'),('October','October'),('November','November'), ('December', 'December')
)
GENDERS = (('M', 'Male'), ('F', 'Female'))
user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
birthday_year = models.CharField(max_length=1, blank = True, null = True, choices=YEARS)
birthday_month = models.CharField(max_length=1, blank = True, null = True, choices=MONTHS)
gender = models.CharField(max_length=1, blank = True, null = True, choices=GENDERS)
creation_time = models.DateTimeField(auto_now_add = True, auto_now = False)
update_time = models.DateTimeField(auto_now_add = False, auto_now = True)
class Meta:
verbose_name = _('user profile')
verbose_name_plural = _('user profiles')
objects = UserProfileManager()
def create_user_profile(sender, instance, **kwargs):
profile = UserProfile.objects.create_user_profile(user=instance)
profile.save()
post_save.connect(create_user_profile, sender=User)