django-distill icon indicating copy to clipboard operation
django-distill copied to clipboard

Using django contrib's Redirects

Open thijsdezoete opened this issue 1 year ago • 5 comments

Hi there! Awesome project, I am a big fan!

However I would either like to know how I can contribute to support django's contrib Redirects in distill, or how to properly implement a redirect in a project.

I have tried a few options, but I still seem to get stuck and can't quite figure out where I'm going wrong:

Using a view function like so:

def provider_redirect_view(request, new_url):
    html = f'<html><head><meta http-equiv="refresh" content="0;url={new_url}" /></head><body></body></html>'
    return HttpResponse(html)

And then looping over all the redirects:

for redirect in Redirect.objects.all():
    print(f"Generating pattern for: {redirect.old_path}")
    unique_name = f"redirect_{redirect.id}"

    # Each tuple should match the arguments expected by the view
    def x_distill_func(new_url=redirect.new_path):
        return [(new_url,)]

    urlpatterns.append(
        distill_path(
            redirect.old_path.lstrip('/'),
            provider_redirect_view,
            name=unique_name,
            distill_func=x_distill_func
        )
    )

Any help is much appreciated, or any hints as to how I can proceed. All I'm really trying to do is have distill generate a small html file for each redirect with a refresh code to another url (following the django contrib redirect model old_path and new_path variables).

thijsdezoete avatar Nov 28 '23 00:11 thijsdezoete