Redirect to a different app

I have the following redirect:

    def GoToActivity(request, activity_id):

        return HttpResponseRedirect(reverse('Activity:ActivityInstance'), args=(activity_id,))

The Activity app has the following urls.py:

from django.urls import path

from .activities import ActivityView

app_name = "Activity"

urlpatterns = [
    path('ActivityInstance/<int:activity_id>', ActivityView.as_view(), name='ActivityInstance'),
]

and I’m getting this error:

I’ve followed URL dispatcher | Django documentation | Django as best I can, but I’m stumped now.

I’m guessing I’ve missed something simple, being a newbie, but I’m struggling to work out why it’s not recognising the parameter “activity_id”.

I think your args should be an argument to reverse() and not to the HttpResponseDirect.
Quoting an example from the documentation on reverse:

def myview(request):
    return HttpResponseRedirect(reverse('arch-summary', args=[1945]))

Ta very…well spotted. All working now :slight_smile:

P.S. Does anyone know why this function is called “reverse”? I can’t find an explanation and it’s not an intuitive name as far as I can tell…it’s not reversing anything I can see.

Yes, it’s a “reverse search” for a URL. Instead of specifying a URL, you’re finding a URL
by matching the name, rather than finding a view by matching a URL - the resolve function. So in a sense, you’re working backwards.

Basically just reinforcing what Ken already replied, but one way to think about it:

The first step for Django (or any web application framework for that matter) in processing a request is mapping an incoming URL to a view. In Django this is achieved via urls.py.

So in that context if you consider the of “URL → view” to be the ‘common’ direction of searching, then the opposite of that is done with reverse(), i.e. answering the question of “given the name of a certain view, which if requested will be routed back to that view?”.

Thanks for the explanations, I get that now. It’s a pity the documentation doesn’t explain the logic for the name in this way.