谁能解释一下如何将 arg 或 kwargs 从重定向传递到另一个视图?

发布于 2024-11-19 00:39:41 字数 2315 浏览 3 评论 0原文

我试图让我正在开发的网站的经理向会员发送邮件并发送获取确认页面。

发送邮件正在工作并且发送邮件正常,但在确认中我希望他获得他向其发送邮件的人员列表。

因此,我需要将 users_list 从一个函数(视图)发送到直接中的另一个函数(视图),并在另一个视图中读取它,将其发送到 render_to_response。

我已经阅读了很多内容并尝试了反向操作以及我在网上阅读的许多其他内容,但似乎无法使其工作...

代码:

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'],
                    '[email protected]',
                    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))

第一个视图用于获取用户列表,并将其发送到表单,然后从表单中获取数据,包括选中的复选框,然后获取用户的用户邮件地址(用户通过邮件登录,因此必须有可用的邮件),然后将邮件发送给用户并重定向到发送页面(确认页面),管理员应在其中获取列表用户(这是我遇到困难的地方,在 URLSCONF 文件中要执行此操作)

谢谢,

Erez

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'],
                    '[email protected]',
                    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2024-11-26 00:39:41

而不是尝试以反向/重定向方式传递它,您可以将 users_list 设置为 会话变量。例如

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

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文