I have a place in my Django app where I need to construct a callback to my domain after a third-party auth, but I’m stuck on how to do this since that view in question doesn’t really map to one model (or rather, the view code references multiple models), and the docs for get_absolute_url() construction and permalinks all reference models.
For instance, in my template I currently have something like:
<a class="btn btn-danger large" href="http://to/third/party?api_key=noneyobiz&cb=http://localhost:8000/signup">Join via Somethingorother</a>
the line for this view in urls.py is:
url(r'^signup/$', 'signup', name="signup"),
I want the hardcoded ‘http://localhost:8000/signup’ to be dynamic. I’m hoping this functionality doesn’t depend on my using generic views. Actually I don’t understand why generating a permalink is even tied to models at all, it seems like it should only depend on the urlconf. What am I missing here?
permalinkis a thin wrapper ofdjango.core.urlresolvers.reverse. Its belongs to django.db.models to be a shortcut because we usually writereverseinsideget_absolute_urlof models. So use reverse hereUpdate
To use absolute URI, you could
Site.objects.get_current()w/ the path you get fromreverseorurlto get the absolute URI, as Daniel suggested.If your callback URI is in the same domain w/ the view rendering the template, you could rely on
requestto get actual absolute URI:request.build_absolute_uri(reverse('signup'))Furthermore, you may want to escape the URI in template, like
{{ absolute_uri|urlencode }}. or in view throughurllib.quoteorurllib.urlencode