-8
def change_view(self, request, object_id,extra_context=None):
result = super(mymodeladmin, self).change_view(request, object_id, extra_context)
result['Location'] = "your location"
return result
105
To change the redirect destination after save in the admin, you need to override response_add()
(for adding new instances) and response_change()
(for changing existing ones) in the ModelAdmin
class.
See the original code in django.contrib.admin.options
.
Quick examples to make it clearer how to do this (would be within a ModelAdmin class):
from django.core.urlresolvers import reverse
def response_add(self, request, obj, post_url_continue=None):
"""
This makes the response after adding go to another
app's changelist for some model
"""
return HttpResponseRedirect(
reverse("admin:otherappname_modelname_changelist")
)
def response_change(self, request, obj, post_url_continue=None):
"""
This makes the response go to the newly created
model's change page without using reverse
"""
return HttpResponseRedirect("../%s" % obj.id)
- [Django]-How can I check if a Python unicode string contains non-Western letters?
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-How to get the current URL within a Django template?
44
To add to @DanielRoseman’s answer, and you DON’T want the user redirected when they choose Save and continue and not the Save button, then you could use this solution instead.
def response_add(self, request, obj, post_url_continue="../%s/"):
if '_continue' not in request.POST:
return HttpResponseRedirect(get_other_app_url())
else:
return super(MyModelAdmin, self).response_add(request, obj, post_url_continue)
def response_change(self, request, obj):
if '_continue' not in request.POST:
return HttpResponseRedirect(get_other_app_url())
else:
return super(MyAdmin, self).response_change(request, obj)
- [Django]-Django setting for default template tag output when variable is None?
- [Django]-Django project models.py versus app models.py
- [Django]-Default filter in Django model
1
Given the previous answers I would suggest to override response_post_save_add
and response_post_save_change
methods. So you don’t skip django’s default logic in response_add
and response_change
- [Django]-Should I avoid multi-table (concrete) inheritance in Django by any means?
- [Django]-How to update an object from edit form in Django?
- [Django]-Create a field whose value is a calculation of other fields' values
0
Maybe take a look at URL namespaces… You could probably use HttpResponseRedirect + reverse to redirect the user inside of your overridden save method. Keep in mind that this is a new feature in Django 1.1 and is not supported in 1.0.x.
http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces
- [Django]-Display only some of the page numbers by django pagination
- [Django]-Is it okay to set instance variables in a Django class based view?
- [Django]-'RelatedManager' object is not iterable Django