开发者

Is "message" a reserved word in Django or Python?

开发者 https://www.devze.com 2023-01-28 00:42 出处:网络
I seem to get a TypeError (\'message\' is an invalid keyword argument for this function) every time I try adding something in the DB via the Django admin interface. The object is added but th开发者_运

I seem to get a TypeError ('message' is an invalid keyword argument for this function) every time I try adding something in the DB via the Django admin interface. The object is added but th开发者_运维问答is exception is raised. Could be this linked to the fact that I have a model named "Message"?


No. Python's reserved words do not include message and the TypeError you've described doesn't suggest a namespace collision. Look at the function's keyword arguments and make sure that message is among them. It isn't though, so maybe you meant to type msg.


When trying to assign a value to reserved keywords, SyntaxError is raised.


That means the function you are calling does not accept an argument named "message".

I guess that's because the model your are using doesn't have a field named "message"


This example shows what others have already pointed out.

>>> def hello(msg):
...     print "Hello, ", msg    
... 
>>> hello("world")
Hello,  world
>>> hello(msg="world")
Hello,  world
>>> hello(message="world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: hello() got an unexpected keyword argument 'message'


Seems I was missing the directives for the Django messages framework in my settings.py. Thanks anyway for the answers.


I believe message | messages is a reserved word! The following is my code snippet:

messages = page.object_list
variables = RequestContext(request, {
    'messages': messages,
    'show_paginator': paginator.num_pages > 1,
    'has_prev': page.has_previous(),
    'has_next': page.has_next(),
    'page': page_number,
    'pages': paginator.num_pages,
    'next_page': page_number + 1,
    'prev_page': page_number - 1 
})
return render_to_response('user_page.html', variables)

The problem occurs when I check for the messages list in user_page.html. For whatever reason, the check, {% if messages %}, will always return false, even if the list is populated.The minute I changed variable names it worked as intended. I haven't found any documentation backing this up, all I know is that I banged my head for 1.5 days till randomly figuring it out.

0

精彩评论

暂无评论...
验证码 换一张
取 消