Using multiple keyword arguments with reverse()

43 views
Skip to first unread message

Derek

unread,
Jul 8, 2018, 7:59:35 AM7/8/18
to django-users
Hi

I am working with Django 1.11 and Python 3.4.

I am attempting to pass multiple keyword arguments from a reverse() call (from my app called `uploads`).

urls.py

urlpatterns = [
    url(
        regex=r'^add/$',
        view=views.add_new,
        name='add_new'),
    url(
        regex=r'^details/(?P<view>[a-z.]+)/(?P<mid>\d+)?$',
        view=views.upload_details,
        name='upload_details'),
]

views.py

def add_new(request)
    # ... process data via a form; create a result object
    return HttpResponseRedirect(
        reverse('uploads:upload_details',
                kwargs={'view': view, 'mid':result.pk})
    )    

def upload_details(request, view='site', mid=None):
    # ... run process based on view and mid args


When the `upload_details` view gets called, the 'mid' does not set (even though it has a valid integer value); examining the contrib/auth/decorators.py file in debug mode:

    def decorator(view_func):
        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            if test_func(request.user):
                return view_func(request, *args, **kwargs)

shows that the kwargs now only holds:
    {'view': 'site'}

(for reference, the browser address shows: http://127.0.0.1:8000/uploads/details/site/3 )

I am not sure how to pass through both keyword arguments in a way that Django retains and uses them?

Thanks
Derek

mottaz hejaze

unread,
Jul 8, 2018, 10:10:22 AM7/8/18
to Django users
try to use url('view function name' x y)

Derek

unread,
Jul 8, 2018, 1:30:05 PM7/8/18
to Django users
Hi

I am not quite sure what you are suggesting?  Do you think the url() in the urlpatterns should be changed - I have not seen a format like that?

Perhaps you can give an example as it relates to the code I posted.

Thanks
Derek

Melvyn Sopacua

unread,
Jul 8, 2018, 2:49:17 PM7/8/18
to django...@googlegroups.com
On zondag 8 juli 2018 13:58:33 CEST Derek wrote:

> I am attempting to pass multiple keyword arguments from a reverse() call
> (from my app called `uploads`).

There's no call to reverse in your code. But to do this:

reverse('upload_details', kwargs={'view': 'site', 'mid', 3})

> *urls.py*
>
> urlpatterns = [
> url(
> regex=r'^add/$',
> view=views.add_new,
> name='add_new'),
> url(
> regex=r'^details/(?P<view>[a-z.]+)/(?P<mid>\d+)?$',
> view=views.upload_details,
> name='upload_details'),
> ]
>
> *views.py*
>
> def add_new(request)
> # ... process data via a form; create a result object
> return HttpResponseRedirect(
> reverse('uploads:upload_details',
> kwargs={'view': view, 'mid':result.pk})
> )
>
> def upload_details(request, view='site', mid=None):
> # ... run process based on view and mid args
>
>
> When the `upload_details` view gets called, the 'mid' does not set (even
> though it has a valid integer value); examining the
> contrib/auth/decorators.py file in debug mode:
>
> def decorator(view_func):
> @wraps(view_func, assigned=available_attrs(view_func))
> def _wrapped_view(request, *args, **kwargs):
> if test_func(request.user):
> return view_func(request, *args, **kwargs)

And how is that relevant as well? I think you're missing some code in your
copy, cause this decorator also is not in your view code.

--
Melvyn Sopacua

Derek

unread,
Jul 9, 2018, 8:59:28 AM7/9/18
to django-users
Thanks Melvyn

The call to reverse() *was* included in my original code snippets: see below.

> return HttpResponseRedirect(
>         reverse('uploads:upload_details',
>                 kwargs={'view': view, 'mid':result.pk})

And that call does not work.

Derek

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/e2BIjOI44Bc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3786925.Ha4mB3saOt%40fritzbook.
For more options, visit https://groups.google.com/d/optout.

mottaz hejaze

unread,
Jul 9, 2018, 10:39:49 PM7/9/18
to Django users
def my_view(request):
...
return redirect('some-view-name', foo='bar')

Melvyn Sopacua

unread,
Jul 10, 2018, 3:10:18 PM7/10/18
to django...@googlegroups.com

On maandag 9 juli 2018 14:58:25 CEST Derek wrote:

> Thanks Melvyn

>

> The call to reverse() *was* included in my original code snippets: see

> below.

>

> > return HttpResponseRedirect(

> >

> > reverse('uploads:upload_details',

> >

> > kwargs={'view': view, 'mid':result.pk})

>

> And that call does not work.

 

Ah, totally read over it. But - it *does* work, otherwise upload_details wouldn't get called, since the url would not have a 3 in it.

 

Your problem is somewhere else and I still don't get how the decorator ties in. Try to reduce it to an mcve.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages