Skip to content Skip to sidebar Skip to footer

Django: Getting NoReverseMatch While Submitting The Form, URL Has Slug

I am learning Django by building an application, called TravelBuddies. It will allow travelers to plan their trip and keep associated travel items (such as bookings, tickets, copy

Solution 1:

A better way is define a get absolute method in your activity model and call it in your views.

def get_absolute_url(self):
        return reverse("trips:activity", kwargs={"slug": self.slug})

and redirect in your views following way:

if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        return HttpResponseRedirect(instance.get_absolute_url())

Or if you use generic create view you can do as follows and give a success url

class TripCreateView(CreateView):
    model = Trip
    form_class = TripCreateForm
    template_name = 'your_remplate.html'

    def form_valid(self, form):
    ---------
    ---------
    return HttpResponseRedirect(self.get_success_url())

Post a Comment for "Django: Getting NoReverseMatch While Submitting The Form, URL Has Slug"