[Answered ]-Allow page to only be loaded once in django

1πŸ‘

βœ…

A possible solution is to store a value in the user’s session.
It should be pretty easy assuming you have a separate view for each page.

Set the session variable in the signup view, after a successful signup (before redirecting):

request.session['conversion'] = True

And then add the value to the context in the thank you page:

return direct_to_template(request, template_name, {
    'conversion': request.session.pop('conversion', False),
})

Then you simply wrap the conversion tracking code in an if statement in the template:

{% if conversion %}
    {# Conversion tracking script #}
{% endif %}

I think that this is more reliable then checking the referrer.

πŸ‘€yprez

1πŸ‘

Checking HTTP_REFERER is pretty reliable; check the referrer in your view:

data = {
    'foo': 'bar',
    ...
}
if request.META.get('HTTP_REFERER', False) == reverse('signup'):
    data.update({'conversion': True})
render(request, 'thanks.html', data)

then use that information in your template to determine whether to detect a conversion:

{% if conversion %}<script type='text/javascript'> // record conversion</script>{% endif %}

Although Yuri’s answer is more generally correct (as it ensures that each conversion will be recorded at least once, as long as the user visits the thank-you page within a reasonable time-frame), it will involve a hit to session storage every time. Use either, or both ;)

πŸ‘€supervacuo

Leave a comment