Python-Django cannot interpret the id in an URL and give "<built-in function id>" instead of redirecting on the rigth page -
i have simple app creates events , add attendees them built django 1.8. work on virtualenv python3. when create event, i'm redirected detail page of event , have possibility add attendees. when add one, django cannot redirect me page of event, cause cannot interpret id , gives me <built-in function id>
.
my app event :
models.py
class event(models.model): name = models.charfield("event title", max_length=255) organizer = models.foreignkey(user) attendees = models.manytomanyfield(user, verbose_name="attendees", blank=true, through="eventattendees", related_name="%(app_label)s_%(class)s_attendees") def get_absolute_url(self): return reverse('event:detail', kwargs={'pk':self.pk}) def delete_url(self): return reverse('event:delete', kwargs={'pk':self.pk}) def update_url(self): return reverse('event:update', kwargs={'pk':self.pk}) def __str__(self): return self.name class meta: verbose_name = 'event' verbose_name_plural = 'events' class eventattendees(models.model): event = models.foreignkey(event) attendee = models.foreignkey(user) def delete_url(self): return "/event/%i/attendee/%i/delete/" % (self.event.id, self.attendee.id) class meta: unique_together = ( 'event', 'attendee', )
forms.py
class eventform(modelform): class meta: model = event fields = ('name',) class eventattendeesform(modelform): class meta: model = eventattendees fields = ('event', 'attendee',) def __init__(self, *args, **kwargs): super(eventattendeesform, self).__init__(*args, **kwargs) self.fields['event'].widget = hiddeninput() if 'event' in self.initial: attendees = [user.pk user in self.initial['event'].attendees.all()] self.fields['attendee'].queryset=user.objects.exclude(pk__in = attendees)
views.py
class eventcreateview(createview): model = event form_class = eventform template_name = 'evenement/event/create.html' def form_valid(self, form): obj = form.save(commit=false) o = user.objects.get(username=self.request.user) obj.organizer = o obj.save() return httpresponseredirect(obj.get_absolute_url()) def delete_attendee(request, event_id, attendee_id): if request.method == 'post': event = event.objects.get(pk = event_id) attendee = user.objects.get(pk = attendee_id) to_be_deleted = eventattendees.objects.get( event = event, attendee = attendee) to_be_deleted.delete() if request.is_ajax(): return httpresponse("ok") return httpresponseredirect('/event/%s/detail/' % event_id) class eventdetailview(detailview): model = event template_name = 'evenement/event/details.html' def get_context_data(self, **kwargs): context = super(eventdetailview, self).get_context_data(**kwargs) form = eventattendeesform(initial = {'event':self.object}) attendees = [user.pk user in self.object.attendees.all()] form.fields['attendee'].queryset = user.objects.exclude(pk__in = attendees) form.fields['event'].widget = hiddeninput() context['form'] = form return context def post(self, request, *args, **kwargs): form = eventattendeesform(self.request.post) if form.is_valid(): form.save() return httpresponseredirect('/event/%s/detail/' %id) else: return render(request, 'evenement/event/details.html', {'event': form.instance.event, 'form': form})
i think problem located in function post of class eventdetailview, here
return httpresponseredirect('/event/%s/detail/' %id)
python cannot interpret %id
i wanted join app, don't know how to. many answer.
you have id of item, you're not doing.
if form.is_valid(): obj = form.save() return httpresponseredirect('/event/%s/detail/' % obj.id)
Comments
Post a Comment