使用信号扩展Django-Registration
-
29-09-2019 - |
题
我在Stackoverflow上找到了一个解决方案,可以使用信号扩展使用新字段的Django Registration。这是链接: http://dmitko.ru/?p=546 .
我创建了扩展的配置文件模型,扩展表格,为设置,定义的URL添加了所需的选项,并且显示了正确的表单,但仅创建了正常用户(来自Auth Module)。为什么会发生?
account.models:
from django.db import models
from django.contrib.auth.models import User
from registration.signals import user_registered
import hashlib
class InheritedProfile(models.Model):
first_name = models.CharField("Name", max_length=50, blank=True, null=True)
last_name = models.CharField("Last name", max_length=50, blank=True, null=True)
pid = models.CharField("PESEL", max_length=11, blank=True, null=True)
street = models.CharField("Street", max_length=50, blank=True, null=True)
number = models.CharField("Flat/house number", max_length=10, blank=True, null=True)
code = models.CharField("Zip ", max_length=6, blank=True, null=True)
city = models.CharField("City", max_length=50, blank=True, null=True)
class Meta:
abstract=True
class UserProfile(InheritedProfile, User):
def upload_path(self, field_attname):
filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
return "uploads/users/%s" % (filename,)
image = models.ImageField(upload_to=upload_path, verbose_name="Image", blank=True, null=True)
def user_created(sender, user, request, **kwargs):
form = ExtendedRegistrationForm(request.POST)
extended_user = UserProfile(user=user)
extended_user.is_active = False
extended_user.first_name = form.extended_user['first_name']
extended_user.last_name = form.extended_user['last_name']
extended_user.pid = form.extended_user['pid']
extended_user.image = form.extended_user['image']
extended_user.street = form.extended_user['street']
extended_user.number = form.extended_user['number']
extended_user.code = form.extended_user['code']
extended_user.city = form.extended_user['city']
extended_user.save()
user_registered.connect(user_created)
我需要这种继承的构件是抽象的,因为其他模型将使用相同的字段。
account.forms
from django import forms
#import strings
from registration.forms import RegistrationForm
from models import UserProfile, InheritedProfile
class ExtendedRegistrationForm(RegistrationForm):
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), label="First name")
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), label="Last name")
pid = forms.RegexField(regex=r'^\d{11}', max_length=11 ,widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)))
image = forms.ImageField(label="Image",)
street = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), label="Street")
number = forms.CharField(widget=forms.TextInput, label="House/flat number")
code = forms.RegexField(regex=r'^\d{2}[-]\d{3}', max_length=6, widget=forms.TextInput(attrs=attrs_dict), label="Postal code")
city = forms.CharField(widget=forms.TextInput, label="City")
和添加到设置中的选项:
AUTH_PROFILE_MODULE = 'account.UserProfile'
ACCOUNT_ACTIVATION_DAYS = 7
最后,这就是注册信号的样子:
from django.dispatch import Signal
# A new user has registered.
user_registered = Signal(providing_args=["user", "request"])
编辑:user_created的凹痕没有任何改变,直到我尝试更改
user_registered.connect(user_created)
到
user_registered.connect(user_created, sender=UserProfile)
现在我得到了:
"smtpserverdisconted
异常位置:/bin/python-2.6.1/lib/python2.6/smtplib.py in getreply,行340英寸
追溯:
File "/home/fandrive/site-packages/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/home/fandrive/registration/views.py" in register
47. new_user = backend.register(request, **form.cleaned_data)
File "/home/fandrive/registration/backends/default/__init__.py" in register
20. password, site)
File "/home/fandrive/site-packages/django/db/transaction.py" in _commit_on_success
240. res = func(*args, **kw)
File "/home/fandrive/registration/models.py" in create_inactive_user
80. registration_profile.send_activation_email(site)
File "/home/fandrive/registration/models.py" in send_activation_email
256. self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
File "/home/fandrive/site-packages/django/contrib/auth/models.py" in email_user
271. send_mail(subject, message, from_email, [self.email])
File "/home/fandrive/site-packages/django/core/mail.py" in send_mail
390. connection=connection).send()
File "/home/fandrive/site-packages/django/core/mail.py" in send
266. return self.get_connection(fail_silently).send_messages([self])
File "/home/fandrive/site-packages/django/core/mail.py" in send_messages
172. sent = self._send(message)
File "/home/fandrive/site-packages/django/core/mail.py" in _send
186. email_message.message().as_string())
File "/bin/python-2.6.1/lib/python2.6/smtplib.py" in sendmail
708. self.rset()
File "/bin/python-2.6.1/lib/python2.6/smtplib.py" in rset
438. return self.docmd("rset")
File "/bin/python-2.6.1/lib/python2.6/smtplib.py" in docmd
363. return self.getreply()
File "/bin/python-2.6.1/lib/python2.6/smtplib.py" in getreply
340. raise SMTPServerDisconnected("Connection unexpectedly closed")
Exception Type: SMTPServerDisconnected at /user/register/
Exception Value: Connection unexpectedly closed
即使我目前正在使用虚拟电子邮件后端。注册在注册时发送邮件功能解决了此问题,但仍未创建我的扩展用户。
解决方案
问题可能是您如何连接信号的方式?在我的解决方案中是:
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
data = profile.Profile(user=user)
data.city_id = form.data["city"]
data.save()
from registration.signals import user_registered
user_registered.connect(user_created)
在你的
from django.dispatch import Signal
# A new user has registered.
user_registered = Signal(providing_args=["user", "request"])
另外,我将打开日志记录,以确保调用您的方法。我的解决方案在制作中正常工作,如果您需要的话,我可以查找其他细节。
不隶属于 StackOverflow