django-analytical icon indicating copy to clipboard operation
django-analytical copied to clipboard

avoiding 'RequestContext' object has no attribute 'items'

Open ScottEAdams opened this issue 11 years ago • 3 comments
trafficstars

I use TEMPLATE_CONTEXT_PROCESSORS to chuck in some extra context for olark (nickname, email, fullname, translations etc). I have a whole bunch of views that do somthing like this:

def tutorials_view(request):
    return render_to_response('tutorials.html', RequestContext(request))

They throw the items error. Those I can fix to use the proper render or context_instance= methods as you recommend in the tutorials but it doesn't fix views I have from external modules and so forth.

However using regex on the context seems to work on all views:

def _get_configuration(self, context):
        code = []
        matches = re.findall(r"olark_[a-zA-Z_]*", str(context))
        for m in matches:
            key = m[6:]
            if key in MESSAGE_KEYS:
                code.append(MESSAGE_CODE % {'key': key, 'msg': context[m]})
        return code

instead of

def _get_configuration(self, context):
        code = []
        for dict_ in context:
            for var, val in dict_.items():
                if var.startswith('olark_'):
                    key = var[6:]
                    if key in MESSAGE_KEYS:
                        code.append(MESSAGE_CODE % {'key': key, 'msg': val})
        return code

Any reason why not to use the regex method (performance???)

ScottEAdams avatar Aug 13 '14 10:08 ScottEAdams

Hi Scott, not sure if you are still interested in this issue, but I was looking at this and I see a problem with your solution: what happens if the value of a context variable starts with olark_?

Apart from that, I think searching a string-representation, although clever, is a bit too ... clever. I do want to fix the problem, though, so can you tell me a bit more about the problem? For example, what is the stack trace of the error?

jcassee avatar Apr 19 '15 09:04 jcassee

@7wonders Scott, is this still an issue?

Ideally, you would first write some test code that fails with the current implementation. Then we try your approach, and - if it also fails - we adapt it until we have this issue fixed forever.

bittner avatar Sep 24 '15 17:09 bittner

@7wonders Reading your error description it looks obvious that your RequestContext is not populated correctly. If we can make sure this problem is generally valid, and not just specific to your installation, we can try to come up with a solution. Ideally, starting with a failing test case, as mentioned above.

Here is the code I can reproduce the error message with: (= the title of this issue)

from django.template.context import RequestContext
r = RequestContext(None)  # parameters: (request, dict_=None, processors=None)
r.items()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'RequestContext' object has no attribute 'items'

This would happen when such an unfilled context object hits line 98 in our olark template tag. See the Django docs for parameter details of RequestContext. Version numbers of your Python and Django installation would also be helpful.

Do you have some sample code to share to reproduce and further analyse the problem? We'll close this issue otherwise for lack of activity.

bittner avatar May 26 '16 16:05 bittner