简体   繁体   中英

Best way to share data between node.js and django?

I have a variable in django settings.(eg settings.ALLOW_REDIRECT). I am running some tests (django and node.js tests) and the value of settings.ALLOW_REDIRECT would be changed after some tests.

What is the best way to let the node.js tests to access the value of the variable. I thought of using a conf file in which the value of variable can be stored and altered by django. This can be read by the node.js script. Is there any simpler method than this?

You can pass the value of settings.ALLOW_REDIRECT into your template render call:

def myview(request):
    ...
    return render(request, 'mytemplate.html', {'allow_redirect': settings.ALLOW_REDIRECT})

And then the template can check the value of allow_redirect and insert some small unique element into the page HTML to reflect its value:

{% if allow_redirect %}
    <div id="allow_redirect_is_true"> </div>
{% else %}
    <div id="allow_redirect_is_false"> </div>    
{% endif %}

And then your js code can check for the presence of an element with an id of allow_redirect_is_true or allow_redirect_is_false .

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