2

我阅读了 django-docs 但仍然无法弄清楚,我错过了什么并且做错了什么?当我转到 url: url(r'^(?P<calendar_id>\d+)/edit-event/(?P<pk>\d+)/$', views.UpdateEventView.as_view(), name='event_edit')并更新表单时,它会创建一个新事件而不是更新。编码:

模型.py:

class Event(models.Model):
    calendar = models.ForeignKey(Calendar, verbose_name=_("Calendar"), related_name="events")
    message = models.ForeignKey(Message, verbose_name=_("Message"), related_name="events")
    recipient = models.ForeignKey(Contact, verbose_name=_("Recipient"), related_name="events")
    event_date = models.DateField(_("Date"))
    start_time = models.TimeField(_("Start time"))
    end_time = models.TimeField(_("End time"), blank=True, null=True)
    content = models.TextField(_("Event Notes"), blank=True, null=True)
    location = models.CharField(_("Location of meeting"), blank=True, null=True, max_length=100)
    # recurring_options = models.IntegerField(choices=RecurringOptions.CHOICES, verbose_name=_("Recurring time"))

    class Meta:
        verbose_name = _("Event")
        verbose_name_plural = _("Events")

    def __unicode__(self):
        return self.recipient.name

    @models.permalink
    def get_absolute_url(self):
        return ("events", (str(self.calendar.pk),))

    @models.permalink
    def get_edit_url(self):
        return ("event_edit", (str(self.calendar.pk), str(self.pk),))

视图.py:

class UpdateEventView (EventMixin, UpdateView):

    model = Event
    form_class = CreateEventForm

表格.py:

class CreateEventForm(forms.ModelForm):

    add_new_contact = forms.BooleanField(label=ugettext("Add new contact name") , required=False)
    new_contact_name = forms.CharField(label=ugettext("New contact name") , required=False)
    new_contact_phone = forms.CharField(label=ugettext("New contact phone") , required=False)
    add_new_message = forms.BooleanField(label=ugettext("Add new message") , required=False)
    new_message = forms.CharField(label=ugettext("New message") , required=False)
    reminder = forms.TypedMultipleChoiceField(label=ugettext("Reminder Time"),
                                         choices=ReminderOptions.CHOICES,
                                         widget=forms.CheckboxSelectMultiple,
                                         coerce=int,
                                         required=True)

    class Meta:
        model = Event
        fields = (
                    'recipient',
                    'event_date',
                    'start_time',
                    'message',
                    )

    def __init__(self, calendar, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_class = 'form-inline'
        self.helper.form_action = reverse('event_create', args=(str(calendar.id),))
        self.helper.layout = Layout(
                                    HTML("""
                                    <div id="div_id_recipient_add" class="control-group">
                                    <a href="#addContact" role="button" class="btn btn-warning" data-toggle="modal">+</a>
                                    </div>
                                    """),
                                    Field('recipient'),
                                    placeholder=_("add_new_contact")),
                                    Field('event_date', placeholder=_("Date")),
                                    Field('start_time', placeholder=_("Start time")),
                                    HTML("""
                                    <div id="div_id_message_add" class="control-group">
                                    <a href="#addMessage" role="button" class="btn btn-warning" data-toggle="modal">+</a>
                                    </div>
                                    """),
                                    Field('message', placeholder=_("Message")),
                                    placeholder=_("add_new_message")),
                                    Field('reminder'),

                                    )       


        self.helper.add_input(Submit('submit', _('Add')))

        super(CreateEventForm, self).__init__(*args, **kwargs)
        self.fields['recipient'].queryset=calendar.contacts.all()
        self.fields['message'].queryset=calendar.messages.all()

class UpdateEventForm(forms.Form):

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_class = 'form-inline'
        self.helper.add_input(Submit('submit', _('Update')))

        super(CreateEventForm, self).__init__(*args, **kwargs)
        submit_button_text = _('Save')
4

0 回答 0