开发者

Can any one explain how can i pass arg or kwargs from redirect to another view?

开发者 https://www.devze.com 2023-03-17 21:35 出处:网络
I am trying to let the manager of a site I\'m developing send mail to the members and send get a confirmation page.

I am trying to let the manager of a site I'm developing send mail to the members and send get a confirmation page.

The send mail is working and sending the mail OK, but in the confirmation i want him to get the the list of people he sent the mail to.

So, i need to send the users_list from one function (view) to the other in the directs and read it in the other view sending it to render_to_response.

I've read a lot and tried reverse and many other things i read over the web but can't seem to make it work...

The code:

def send_members_mail(request):
    #a list of all active users in the system to show for send mail
    users_list = User.objects.filter(is_active = True)
    if request.method == 'POST':
        form = UsersForm(request.POST)
        if form.is_valid():
            user_list = []
            cd = form.cleaned_data
            #getting a list of user s that where chosen for send mail, -1 for all users
            for user_id in request.POST.getlist('chk_users'):
                user_list.append(user_id)
            #sending the mail to all list
            if '-1' in user_list:
                 users_list = User.objects.filter(pk__in=users_list).values_list('email', flat=True)
            #sending mail to specific members
            else:
                 users_list = User.objects.filter(pk__in=user_list).values_list('email', flat=True)

            cd = form.cleaned_data

            try:
                send_mail(
                    cd['subject'],
                    cd['message'],
                    'support@test.com',
                    users_list,
                )
            except BadHeaderError:
                return HttpResponse('The Mail header was corrupted, It might be because a server error or might be that you have a virus on your computer!!! please try sending again')
            return redirect('/contacts/sent/', users_list=users_list)

    else:
        form = UsersForm()
    return render_to_response('contacts/send_members_mail.html', {'form':form, 'users_list':users_list}, context_instance=RequestContext(request))

def sent(request, users_list,  *args, **kwargs):
    assert False, users_list
    return render_to_response('contacts/sent.html', context_instance=RequestContext(request))

The first view is for getting the list of users, and sending it to the form, then getting the data from the form including the check boxes that where checked and then getting the users mail addresses for the users (users login by mail, so have to have a working mail)开发者_高级运维 and then sending the mail to the users and redirecting to the send page (confirmation page) where the admin should get the list of users (THIS IS WHERE I AM GETTING STUCK, THIS AND THAT TO DO IN THE URLSCONF FILE)

thank you,

Erez


Rather then trying to pass it in the reverse/redirect you could set users_list as a session variable. e.g.

# before redirect
request.session['users_list'] = users_list

# in sent view
users_list = request.session.get('users_list', None)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号