在此示例中如何使用参数重定向到另一个视图?

时间:2017-06-04 12:52:31

标签: python django redirect

我在这里有一个intro.html 呈现视图:

@login_required
def share(request):
    user = request.user
    small = user.username.title()
    cases = Case.objects.filter(users=user).order_by('-english')
    groups = user.groups.all()
    allgroups = Group.objects.all()
    suggestgroups = set(allgroups).difference(set(groups))
    allusers = User.objects.all().exclude(username=user.username)

    sendgroups = groups

    if not user.is_superuser:
        user_ex = UserEx.objects.get(user=request.user)
        friendlist = FriendList.objects.get(user=user_ex)
        friends = friendlist.friends.all().exclude(username=user.username)
        friendrequest = FriendReqRecList.objects.get(user=user_ex)
        friendrequestsent = FriendReqSentList.objects.get(user=user_ex)
        friendrequests = friendrequest.friend_rec_requests.all().exclude(username=user.username)
        friendrequestsents = friendrequestsent.friend_sent_requests.all().exclude(username=user.username)
        nonfriends = set(allusers).difference(set(friends))

        sendfriends = friendlist.friends.all().exclude(username=user.username)
        return render(request, 'intro.html',
                      {'sendfriends': sendfriends,
                       'sendgroups': sendgroups,
                       'allusers': allusers,
                       'cases': cases,
                       'friendrequests': friendrequests,
                       'friendrequestsents': friendrequestsents,
                       'friends': friends,
                       'groups': groups,
                       'nonfriends': nonfriends,
                       'small' : small,
                       'suggestgroups': suggestgroups})

    return render(request, 'intro.html', {'suggestgroups': suggestgroups,
                                          'cases': cases,
                                          'groups': groups,
                                          'small' : small})

我有另一个共享视图,它将朋友和组添加到intro.html(更改只是额外的两个参数,sendfriends和sendgroups)。

{{1}}

第二个视图还使用额外的两个参数(sendfriends和sendgroups)呈现intro.html。但我想遵守不要重复(DRY)规则并在此处使用 redirect()。如何通过共享视图传递这两个参数并重定向到主视图?

1 个答案:

答案 0 :(得分:2)

这个怎么样:

def home(request):
    return render_intro(request)


@login_required
def share(request):
    return render_intro(request, is_share=True)


def render_intro(request, is_share=False):
    context = intro_context(request.user, is_share)
    return render(request, 'intro.html', context)


def intro_context(user, is_share=False):
    small = user.username.title()
    cases = Case.objects.filter(users=user).order_by('-english')
    groups = user.groups.all()
    allgroups = Group.objects.all()
    suggestgroups = set(allgroups).difference(set(groups))
    allusers = User.objects.all().exclude(username=user.username)

    if user.is_superuser:
        return {'suggestgroups': suggestgroups,
                'cases': cases,
                'groups': groups,
                'small' : small}

    user_ex = UserEx.objects.get(user=user)
    friendlist = FriendList.objects.get(user=user_ex)
    friends = friendlist.friends.all().exclude(username=user.username)
    friendrequest = FriendReqRecList.objects.get(user=user_ex)
    friendrequestsent = FriendReqSentList.objects.get(user=user_ex)
    friendrequests = friendrequest.friend_rec_requests.all().exclude(username=user.username)
    friendrequestsents = friendrequestsent.friend_sent_requests.all().exclude(username=user.username)
    nonfriends = set(allusers).difference(set(friends))

    context = {'allusers': allusers,
               'cases': cases,
               'friendrequests': friendrequests,
               'friendrequestsents': friendrequestsents,
               'friends': friends,
               'groups': groups,
               'nonfriends': nonfriends,
               'small' : small,
               'suggestgroups': suggestgroups}

    if is_share:
        sendgroups = groups
        sendfriends = friendlist.friends.all().exclude(username=user.username)
        context.update({'sendfriends': sendfriends,
                        'sendgroups': sendgroups})

    return context