django-profiles和nonetype用户

时间:2012-02-17 07:01:00

标签: django django-profiles

我很难过!

我正在构建一个表单,允许用户同时填写个人资料字段和用户字段。网络上有这样的例子和这个网站上的数字(例如How to create a UserProfile form in Django with first_name, last_name modifications?

然而,尽我所能,我继续在我的form.py

中获得一个无类型的用户

一些代码: forms.py

class ProfileForm(ModelForm):
first_name = forms.CharField(label="First name")
last_name = forms.CharField(label="Last name")

def __init__(self, *args, **kwargs):
    super(ProfileForm, self).__init__(*args, **kwargs)
    print self.instance #profile does not exist here 
    self.fields['first_name'].initial = self.instance.user.first_name
    self.fields['last_name'].initial = self.instance.last_name

def save(self, *args, **kwargs):
    super(ProfileForm, self).save(*args, **kwargs)
    self.instance.user.first_name = self.cleaned_data.get('first_name')    
    self.instance.user.last_name = self.cleaned_data.get('last_name')
    self.instance.user.save()

class Meta:
    model = UserProfile
    exclude = ('user')

urls.py

    url(r'^profiles/edit/', 'profiles.views.edit_profile', {'form_class': ProfileForm}, name='edit_profile' ),

template.html

    <div class="clearfix">
    <legend>Edit your profile details</legend>
</div>
{% include 'profiles/profile_form.html' with user=user %}

该视图来自django-profiles,但我甚至尝试进入应用程序并看到我的用户存在并被拉入视图中的实例,该实例在表单中没有用户!

其他注意事项:我使用电子邮件作为我的身份验证方法而不是用户名,但仍然有基于文本的随机用户名(因为我知道电子邮件存在问题  作为django-profiles中的用户名)。这仍然是我的问题吗?

如果它有用,这是我的身份验证def:

    def authenticate(self, username=None, password=None):
    """ Authenticate a user based on email address as the user name. """
    try:
        user = User.objects.get(email=username)
        if user.check_password(password):
            return user
    except User.DoesNotExist:
        return None 

def get_user(self, user_id):
    """ Get a User object from the user_id. """
    try:
        return User.objects.get(pk=user_id)
    except User.DoesNotExist:
        return None

这是作为自定义后端的引用,我已经确认它有效。

我希望有人有一些见解!

编辑:

我的完全例外:

Exception Type: TemplateSyntaxError
Exception Value: Caught AttributeError while rendering: 'NoneType' object has no attribute 'first_name'
Exception Location:/Users/willcadell/placespeak/webapps/place/../place/accounts/forms.py in __init__, line 13

view(来自django-profiles):

def edit_profile(request, form_class=None, success_url=None,
             template_name='profiles/edit_profile.html',
             extra_context=None):

    try:

        profile_obj = request.user.get_profile()
        print profile_obj #profile is good here
    except ObjectDoesNotExist:
        return HttpResponseRedirect(reverse('profiles_create_profile'))

    if success_url is None:
        success_url = reverse('profiles_profile_detail', kwargs={ 'username': request.user.username })
    if form_class is None:
        form_class = utils.get_profile_form()      
    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES, instance=profile_obj)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(success_url)
    else:
        form = form_class(instance=profile_obj)

    if extra_context is None:
        extra_context = {}

    context = RequestContext(request)

    for key, value in extra_context.items():
        context[key] = callable(value) and value() or value

    return render_to_response(template_name,
                          { 'form': form,
                          'profile': profile_obj,},
                          context_instance=context)

我还添加了两条注释,指出配置文件存在的位置和不存在的位置,即它在视图中作为profile_obj但在表单中不显示为self.instance。

另一个(可能不相关)的症状是,对于这个异常页面,django工具栏没有出现......很奇怪!

2 个答案:

答案 0 :(得分:1)

您可能并不总是有一个实例,这可能是它显示为无的原因。 尝试做

def __init__(self, *args, **kwargs):
    super(UserProfileForm, self).__init__(*args, **kwargs)
    if kwargs.has_key('instance'):
        instance = kwargs['instance']
        self.initial['first_name'] = instance.first_name
        self.initial['last_name'] = instance.last_name

此外,您使用first_name对象中的user,但使用last_name对象中的user_profile!我会保持一致。我正在使用以下代码从两个位置提供这些代码

   # first_name - stores in auth.User
    def _get_first_name(self):
        return self.user.first_name
    def _set_first_name(self, name):
        self.user.first_name = name
    first_name = property(_get_first_name, _set_first_name)

    # last_name - stores in auth.User
    def _get_last_name(self):
        return self.user.last_name
    def _set_last_name(self, name):
        self.user.last_name = name
    last_name = property(_get_last_name, _set_last_name)

答案 1 :(得分:0)

这就是我最终做的事情:

view.py:

def edit_profile(request):

profile_obj = request.user.get_profile()
print dir(profile_obj.profileasset_set)
asset_obj = profile_obj.
user_obj = request.user

if request.method == 'POST':
    form = ProfileForm(request.POST, )
    if form.is_valid():

        profile_obj.twitter = form.cleaned_data['twitter']
        profile_obj.about = form.cleaned_data['about']
        profile_obj.url = form.cleaned_data['url']
        profile_obj.save()

        profile


        user_obj.first_name = form.cleaned_data['first_name']
        user_obj.last_name = form.cleaned_data['last_name']
        user_obj.save()

        form.save()
        return HttpResponseRedirect('/profiles/profile')

else:
    form = ProfileForm(instance=user_obj, 
        initial={
                 'first_name':user_obj.first_name,
                 'last_name':user_obj.last_name,
                 'twitter':profile_obj.twitter,
                 'url':profile_obj.url,
                 'about':profile_obj.about,
                }
        )

template = 'profiles/edit_profile.html'
data = {'form': form, }      
return render_to_response(template, data, context_instance=RequestContext(request))

forms.py:

class ProfileForm(ModelForm):

first_name = forms.CharField(label="First name")
last_name = forms.CharField(label="Last name")
twitter = forms.CharField(label="Twitter")
about = forms.CharField(label="About", widget=forms.Textarea)
url = forms.CharField(label="URL")

class Meta:
    model = UserProfile
    fields = ('url', 'twitter', 'about')

希望这有助于某人!