在 Django 的管理界面中发布表单

发布于 2024-08-18 06:46:13 字数 1813 浏览 4 评论 0原文

我正在编写一个 Django 管理操作来批量发送电子邮件联系人。该操作定义如下:

def email_selected(self,request,queryset):
    rep_list = [] 
    for each in queryset:

        reps = CorporatePerson.objects.filter(company_id = Company.objects.get(name=each.name))

        contact_reps = reps.filter(is_contact=True)
        for rep in contact_reps:
            rep_list.append(rep)

    return email_form(request,queryset,rep_list)

email_form 作为视图存在,并使用以下代码填充模板:

def email_form(request,queryset,rep_list):
    if request.method == 'POST':
        form = EmailForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email','noreply@localboast'),['[email protected]'],
            )
            return HttpResponseRedirect('thanks')
        else:
            form = EmailForm()
        return render_to_response('corpware/admin/email-form.html',{'form':form,})

模板存在如下:

<body>
    <form action="/process_mail/" method="post">
        <table>
            {{ form.as_table }}
        </table>
        <input type = "submit" value = "Submit">
    </form>
</body>

/process_mail/ 硬链接到 urls.py 中的另一个视图 - 这是一个问题。我真的很喜欢它,这样我就不必使用

但不幸的是我似乎无法发布用户输入到视图处理程序,而无需在其位置重新加载模型的管理界面(当我点击提交按钮时,会出现管理界面,这是我不想要的。)

有没有一种方法可以使表单 POST到其自身(

)以便我可以处理在 email_form 中收到的输入?尝试使用无关的 URL 和不需要的函数处理输入让我很困扰,因为我正在对 URL 进行硬编码以使用代码。

I'm writing a Django admin action to mass e-mail contacts. The action is defined as follows:

def email_selected(self,request,queryset):
    rep_list = [] 
    for each in queryset:

        reps = CorporatePerson.objects.filter(company_id = Company.objects.get(name=each.name))

        contact_reps = reps.filter(is_contact=True)
        for rep in contact_reps:
            rep_list.append(rep)

    return email_form(request,queryset,rep_list)

email_form exists as a view and fills a template with this code:

def email_form(request,queryset,rep_list):
    if request.method == 'POST':
        form = EmailForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email','noreply@localboast'),['[email protected]'],
            )
            return HttpResponseRedirect('thanks')
        else:
            form = EmailForm()
        return render_to_response('corpware/admin/email-form.html',{'form':form,})

and the template exists as follows:

<body>
    <form action="/process_mail/" method="post">
        <table>
            {{ form.as_table }}
        </table>
        <input type = "submit" value = "Submit">
    </form>
</body>

/process_mail/ is hardlinked to another view in urls.py - which is a problem. I'd really like it so that I don't have to use <form action="/process_mail/" method="post"> but unfortunately I can't seem to POST the user inputs to the view handler without the admin interface for the model being reloaded in it's place (When I hit the submit button with , the administration interface appears, which I don't want.)

Is there a way that I could make the form POST to itself (<form action="" method="post">) so that I can handle inputs received in email_form? Trying to handle inputs with extraneous URLs and unneeded functions bothers me, as I'm hardcoding URLs to work with the code.

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

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

发布评论

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

评论(1

眼角的笑意。 2024-08-25 06:46:13

您可以使用 django 的内置 url 标签来避免硬编码链接。参见...

http://docs.djangoproject.com/en /dev/ref/templates/builtins/#url

您可能最好设置一个由 cron 作业触发的群发邮件程序,而不是在帖子上触发。

查看我在这里发布的答案
Django 计划作业

另外,如果你坚持在视图更新时触发 email_send 函数,也许看看在

http://docs.djangoproject.com/en/dev/topics/signals/

You can use django's inbuilt url tag to avoid hardcoding links. see...

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#url

Chances are you'd be better off setting up a mass mailer to be triggered off by a cron job rather than on the post.

Check out the answer I posted here
Django scheduled jobs

Also if you insist on triggering the email_send function on a view update perhaps look at

http://docs.djangoproject.com/en/dev/topics/signals/

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