15π
I was looking at the wrong place.
I have to override form_valid
to not redirect to the URL (return HttpResponseRedirect(self.get_success_url())
)
def form_valid(self, form):
self.object = form.save()
# Does not redirect if valid
#return HttpResponseRedirect(self.get_success_url())
# Render the template
# get_context_data populates object in the context
# or you also get it with the name you want if you define context_object_name in the class
return self.render_to_response(self.get_context_data(form=form))
3π
I donβt think you need the object being created to redirect to the same URL of the view. Iβd use reverse:
class MyModelCreate(CreateView):
model = MyModel
success_url = reverse('path.to.your.create.view')
- OSError: cannot load library 'gobject-2.0': error 0x7e
- Save Base64 String into Django ImageField
- {% trans "string" %} not working on templates but {% trans variable %} does
- Django calling REST API from models or views?
1π
So itβs 6 years later and I bumped into this problem while working on my project. In case anyone also faces this problem too in the near future here is a simple and easy fix but might not be recommended but it got the job done for me.
By the way I like using ClassBasedViews
.
In your views.py
file
class MyMode(CreateView):
model = my_model
success_url = '/path-to-webpage/'
So what I basically did was to hard-code in the path to the web-page under the success_url
and that got the problem solved.
This works when you are not planning to change your URLpatterns anytime otherwise you will also have to change the URL in the views.py
file too.
- Gunicorn not responding
- GAE and Django: What are the benefits?
- Passing arguments to management.call_command on a django view