In my url.py I have urls like:
url(r'^messstellen/monatlicher_verbrauch/(?P<pk>[0-9]+)/update/$',
generic.UpdateView.as_view(
model=MonatlicherVerbrauch,
form_class=MonatlicherVerbrauchForm,
success_url=reverse('messstellen:messstellen_index'),
template_name='messstellen/monatlich_form.html',
),
now I want to let the success_url
be something like:
success_url = redirect('messstellen:messtelle_detail', pk=pk)
where the pk schould be the same like in the regex pattern (?P<pk>[0-9]+)
Is there a way to do it in the url.py view?
If you don't define success_url
, then Django will use your model's get_absolute_url
method, which you could define as:
class MonatlicherVerbrauch(models.Model):
...
def get_absolute_url(self):
return reverse('messstellen:messtelle_detail', args=[self.pk])
If your get_absolute_url
points to a different url, then I don't think it is possible to set the success_url
dynamically in the urls. You will have to override the view, and define get_success_url
.
class MonatlicherVerbrauchUpdateView(UpdateView):
def get_success_url(self):
return reverse('messstellen:messtelle_detail', args=[self.object.pk])
# define these attributes in the view as well, to keep urls simple
model=MonatlicherVerbrauch,
form_class=MonatlicherVerbrauchForm,
template_name='messstellen/monatlich_form.html',
Then use MonatlicherVerbrauchUpdateView
in your urls instead of UpdateView
.
url(r'^messstellen/monatlicher_verbrauch/(?P<pk>[0-9]+)/update/$',
MonatlicherVerbrauchUpdateView.as_view()),
The advantage of subclassing the generic view is that it separates the logic of your views from the urls.