我有以下代码。我在OfferAdmin中有一个'send_offer'动作。
基本上,该操作会显示一个表单,当用户完成时,会向用户发送一封电子邮件。
但是,下面的代码对我不起作用。当我点击优惠旁边的复选框并从django admin的下拉菜单中选择“发送优惠电子邮件”时,我会看到该表单,但是没有发送给用户的电子邮件。
基本上,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"
答案 0 :(得分:0)
您需要处理表单提交的intermediate page。
由于您的代码当前正常工作,因此在提交表单时,请求由changelist视图处理
不是send_offer
功能。
编辑:另请注意,管理员操作始终会收到POST请求。