DjangoRestFramework HTTPS Links with Routers and Views

Is there an easy way to change out-of-box links for views and routers?

For example, in urls.py:

from rest_framework import viewsets, routers
...
class Activity_TypeViewSet(viewsets.ModelViewSet):
    model = Activity_Type
...
router.register(r'activity_types', Activity_TypeViewSet)
...
url(r'^', include(router.urls)),

This sets the url structure as:

{"activity_types": "http://odd.quantdevgroup.com/activity_types/"}

The question boils down to how easy it is to make a link:

{"activity_types": "**https**://odd.quantdevgroup.com/activity_types/"}

If I disabled http / port 80 and allowed only https / port 443, then my application needs to manually add https to the link after it cannot open http (because I disabled port 80).

+3
source share
1 answer

DRF reverse request.build_absolute_uri(url) URL-, URL- " , " ., , http, URL- HTTP HTTPS.

django , SECURE_PROXY_SSL_HEADER.

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

:

, , , . , . .

, : ):

Django -. - X-Forwarded-Proto . , , - . X-Forwarded-Proto Django, , HTTPS. , None HTTPS, , .

nginx X-Forwarded-Proto :

location / {
    # ... 
    proxy_set_header X-Forwarded-Proto $scheme;
}
+13

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


All Articles