django CBV generic DetailView重定向,如果对象不存在

时间:2017-12-22 09:47:31

标签: django python-3.x detailview

我有一个DetailView,如果当前登录的用户创建了一个userprofile,它将显示userprofile。如果用户没有创建配置文件,我需要一个else条件。由于我是django和python的新手,甚至更新的CBV,我无法弄清楚我的下一步。我希望def get_object()中有一种方法可以重定向到UserProfile。 如果没有userprofile,则会导致Related Object DoesNotExist错误。如何编写else或except条件以重定向到ProfileView(表单以创建配置文件)

  

PS:UserProfile是创建配置文件,UserProfileView是查看a   创建的配置文件,UserProfileUpdate将更新现有的配置文件。

我不想通过网址传递PK

我是django 2.0,python 3.6.3

专门寻找如何从DetailView方法重定向except / if案例

Models.py

class User(AbstractUser):
    """User model."""

    username = None
    email = models.EmailField(_('email address'), unique=True)
    phone = models.CharField(max_length=128, unique=True, null=True,
                             validators=[validators.RegexValidator(
                                 r'^(?:\+?(\d{2}))?(\d{10})$',
                                 _('Enter a valid phone number. Type without space or special charecter.')
                             )])

    objects = UserManager()

    REQUIRED_FIELDS = ['first_name']
    USERNAME_FIELD = 'email'

    def __str__(self):
        return self.email


class UserProfile(models.Model):
    """User Profile"""
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    country = models.CharField(max_length=128)
    state = models.CharField(max_length=128)
    city = models.CharField(max_length=128)
    landmark = models.CharField(
        max_length=128, help_text='Enter a landmark closest to you')
    address_line_1 = models.CharField(
        max_length=128, help_text='House name/Flat No')
    address_line_2 = models.CharField(
        max_length=128, help_text='Street Name/No')
    address_line_3 = models.CharField(
        max_length=128, help_text='Locality Name')
    pincode = models.IntegerField()
    land_phone = models.CharField(max_length=128, unique=True, null=True,
                                  validators=[validators.RegexValidator(
                                      r'^(?:\+?(\d{4}))\-?(\d{7})$',
                                      _('Enter a valid phone number. Type without space. Format 0400-2012345.')
                                  )])

    def __str__(self):
        return self.user.email

Views.py

class UserProfileFormView(FormView):
    form_class = UserProfileForm
    template_name = 'userprofile.html'
    success_url = '/'

    def form_valid(self, form):
        import pdb
        pdb.set_trace()
        temp_form = form.save(commit=False)
        temp_form.user = self.request.user
        temp_form.save()
        return super().form_valid(form)

    def form_invalid(self, form):
        response = super().form_invalid(form)
        return redirect('users:userprofile')


class UserProfileView(DetailView):
    model = UserProfile
    context_object_name = 'userprofile'
    template_name = 'x.html'

    # def dispatch(self, request, *args, **kwargs):
    #     import pdb; pdb.set_trace()
    #     if self.request.user.userprofile.pk is not None:
    #         pass
    #     else:
    #         return redirect('users:userprofile')

    def get_object(self):
        import pdb; pdb.set_trace()
        self.object = UserProfile.objects.get(pk=self.request.user.userprofile.pk)
        return self.object

    # def get_context_data(self, **kwargs):
    #     import pdb; pdb.set_trace()
    #     context = kwargs
    #     context_object_name = 'userprofile'
    #     context['userprofile'] = UserProfile.objects.get(pk=self.request.user.userprofile.pk)
    #     if context_object_name:            
    #         return context
    #     else:
    #         return redirect('users:userprofile')


class UserProfileUpdate(UpdateView):
    model = UserProfile
    fields = ('address_line_1', 'address_line_2', 'address_line_3',
              'landmark', 'city', 'state', 'country', 'pincode', 'land_phone')

    template_name = 'userprofile.html'
    success_url = 'home'

3 个答案:

答案 0 :(得分:2)

我知道您的问题已经很老了,但是没有人真正回答过,所以我想我会

我建议在您的用户模型上创建一个方法,以检查是否有用户配置文件,例如:

def has_profile(self):
  return hasattr(self, 'userprofile')

'hasattr'将返回True或False。 现在,您可以在分派中完成

def dispatch(self, request, *args, **kwargs):
    if not self.request.user.has_profile():
        return redirect('users:userprofile')
    return super().dispatch(request, *args, **kwargs)

答案 1 :(得分:0)

class UserProfileView(DetailView):
    model = UserProfile
    context_object_name = 'userprofile'
    template_name = 'x.html'

    def get(self, request, *args, **kwargs):
        from django.http import Http404
        try:
            self.object = self.get_object()
            context = self.get_context_data(object=self.object)
            return self.render_to_response(context)
        except Http404:
            # redirect is here
            from django.shortcuts import redirect
            from django.urls import reverse_lazy
            return redirect(reverse('users:userprofile_create'))

答案 2 :(得分:0)

所以经过互联网搜索后,这是我能想到的最佳答案。希望其他人会发现这个有用,或者一个知识更丰富的人有更优化的解决方案。

class UserProfileView(DetailView):
    model = UserProfile
    context_object_name = 'userprofile'
    template_name = 'x.html'


    def dispatch(self, request, *args, **kwargs):
        try:
            self.object =  self.request.user.userprofile
        except:
            return redirect('users:userprofile')

        self.get_object()
        return super(UserProfileView, self).get(request, *args, **kwargs)

    def get_object(self):        
        self.object = self.request.user.userprofile
        return self.object

我在这里所做的(或者更确切地说django为我做的)是使用派遣。通过使用dispatch,我只能过滤那些具有配置文件的用户继续获取配置文件或使用get_object方法返回UserProfile对象。如果用户没有配置文件,则dispatch方法中的except条件会将用户重定向到配置文件创建页面。如果用户具有配置文件,则调度调用(覆盖。我不确定使用哪个术语)get_object方法来获取现有对象。总而言之,get_object方法可以仅返回对象作为响应。所以我尝试重定向的尝试(注释的代码部分)无法正常工作,因为django不会允许它。如果我的任何解释是错误的,请指出我的缺点