我在我的django项目中使用userena。我决定在我的项目中包含一个自动引用代码生成器选项。你知道,要使用userena,我们必须创建一个额外的django应用程序,并且必须创建个人资料模型。因此,我创建了一个应用程序,即帐户,我的模型就是这样,
import uuid
import base64
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from userena.models import UserenaBaseProfile
from userena.utils import user_model_label
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User,unique=True,
verbose_name=_('user'),related_name='my_profile')
favourite_snack = models.CharField(_('favourite snack'),max_length=5)
提到,这是模型之前我添加了自动引荐代码生成器的功能。然后我决定添加一些功能来为每个用户生成引用代码注册。这就是为什么我在我的模型中添加了一些功能,就像那样......
import uuid
import base64
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from userena.models import UserenaBaseProfile
from userena.utils import user_model_label
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User,unique=True,
verbose_name=_('user'),related_name='my_profile')
favourite_snack = models.CharField(_('favourite snack'),max_length=5)
referral_code = models.CharField(max_length=300, blank=True, null=True)
def get_absolute_url(self):
return u'/profile/show/%d' % self.id
def generate_verification_code(self):
return base64.urlsafe_b64encode(uuid.uuid1().bytes.encode("base64").rstrip())[:25]
def save(self, *args, **kwargs):
if not self.pk:
self.referral_code = self.generate_verification_code()
elif not self.verification_code:
self.referral_code = self.generate_verification_code()
return super(MyProfile, self).save(*args, **kwargs)
现在注册后会为每个用户生成一个唯一的引荐代码。很好,我没有遇到任何问题,但,因为 MyProfile (上面给出)模型与userena相关,可能是为什么(实际上我不确定)我正面临错误
'MyProfile' object has no attribute 'verification_code'
我正面临上述错误,而我正在尝试使用默认的userena配置文件编辑选项 编辑我的个人资料。
我再次提到它,上面的错误正在发生而我正在尝试使用默认的userena配置文件编辑选项编辑我的个人资料
我也发布了引用..
环境:
请求方法:POST 请求网址:http://www.myproject.com/accounts/veer/edit/
Django Version: 1.5.5
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'photo',
'userena',
'guardian',
'easy_thumbnails',
'accounts',
'cloudinary',
'paypal.standard.ipn',
'myprofile',
'watermarker',
'mail',
'stored_messages',
'rest_framework',
'endless_pagination')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/codes/A/test/shutterstock/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/codes/A/test/shutterstock/userena/decorators.py" in _wrapped_view
28. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django_guardian-1.1.1- py2.7.egg/guardian/decorators.py" in _wrapped_view
106. return view_func(request, *args, **kwargs)
File "/codes/A/test/shutterstock/userena/views.py" in profile_edit
711. profile = form.save()
File "/codes/A/test/shutterstock/userena/forms.py" in save
241. profile = super(EditProfileForm, self).save(commit=commit)
File "/codes/A/test/shutterstock/django/forms/models.py" in save
370. fail_message, commit, construct=False)
File "/codes/A/test/shutterstock/django/forms/models.py" in save_instance
87. instance.save()
File "/codes/A/test/shutterstock/accounts/models.py" in save
23. elif not self.verification_code:
Exception Type: AttributeError at /accounts/veer/edit/
Exception Value: 'MyProfile' object has no attribute 'verification_code'
现在我的问题是,有没有其他方法可以使用userena生成推荐代码或任何其他方式,使用userena还是有任何我当前问题的解决方案**?
答案 0 :(得分:2)
你有一个错字:
elif not self.verification_code:
应该是:
elif not self.referral_code:
以下不是解决方案,但您也询问了替代方案。
如果您只想在注册时生成一次代码(而不是在它为空或无时生成代码),您可以直接在字段中设置默认值:
referral_code = models.CharField(
max_length=300,
blank=True,
null=True,
default = base64.urlsafe_b64encode(uuid.uuid1().bytes.encode("base64").rstrip())[:25])
对于get_absolute_url
我强烈建议你使用reverse
来命名网址而不是硬编码路径。例如,如果你有一组名为' profile&#39的网址;其中一个被命名为" show"它看起来像这样:
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse("profile:show", args =(self.id,))
你的主urls.py会有一个条目:
url(r'^profile/', include(profile.urls), name = "profile"),
和profile.urls会有:
url(r'^show/(?P<id>\d+)/$', view_name, name = "show),