[Answer]-How to tell a Django view who called it?

1👍

✅

You can have a part of the URL to be used as a parameters. Each parameter will be set based on the regex provided. an example here. One view can handle all your urls that contain the three parameters you mentioned.

url(r'^members/near/(?P<from_uid>\d+)/profile/(?P<to_uid>\d+)/from//(?P<view_name>\W+)/$', MyView.as_view(), name = 'my_named_view')

Then in your view you just pull these parameters from the url

from_uid = self.kwargs['from_uid']
to_uid = self.kwargs['to_uid']
view_name = self.kwargs['view_name']

if view_name == "....":
    # render to template1
elif view_name == "....":
    # render to template2

0👍

Use GET parameters like this…

Template 1:
<a href="/third/view/?from=view1">Link</a>

Template 2:
<a href="/third/view/?from=view2">Link</a>

And in your third view…
from_view = self.request.GET.pop('from')
if from_view == 'view1':
...
elif from_view == 'view2':
...

GET parameters are more appropriate than URL-captured parameters in a situation like this.

Leave a comment