On Aug 19, 9:16 am, Guy <[email protected]> wrote:
> I am having trouble with learning to use httpresponseredirect and
> reverse.
> I am trying to generate an admin action on a selection of a list of
> items. The purpose is to generate an intermediate window that can be
> used to select a new
> status for all the objects,
>
> The admin action shows up in the pulldown menu on /admin/items/item.
> However, once clicked, I get the following error: NoReverseMatch at /
> admin/items/item/
>
> Any advice would be greatly appreciated, I have been pounding me head
> for a day or so on this. I am certainly a novice, so this is likely
> to be a very newbie misunderstanding.
>
> Many thanks,
> Guy
>
> #myproject/app/admin.py
>
> class ItemAdmin(admin.ModelAdmin):
>
> fieldsets = ( #... )
>
> #set up management command
> def change_status(self, request, queryset):
> """call intermediary window to present admin with option to
> update
> the status for a batch of items. Basically it seems
> inefficient to
> make several 'admin actions' on the regular item list page,
> (one
> for each status type). Seems more logical to select change
> status from
> the admin action dropdown, and then have an intermediate
> window ask
> what the status should be"""
>
> return HttpResponseRedirect(reverse("change-item-status",
> kwargs = {'queryset': queryset, 'request': request,}))
>
> change_status.short_description = "change status of items in
> batch"
> actions = ['change_status']
>
> #myproject/urls.py
> from django.conf import settings
> from django.conf.urls.defaults import *
> from django.contrib import admin
>
> admin.autodiscover()
>
> urlpatterns = patterns('',
> url(r'^admin/items/item/changestatus/$',
> 'myapp.items.admin_views.changeitemstatus', kwargs = {},
> name="change-item-status")
> )
>
> #myproject/app/admin_views.py
> def changeitemstatus(request,**kwargs):
> """intermediary window to present admin with option to update the
> status
> and apply any notes to a batch of items"""
> template_name='admin/items/changeitemstatus.html'
> context = RequestContext(request,
> {'items': kwargs['queryset'],
> 'choices': item.status_choices})
>
> return render(template_name, context)
There seem to be a few confusions here. The arguments to a view are
those elements that are passed in the URL itself and extracted via the
urlconf regex. It is possible to insert extra arguments into the call
to the view by hard-coding them in the urlconf, as I think you are
trying to do with your `kwargs = {}`, but then there is no way of
specifying these dynamically - because the urlconf is matched against
the URL.
Next, what HttpResponseRedirect does - as the name implies - is send a
response to the browser, with an HTTP status code telling the browser
to request another URL, plus a Location header telling it what that
URL should be. So, again, any dynamic arguments you pass in (eg via
reverse) have to be things you can actually put into a URL.
So it isn't possible to arbitrarily insert the queryset as the
argument to reverse, as you attempt here, because there is no way to
insert that queryset into the URL in the redirect and extract it from
there in the urlconf.
Instead, you need to think about approaching this from a different
angle. If you want to preserve a queryset across requests, you will
need to save it somewhere - the session could be ideal for this.
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.