发送消息,即时显示报错

来源:9-14 前端WebSocket API讲解

慕婉清0635027

2020-05-14

当聊天发送消息后对方必须刷新才能显示,对方再回复时就出现下面的错误,并且回复没反应,然后浏览器会出现下面的问题,希望老师能帮忙解决一下图片描述
/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/bin/python /Users/php8er/Desktop/zanhu/manage.py runserver 127.0.0.1:8000
Performing system checks…

Watching for file changes with StatReloader
System check identified no issues (0 silenced).
May 14, 2020 - 17:39:07
Django version 2.2, using settings 'config.settings.local’
Starting ASGI/Channels version 2.1.7 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /ws/notifications/ [127.0.0.1:52989]
WebSocket CONNECT /ws/notifications/ [127.0.0.1:52989]
WebSocket HANDSHAKING /ws/notifications/ [127.0.0.1:52993]
WebSocket CONNECT /ws/notifications/ [127.0.0.1:52993]
HTTP GET /messages/php9er/ 200 [1.65, 127.0.0.1:53011]
WebSocket DISCONNECT /ws/notifications/ [127.0.0.1:52993]
HTTP GET /static/js/notifications.js 404 [0.10, 127.0.0.1:53011]
HTTP GET /static/img/favicon.png 200 [0.03, 127.0.0.1:53011]
HTTP GET /messages/ 200 [0.74, 127.0.0.1:53069]
WebSocket DISCONNECT /ws/notifications/ [127.0.0.1:52989]
HTTP GET /static/js/notifications.js 404 [0.00, 127.0.0.1:53069]
HTTP POST /messages/send-message/ 200 [0.04, 127.0.0.1:53069]
HTTP GET /messages/php9er/ 200 [0.74, 127.0.0.1:53011]
HTTP GET /static/js/notifications.js 404 [0.04, 127.0.0.1:53011]
HTTP GET /static/img/favicon.png 200 [0.00, 127.0.0.1:53011]
Internal Server Error: /messages/send-message/
Traceback (most recent call last):
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/mysql/base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 209, in execute
res = self._query(query)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 315, in _query
db.query(q)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/MySQLdb/connections.py”, line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.IntegrityError: (1062, “Duplicate entry ‘dc6e6a5a8baa4f1998855122ba933828’ for key ‘PRIMARY’”)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/core/handlers/exception.py”, line 34, in inner
response = get_response(request)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/core/handlers/base.py”, line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/core/handlers/base.py”, line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py”, line 74, in inner
return func(*args, **kwds)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/contrib/auth/decorators.py”, line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File “/Users/php8er/Desktop/zanhu/zanhu/helpers.py”, line 15, in wrap
return f(request, *args, **kwargs)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/views/decorators/http.py”, line 40, in inner
return func(request, *args, **kwargs)
File “/Users/php8er/Desktop/zanhu/zanhu/messager/views.py”, line 68, in send_message
message=message
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/manager.py”, line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/query.py”, line 422, in create
obj.save(force_insert=True, using=self.db)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/base.py”, line 741, in save
force_update=force_update, update_fields=update_fields)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/base.py”, line 779, in save_base
force_update, using, update_fields,
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/base.py”, line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/base.py”, line 908, in _do_insert
using=using, raw=raw)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/manager.py”, line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/query.py”, line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/models/sql/compiler.py”, line 1332, in execute_sql
cursor.execute(sql, params)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/utils.py”, line 99, in execute
return super().execute(sql, params)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/utils.py”, line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/utils.py”, line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/utils.py”, line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/django/db/backends/mysql/base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 209, in execute
res = self._query(query)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/MySQLdb/cursors.py”, line 315, in _query
db.query(q)
File “/Users/php8er/.local/share/virtualenvs/zanhu-tgEnzapH/lib/python3.7/site-packages/MySQLdb/connections.py”, line 239, in query
_mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1062, “Duplicate entry ‘dc6e6a5a8baa4f1998855122ba933828’ for key ‘PRIMARY’”)
HTTP POST /messages/send-message/ 500 [0.06, 127.0.0.1:53011]

写回答

1回答

Jack

2020-05-14

已解决。Message模型类中是

uuid_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

不是

uuid_id = models.UUIDField(primary_key=True, default=uuid.uuid4(), editable=False)

所以就出现了每次生成的主键一样。报错  django.db.utils.IntegrityError: (1062, “Duplicate entry ‘dc6e6a5a8baa4f1998855122ba933828’ for key ‘PRIMARY’”)

0
0

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程