Hi all,
I believe that I now have found a way to use the Django admin edit
form, addressed from an external page, and then get returned back to
the external page, rather than the default admin listing of records.

I got a first good hint from Malcolm, and after a little tweaking of
URL configs and a making custom "relay" view, I got it working. The
general idea is to intercept the URL pointing at the admin listing of
records and point to a custom view that directs the user back to the
external page.

But as I wanted the admin listing to be left untouched if you in fact
arrived to the admin edit from the admin listing of records, I had to
make a new URL pointing at it, to get there without disturbance of my
own previous interception.

This solution may not be rock-solid for the future, since it involves
a bit of tampering with the URL to the admin list, but I want to post
it here as a general idea, if someone else want to do something like
this. Maybe you can give me some feedback and suggestions for
improving the solution and/or making it more stable.

This is how the code looks:

The URLConf pointing at my relay view for external linking:
----------------------------------------------------------
from django.conf.urls.defaults import *
urlpatterns = patterns('kvalster.publications.views',
    (r'^publication/(?P<publication_id>\d+)/admin_edit_relay',
'admin_edit_relay'),
)
-------------------------------------

The URLConf pointing at the relay for linking back:
--------------------------------------------------
from django.conf.urls.defaults import *
urlpatterns = patterns('',
    (r'^admin/(publications)/(publication)/list$',
'django.contrib.admin.views.main.change_list'),
    (r'^admin/publications/publication/$',
'kvalster.publications.views.admin_edit_relay'),
    (r'^admin/', include('django.contrib.admin.urls')),
)
-------------------------------------

And the relay view itself:
#
======================================================================================
def admin_edit_relay(request, publication_id = ''):
    '''  Redirects to the admin edit page, and sets referrer as return
address
         Redirects from the admin list, if user entered from a custom
publication list '''

    # Check if user is coming from outside of admin area
    if request.META['HTTP_REFERER'].find('admin') < 0 :

        # Set an URL to return to, after editing, as a session
parameter
        request.session['return_to'] = request.META['HTTP_REFERER']

        # Send user to admin edit page
        return HttpResponseRedirect('/admin/publications/publication/'
+ publication_id)

    # If user is coming from admin edit
    else:

        # If we have a custom return to address set
        if 'return_to' in request.session:

            str_return_to = request.session['return_to']
            del request.session['return_to']
            return HttpResponseRedirect(str_return_to)

        # Otherwise, return to ordinary admin list, with new custom
URL
        else:

            return HttpResponseRedirect('/admin/publications/
publication/list')

#
======================================================================================

This is how it works from an external page:

1. Links from the external publication list points to my relay view (/
publications/<id>/admin_edit_relay).
2. The relay view sets a session parameter return_to that has the
value of the referring URL and sends user on to admin edit page.
3. After editing and returning to the admin list of records my URLConf
intercepts and sends user to the relay once again, this time using the
session parameter to redirect the user to the initial page.

This is how it works from internal admin edit:

1. When you enter the URL /admin/publications/publication/ the relay
view intercepts, but as it sees that we are coming from the admin area
and don't have the session parameter return_to set, it redirects to
the URL /admin/publications/publication/list which works fine for
sending directly to the admin list view as long as parameters with
application and model name is sent with it in the URLConf:
    (r'^admin/(publications)/(publication)/list$',
'django.contrib.admin.views.main.change_list')
2. The same applies when you are returning from the admin edit page;
the URL interception take an extra turn and makes the URL to /admin/
publications/publication/list , and thus stays at the admin listing.

Hope this can be of any use to anyone.

Regards,
Ulf

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to