Django/JavaScript: passing hidden variables to a page with window.location.href?
Apologies if this question is a bit convoluted.
I wan开发者_如何转开发t a Django page to display a banner message sometimes. Sometimes the user will arrive via window.location.href
, and then I would like the banner to display, with a message determined by the action the user just performed.
Details:
- I have a site-wide javascript listener that listens for scanner input (which presents as keyboard input).
- When it triggers, using jQuery I return or check out the item, dependent on its state. I do this via an Ajax POST request.
- Then I immediately take the user to the item's page on my site. I do this by setting
window.location.href
to the item's page, inside the response handler of the POST request. The item shows as available or checked out, but I want to show the user another message too... - ...I want to show a banner saying 'Item checked out' or 'Item returned'.
The last item is where I'm having problems: how do I 'tell' the item page what message to show, and when to show a message at all? People will also arrive at item pages not via the scanner.
- I could set GET parameters (
?t=checked_out
or?t=returned
) but that feels messy. - I could use cookies but that feels even messier.
- If I POST to the item page (which also feels wrong) with a
t=checked_out
parameter, wouldn't it be good Django practice to then redirect somewhere else, rather than display the page?
Perhaps I'm just too hung up on the last point.
Anyway, the basic question is: How best can I pass hidden variables to a page via window.location.href
?
Thanks for your help. I have the feeling there's something fundamental that I've yet to learn here :)
Why do an AJAX request at all for step 2/3? You asynchronously POST, then redirect.
Can you do a normal POST with info about whatever the javascript did, add some message in your session in the django backend (and have the item view load it), and do a server redirect to the item page?
The django way would definitely be to do it in django
sessions.
If you must, your method should be possible anyways:
- pass extra bits of information TO django in your ajax post
- set your "hidden variabes" to the django session (
request.session['myvar'] = 'ajax_posted_stuff'
) - javascript redirect (but seriously, it would be best to have the server redirect)
- pull "hidden variables" from the django session (
ajax_posted_stuff = request.session['myvar']
)
Example:
def ajax_view(request):
if successful_response():
request.session['show_banner'] = True
return JSON # or whatever you were doing before
def item_view(request):
context = {}
if request.session.get('show_banner'):
context['show_banner'] = request.session.pop('show_banner')
return render_to_response("mytemplate.html", context)
# item.html
{% if show_banner %}
<h1>Banner shown!</h1>
{% endif %}
Why do you think using a cookie would be messy? I'd say go for cookies if you can. You can read cookies from window.document.cookie
.
As an alternative to cookies, the cleanest solution could be to use the URL hash:
http://example.com/page#co
You can easily check for the presence of the hash with window.location.hash
.
精彩评论