0

I like SO's effect on when you click a notification from the top-left where it says "Stack Exchange", then the SO will open a new page, jump to the specific section, then highlight the section in yellow and fade out.

Currently I have a comment form that is on the bottom of a long website. And when the comment posting fails the validation, I have jQuery to scroll to the form and do the same yellow highlight effect as SO.

Here is my (perfectly working) code:

$(document).ready(function() {
    {% if focus %}
        // Focus
        $('html, body').animate({scrollTop: $('#{{ focus }}').offset().top }, 'slow')
        $('#{{ focus }}').effect("highlight", {}, 3000)
    {% endif %}
})

I have this code in my base.html so every template checks for a template variable called focus. focus is simply a string that holds the id of the <div> to do the yellow highlight effect.

As you can see I feel this is a bit hacked. I need to pass focus in a dictionary using render() in Django's views.py.

What I really want to do is pass it as a GET variable. Something like http://www.example.com/orders/2/?focus=comment_form. Then I can get that GET variable using Javascript and do the highlight effect. But I don't know how to pass a GET variable using render().

Currently my render() looks like this:

dictionary = get_orders_detail_dictionary(order=order, user=request.user, comment_form=form)
dictionary['focus'] = 'comment_form'
return render(request, 'doors/orders/detail.html', dictionary)

The reason I want to use a GET is for like "permalinking" options.

Community
  • 1
  • 1
hobbes3
  • 28,078
  • 24
  • 87
  • 116

1 Answers1

2

Since it's a GET variable, it's available from request.GET. And since you're using the render shortcut, you can enable the request context processor to make the request object available in your templates, at which point you can just do {{ request.GET.focus }}.

However, to me a better option would be to use your own custom context processor which extracts the focus param into a context variable:

def get_focus(request):
    return {'focus': request.GET.get('focus', '')}

and then you can use {{ focus }} exactly as you do now.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for the answer. I guess here is the stupid question: How do I add a `GET` variable to the `render` shortcut? Currently I only know how to pass template variable as a dictionary. – hobbes3 Apr 23 '12 at 14:37
  • That question makes no sense at all, sorry. I've shown you how to pass the template variable via a context processor, I really can't imagine what else you're asking. – Daniel Roseman Apr 23 '12 at 15:14
  • Well I'm reading the ["Request and response objects" documentation](https://docs.djangoproject.com/en/1.4/ref/request-response/) and from what I read it seems like you do something like `request.GET = QueryDict('focus=comment_form')` then do `render(request, 'doors/orders/detail.html', dictionary)` to add a `GET` variable to `request`? – hobbes3 Apr 23 '12 at 16:56
  • Still can't see what that is supposed to achieve. Changing that parameter won't affect what your Javascript sees in any way, since the Javascript runs on the client and looks at the actual URL of the page. But since your URL already has `?focus=comment_form` the Javascript wil already be able to see that. – Daniel Roseman Apr 23 '12 at 20:50