简体   繁体   中英

Django relative urls and https

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

When on one of this page in https mode, all the relative inner links {% url foo %} will point to https://my_url .

However I do not want to have those pages shown as https :home, contacts ...

What are the solutions for this kind of requirements ?

Enforcing absolute url ?

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

is not too nice.

Idea: you can use a custom middleware to redirect from https to http (or vice versa) for centrain URLs or URL patterns. This could also be done in Apache (or other web server) configuration.

As Tomasz suggests, one way to do it is to set up middleware to redirect to and from https as necessary. Here's one implementation - the idea is to decorate those views that should be served under https, and when the user navigates to a view that shouldn't be secure from one that is, the middleware redirects them automatically back to the http version of the page.

可以使用您的网络服务器重写为http,这样Django甚至不需要知道。

I find this snippet takes care of the situation nicely. Views that need SSL will have them, via a redirect from the http to https version of the url, and vice-versa.

Yes, on a https page, the outbound link to a non-https page in your site will still start with https, but the user will be redirected to the http version.

(There is a gotcha, however: it won't work if you're posting from http to https and vice versa)

maybe this can serve for you

http://code.djangoproject.com/wiki/ContributedMiddleware#SSLMiddlewarebyStephenZabel

an contributed SSL Middleware

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM