Redirect to admin

I have an application called CMS with category and article. Simple enough.

I am rewriting app_index.html to enable app_index.html by app_index.html based on ajax and moving articles from one category to another.

Now I would like to redirect after saving / deleting an article or category in cms' app_index.html instead of the change_list.html model. How can this be done?

thanks

 class Category(models.Model): order = models.IntegerField() title = models.CharField(max_length=100) text = models.TextField() class Article(models.Model): published = models.BooleanField(default=None) images = generic.GenericRelation(Photo) category = models.ForeignKey(Category) title = models.CharField(max_length=100) text = models.TextField() order = models.IntegerField(blank = True, null = True) 

Daniel's answer did half the job: redirecting after changing articles and categories.


Not very elegant solution: redirecting to urls.py

 def redirect_cms(response): return HttpResponseRedirect('/admin/cms/') urlpatterns += patterns('', (r'^admin/cms/article/$',redirect_cms), 

any other idea?

+5
source share
3 answers

In the admin subclass, override the response_change and / or response_add methods. They are called after saving the administrator form for existing and new instances, respectively, and are responsible for returning the HttpResponseRedirect , which currently takes you to the change list page.

Take a look at the source code in django.contrib.admin.options to find out what you need to do.

Change There are two ways to delete: as a result of the action on the change_list page, in which case you can use the response_action method; or as a result of deletion in the form of a change, in which case, unfortunately, there is no equivalent method. One way to deal with this may be to override the change_form.html template for the application and remove the delete link, so the only way to remove it is through the list of changes. Not perfect by any means.

+10
source

The question has already been given, but the problem of redirection after removal is not considered. Perhaps the following solution will be useful for someone. The Django ModelAdmin has a member function delete_view . It performs two functions:

  • If a POST not defined, a delete confirmation page is displayed. If you confirm and click "Delete", it generates a POST request.
  • As POST is now defined, the view deletes the requested items and returns the HttpResponseRedirect to the change list (or the administrator index if the user does not have permission to change).

So, we redefine it as follows.

 from django.http import HttpResponseRedirect class MyAdmin(admin.ModelAdmin): def delete_view(self, request, object_id, extra_context=None): """Redirect to website index page on delete.""" response = super(MyAdmin, self).delete_view(request, object_id, extra_context) # Use our own redirect if isinstance(response, HttpResponseRedirect): return HttpResponseRedirect('/') # Any URL here return response 

Django 1.6 seems to work for me.

+5
source

As in Django 1.7, there is a response_delete method that can be overridden for the admin object. See here

 class MyAdmin(admin.ModelAdmin): def response_delete(self, request, obj_display): return HttpResponseRedirect("my_url") 
+4
source

All Articles