如果表单有变化则触发。 Django的

时间:2017-03-19 20:38:52

标签: django

我在使用票务系统时遇到了麻烦。用户创建票证并将信息发送到他们的电子邮件。如何从管理面板更改故障单时发送新信息?

模型:

class TroubleTicket(models.Model):
    title = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    address = models.CharField(choices=ADDRESSES, default=None,  
    max_length=200)
    room = models.CharField(max_length=50)
    message = RichTextUploadingField()
    state = models.CharField(choices=STATES, max_length=30,   
    default='New')
    answer = RichTextUploadingField(blank=True, null=True)
    email = models.CharField(max_length=200, blank=True, null=True)
    create_date = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('ticket_detail', kwargs={'pk': self.id})

    class Meta:
        ordering = ['-create_date']

保存表单并在用户指定邮件时发送消息:

class CreateTicket(CreateView):
    template_name = 'incidentjournal/add_ticket.html'

    form_class = TicketForm
    model = TroubleTicket
    context_object_name = 'ticket'
    success_url = reverse_lazy('home')

    def form_valid(self, form):
        recipient = form.cleaned_data['email'].encode('utf8')
        new_ticket = form.save()
        new_ticket_id = new_ticket.pk
        address = new_ticket.address.encode('utf8')
        title = new_ticket.title.encode('utf8')
        name = new_ticket.name.encode('utf8')
        room = new_ticket.room.encode('utf8')

        send_to_email(recipient, str(new_ticket_id), title, address,  
        room, name)

        send_to_bot(str(new_ticket_id), title, address, room, name)

        return super(CreateTicket, self, ).form_valid(form)

如果在管理面板中更改了状态或答案,我想将更改发送给用户。

感谢您的建议。

0 个答案:

没有答案