69👍
Create a class MyUpdateView
inheritted from UpdateView
and override get_success_url
method:
class MyUpdateView(UpdateView):
def get_success_url(self):
pass #return the appropriate success url
Also i like to pass such parameters like template_name and model inside of inheritted class view, but not in .as_view()
in urls.py
36👍
Had the same issue. Was able to get the paramater from self.kwargs as Dima mentioned:
def get_success_url(self):
if 'slug' in self.kwargs:
slug = self.kwargs['slug']
else:
slug = 'demo'
return reverse('app_upload', kwargs={'pk': self._id, 'slug': slug})
- [Django]-Efficient way to update multiple fields of Django model object
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Import error django corsheaders
8👍
I found a way which is useful and very simple. Check it out.
class EmployerUpdateView(UpdateView):
model = Employer
#other stuff.... to be specified
def get_success_url(self):
pk = self.kwargs["pk"]
return reverse("view-employer", kwargs={"pk": pk})
- [Django]-How to use the 'reverse' of a Django ManyToMany relationship?
- [Django]-Why SlugField() in Django?
- [Django]-What is a django.utils.functional.__proxy__ object and what it helps with?
4👍
Define get_absolute_url(self)
on your model. Example
class Poll(models.Model):
question = models.CharField(max_length=100)
slug = models.SlugField(max_length=50)
# etc ...
def get_absolute_url(self):
return reverse('poll', args=[self.slug])
If your PollUpdateView(UpdateView)
loads an instance of that model as object
, it will by default look for a get_absolute_url()
method to figure out where to redirect to after the POST
. Then
url(r'^polls/(?P<slug>\w+)/, UpdateView.as_view(
model=Poll, template_name='generic_form_popup.html'),
should do.
- [Django]-Django Background Task
- [Django]-TemplateDoesNotExist – Django Error
- [Django]-Where should utility functions live in Django?
0👍
Why don’t you add a ‘next’ parameter to your form (template) and catch it in your view. It’s common practice to achieve redirecting this way.
- [Django]-Celery Flower Security in Production
- [Django]-Django reverse lookup of foreign keys
- [Django]-How do I migrate a model out of one django app and into a new one?