简体   繁体   中英

go back to previous page in django

I want to save the data in the form and go back to previous page when I click on 'Add' button. It's showing some errors. Can anyone suggest the correct way to do it?

template.html

  <div class="panel panel-default">
  <div class="panel-heading">
    <h3 class="panel-title">Add your own Category </h3>
  </div>
  <div class="panel-body">
    <form method="POST" class="post-form">
      {% csrf_token %}
      {{ form.as_p }}

                    </div>
                      </div>
                    </div>
        <input type="hidden" name="next" value="{{ request.path }}">
       <button type="submit" class="save btn btn-default" >Add</button>
    </form>

views.py

class CustomCategory(LoginRequiredMixin,CreateView):
    model = Category

   form_class = CategoryForm

   def form_valid(self, form):
    obj = form.save(commit=False)

   def category(request):
    next = request.POST.get('next', '/')
    return render (request,HttpResponseRedirect(next))

Instead of adding the redirect as an element of the form, why don't you use the success_url field on the view, or if the URL you need to go to depends on an object, use the get_success_url method on the view?

https://docs.djangoproject.com/en/2.0/ref/class-based-views/mixins-editing/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM