Reputation: 363
I am trying to create an event in FullCalendar by passing a timestamp into the url of a Django CreateView. However, after pressing submit on my form I keep getting a blank page and the error:
Method Not Allowed (POST): /fullcalendar/ambroses-calendar/
html:
dayClick: function(date, jsEvent, view) {
if($('#calendar').fullCalendar('getView').type != 'month') {
$("#occDiv").load("{% url 'edit_occurrence_short' 1234567898765 %}".replace(1234567898765, (new Date(date)).getTime())).dialog({
autoOpen: false,
modal: true,
bgiframe: true,
width: 800
});
$("#occDiv").dialog('open');
}
urls.py
url(r'^occurrence/add/(?P<date>\d+)/$',
ShortCreateOccurrenceView.as_view(),
name='edit_occurrence_short')
Views.py:
class ShortOccurrenceMixin(CalendarViewPermissionMixin, TemplateResponseMixin):
model = Occurrence
pk_url_kwarg = 'occurrence_id'
form_class = ShortOccurrenceForm
class ShortCreateOccurrenceView(ShortOccurrenceMixin, CreateView):
template_name = 'schedule/edit_occurrence_short.html'
def form_valid(self, form):
occurrence = form.save(commit=False)
start = datetime.datetime.fromtimestamp(self.kwargs.get('date', None)/1000.0)
end = start + datetime.timedelta(hours=1)
occurrence.start = start
occurrence.end = end
occurrence.save()
return HttpResponseRedirect('home')
Models.py:
class Occurrence(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE, verbose_name=_("event"))
title = models.CharField(_("title"), max_length=255, blank=True)
description = models.TextField(_("description"), blank=True)
start = models.DateTimeField(_("start"), db_index=True)
end = models.DateTimeField(_("end"), db_index=True)
cancelled = models.BooleanField(_("cancelled"), default=False)
original_start = models.DateTimeField(_("original start"), auto_now=True)
original_end = models.DateTimeField(_("original end"), auto_now=True)
created_on = models.DateTimeField(_("created on"), auto_now_add=True)
updated_on = models.DateTimeField(_("updated on"), auto_now=True)
Forms.py:
class ShortOccurrenceForm(forms.ModelForm):
class Meta(object):
model = Occurrence
fields = ('title', 'event')
Upvotes: 0
Views: 1332
Reputation: 363
The comments under the question led me to the right answer. Thank you very much @Willhen Van Onsem and @dirkgroten!
I was able to change the origin of the form POST by setting the action on the form in the html
...
<form action="{% url 'edit_occurrence_short' date %}" method="post" >
...
Upvotes: 1