gpt4 book ai didi

python - 为什么这个 Django View 在显示调试打印语句之前会出错?

转载 作者:行者123 更新时间:2023-12-01 05:10:45 26 4
gpt4 key购买 nike

对于 Django 项目中的开发服务器,我有一个 Django View ,其中生成了 500,显然是在 View 可以运行初始调试输出之前。根据预期的 Ajax 命中,开发服务器的输出为:

Internal Server Error: /ajax/say
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/jonathan/unixytalk/unixytalk/views.py", line 71, in ajax_say
request.session, room = room)):
File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 163, in filter
return self.get_queryset().filter(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 590, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 608, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1198, in add_q
clause = self._add_q(where_part, used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1234, in _add_q
current_negated=current_negated)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1125, in build_filter
clause.add(constraint, AND)
File "/Library/Python/2.7/site-packages/django/utils/tree.py", line 104, in add
data = self._prepare_data(data)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py", line 79, in _prepare_data
value = obj.prepare(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py", line 352, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 1079, in get_prep_lookup
return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 369, in get_prep_lookup
return self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 1073, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'SessionStore'
[18/Jun/2014 11:34:28] "POST /ajax/say HTTP/1.1" 500 111136

urlpatterns 有:

url(r'^ajax/say/?$', 'unixytalk.views.ajax_say'),

函数本身的来源是:

#@ajax_login_required
@csrf_exempt
def ajax_say(request):
print 'Reached here!'
if not request.session.get('identifier', None):
session = Session()
session.save()
request.session['identifier'] = session.id
#print repr(request)
#conversation = request.POST['conversation']
#if not request.user in conversation.users:
#return
try:
print repr(get_post(request))
text = get_post(request)['params']['text']
print 'Door 1'
print repr(text)
except KeyError:
print 'Door 2'
return HttpResponse(json.dumps([]), mimetype='application/json')
except TypeError:
print 'Door 3'
return HttpResponse(json.dumps([]), mimetype='application/json')
if get_post(request)['params'].get('time', None) != None:
room = Room.objects.filter(identifier =
get_post(request)['params']['room'])[0]
if len(IndividualContribution.objects.filter(session =
request.session, room = room)):
instance = IndividualContribution.objects.filter(session =
request.session, room = room)[0]
else:
instance = IndividualContribution(room = room, session =
request.session['identifier'])
instance.text = text
instance.timestamp = get_post(request)['params']['timestamp']
instance.save()
return HttpResponse(json.dumps([]), mimetype='application/json')

此功能仍在开发中,旨在进一步完善。

在显示上面的错误输出之前,开发服务器的控制台输出显示一到两次没有服务器错误的命中。 (引用/ajax/say 的页面应该每秒给出一次 Brain Dump,发生的情况是没有错误的初始位输出,然后每秒 500 次。)非错误输出是:

Inner Sanctum ~/unixytalk $ python manage.py runserver
Validating models...

0 errors found
June 18, 2014 - 11:34:26
Django version 1.6.2, using settings 'unixytalk.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Reached here!
{u'params': {u'room': u'ee8696bab4df6a68e0f3', u'time': 1403109268206}}
Door 2
Reached here!
{"monologues": {}, "sessions": []}
{"monologues": {}, "sessions": []}
/Library/Python/2.7/site-packages/django/http/response.py:330: DeprecationWarning: Using mimetype keyword argument is deprecated, use content_type instead
super(HttpResponse, self).__init__(*args, **kwargs)

[18/Jun/2014 11:34:28] "POST /ajax/listen HTTP/1.1" 200 34
[18/Jun/2014 11:34:28] "POST /ajax/listen HTTP/1.1" 200 34
{u'params': {u'text': u'aaaa', u'room': u'ee8696bab4df6a68e0f3', u'time': 1403109268212}}
Door 1
u'aaaa'
[18/Jun/2014 11:34:28] "POST /ajax/say HTTP/1.1" 200 2

我猜我正在尝试使用字符串或非整数的 ID 或类似的东西创建一个 session 对象,即第一次运行会产生影响第二次和后续运行的污染。 “Session”不是 Django 的 session ,而是一个空的(Django 模型)类,用于创建唯一的整数键。

--更新--

响应 dhana,为该项目定义的模型是:

from django.contrib.auth.models import User
from django.db import models

from unixytalk import settings

class Room(models.Model):
identifier = models.CharField(max_length =
settings.MAX_HASH_LENGTH, null = True, blank = True)

class Session(models.Model):
pass

class IndividualContribution(models.Model):
room = models.ForeignKey(Room)
session = models.IntegerField()
text = models.TextField()
timestamp = models.FloatField()
username = models.TextField()

最佳答案

我认为你的错误在这里

instance = IndividualContribution.objects.filter(session =
request.session, room = room)[0]

当你查询数据时,它给出了这个错误

TypeError: int() argument must be a string or a number, not 'SessionStore'

它询问字符串或数字,但您提供 SessionStore。

如果您提供 IndividualContribution 模型详细信息,我将提供更多详细信息。

关于python - 为什么这个 Django View 在显示调试打印语句之前会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24290993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com