0

我有下面的代码。我在 OfferAdmin 中有一个操作“send_offer”。

基本上,该操作会显示一个表单,当人们完成时,会向用户发送一封电子邮件。

但是,下面的代码对我不起作用。当我单击要约旁边的复选框并从 django 管理员的下拉菜单中选择“发送要约电子邮件”时,我会看到该表单,但未发送给用户的电子邮件。

基本上 request.method == 'POST' 行下方的代码部分不起作用。

我该怎么办?

class OfferAdmin:
...

    class OfferForm(forms.Form):
        title = forms.CharField(max_length=100)
        image_link = forms.CharField(max_length=100)
        list_price = forms.FloatField()
        discount_price = forms.FloatField()
        exclusive_price = forms.FloatField()
        comments = forms.CharField(widget=forms.widgets.Textarea())

    def send_offer(self, request, queryset):
        form = None

        if request.method == 'POST':
            form = self.OfferForm(request.POST)

            if form.is_valid():

                for bid in queryset:
                    title = form.cleaned_data['title']
                    image_link = form.cleaned_data['image_link']
                    list_price = form.cleaned_data['list_price']
                    discount_price = form.cleaned_data['discount_price']
                    exclusive_price = form.cleaned_data['exclusive_price']
                    comments = form.cleaned_data['comments']

                    form_params = {
                        'title': title,
                        'image_link': image_link,
                        'list_price': list_price,
                        'discount_price': discount_price,
                        'exclusive_price': exclusive_price,
                        'comments': comments
                    }

                    params = {
                        'obj': bid,
                        'form': form_params
                    }

                    rts = render_to_string
                    subject = rts(
                        'shopper/email/email-offer-subject.txt', params)
                    text = rts('shopper/email/email-offer.txt', params)
                    html = rts('shopper/email/email-offer.html', params)
                    bid.user.send_email(subject, text, html)

                    self.message_user(request, "Successfully sent email")

                    return HttpResponseRedirect(request.get_full_path())

        if not form:
            for obj in queryset:
                data = {
                    'title': obj.product.title,
                    'image_link': obj.image_link,
                    'list_price': obj.list_price,
                    'discount_price': obj.discount_price,
                    'exclusive_price': obj.exclusive_price,
                }
            form = self.OfferForm(data)

    return render_to_response(
        "shopper/admin/offer_form.html", {
            'bids': queryset, 'offer_form': form},
        context_instance=RequestContext(request))

send_offer.short_description = "Send offer email"
4

1 回答 1

0

您需要处理表单提交的中间页面

当您的代码当前工作时,提交表单时,请求由更改列表视图处理,而不是send_offer功能。

编辑:另外,请注意管理员操作总是收到 POST 请求。

于 2013-03-27T15:54:22.050 回答