以下のコードがあります。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"