Relative Django and https URLs

I have a Django project using https for a specific part of the URL (/ account /, / admin /, / purchase /).

When on one of these pages in https mode all relative internal links {% url foo%} will point to https: // my_url .

However, I do not want these pages to display as https: home, contacts ...

What are the solutions for this kind of requirement?

Providing an absolute URL?

http://{{ domain }}{% url foo %} 

not very nice.

+1
source share
5 answers

As Tomas suggests, one way to do this is to install middleware to redirect to and from https as needed. Here is one implementation - the idea is to decorate those views that should be served under https, and when the user switches to a view that should not be protected from one, the middleware automatically redirects them back to the http version of the page.

+4
source

Idea: you can use special middleware to redirect from https to http (or vice versa) for centners or URL patterns. This can also be done in the configuration of Apache (or another web server).

+4
source

You can use your web server to overwrite on http, so Django doesn't even need to know.

+2
source

I find this fragment takes great care of the situation. Views that need SSL will have them by redirecting from http to the https version of the url and vice versa.

Yes, on the https page, an outgoing link to a page other than https on your site will continue with https, but the user will be redirected to the http version.

(However, there is a question: it will not work if you send a message from http to https and vice versa)

+2
source
+2
source

Source: https://habr.com/ru/post/1339222/


All Articles