Sunday, December 30, 2018

Re: django admin site change_related_template_url problem

You're better off not using the HTML rewriting features of your reverse proxy. Instead configure Django to use the reverse proxied url prefix for all it's URLs.

On December 29, 2018 9:03:39 AM CST, Zhe Li <lizhe05@gmail.com> wrote:
Hi,

I am having issues with django installed on a server reversed proxied to the main website. Most of the sites work just fine except a very small detail in the admin site, change form page.

Django is running on server A, served at / on port 12345. Server B which is the main web server and has a domain www.example.com, and serves content (reverse proxies) from server A under /app/myapp.

i.e. http://A:12345/admin/ translates to https://www.example.com/app/myapp/admin.

Everything is fine except when I try to modify a foreign key value from the model that it is referenced. You know, if I modify fields of model A in the django admin site, I can click on these buttons to change a foreign key model B.

Screen Shot 2018-12-29 at 9.45.42 AM.png





The problem is that the links are not properly translated on server B. The "add" button is fine, but the "change" and the "delete" buttons are not. For example, the correct "change" button should point to https://www.example.com/app/myapp/admin/main/modelb/1/change/?_to_field=id&_popup=1, but it actually points to https://www.example.com/admin/main/modelb/1/change/?_to_field=id&_popup=1, which does not exist. The problem is that the relative URL is stored in the "data-href-template" attribute of the <a> tag, like this

<a class="related-widget-wrapper-link change-related" id="change_id_modelb" data-href-template="/admin/main/modelb/__fk__/change/?_to_field=id&amp;_popup=1" title="Change selected modelb">

which is parsed by static/admin/js/admin/RelatedObjectLookups.js, which adds a "real" href attribute via JS, giving

<a class="related-widget-wrapper-link change-related" id="change_id_system" data-href-template="/admin/main/modelb/__fk__/change/?_to_field=id&amp;_popup=1" title="Change selected modelb" href="/admin/main/modelb/1/change/?_to_field=id&amp;_popup=1"><img src="/app/myapp/static/admin/img/icon-changelink.svg" alt="Change"></a>

The reverse proxy program, however, only translates whatever is in the HTML <a href="" />, and does nothing about this customized data field.

This is not necessarily a bug, as it only happens in special cases like mine. I am not sure what to do -- I can modify this RelatedObjectLookups.js and just add '/app/myapp' in front of every link it produces, which should work and fix this problem at this point. However, a more complete solution would be to overhaul this lookup js and put a real link there, which is quite some work to do. Should I write a new ticket to Django Project, or do you have better suggestions?

Thank you

No comments:

Post a Comment