工作一段时间后,在我看来,以 10 为底的 int() 文字无效
Invalid literal for int() with base 10 in my views after it worked a while
我不断收到此错误 Invalid literal for int() with base 10
从我的 django 项目运行一段时间后的观点来看,
Traceback (most recent call last):
File "/srv/cc/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/srv/cc/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/cc/env/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "./core/views.py", line 133, in send_message
if int(uid2) == uid:
Exception Type: ValueError at /msg/new
Exception Value: invalid literal for int() with base 10: '4,016'
这是我的 views.py 样子
def chat_detail(request, pk):
chat = Chat.objects.get(pk=pk)
added, lm = chat.new(request.user)
if lm:
lm.msg = chat.current_last_msg
lm.save()
recepient = chat.recepient(request.user)
return render(request, 'core/chat.html', {'chat': chat, 'recepient': recepient})
def chat_with_user(request, uid):
chat = Chat.get_or_create(request.user.id, int(uid))
return redirect(f'/chat/{chat.id}')
def chats(request):
user_chats = []
for c in Chat.of_user(request.user):
user_chats.append({'rec': c.recepient(request.user),
'unread': len(c.new(request.user)[0]),
'chat': c})
return render(request, 'core/chats.html', {'user_chats': user_chats})
def send_message(request):
if request.method == 'POST':
m = Message()
uid = request.user.id
uid2 = request.POST.get('uid2', None)
if uid2 is None:
return {}
if int(uid2) == uid:
return {}
m.chat = Chat.get_or_create(uid, int(uid2))
m.sender = request.user
m.text = request.POST.get('text')
m.save()
return redirect(f'/chat/{m.chat.id}')
else:
return {}
所有消息都在这里models.py
class LastMessage(models.Model):
chat = models.ForeignKey('Chat', on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
msg = models.ForeignKey(Message, on_delete=models.CASCADE)
class Chat(models.Model):
users = models.ManyToManyField(User, blank=True)
@property
def msgs(self):
return self.message_set.all()
def new(self, user):
lm = self.last_msg_for(user)
if lm:
return self.added(lm.msg), lm
else:
return [], None
def added(self, start_msg):
return self.message_set.filter(id__gt=start_msg.id).all()
@property
def current_last_msg(self):
try:
return list(self.msgs)[-1]
except:
return None
def last_msg_for(self, user):
lm = self.current_last_msg
if lm:
lm_user = LastMessage.objects.filter(chat=self, user=user).first()
if lm_user is None:
lm_user = LastMessage.objects.create(chat=self, user=user, msg=lm)
return lm_user
return None
def recepient(self, cur_user):
for u in self.users.all():
if u.id != cur_user.id:
return u
return None
@classmethod
def get_or_create(cls, uid, uid2):
chat = cls.objects.filter(users=uid).filter(users=uid2).first()
if chat is None:
chat = cls()
chat.save()
chat.users.set([uid, uid2])
chat.save()
return chat
@classmethod
def of_user(cls, user):
return cls.objects.filter(users=user).all()
请帮助我,我是 django 的初学者
第一个问题是,如果 uid2 是表示 float
的字符串,您应该使用 int(float(uid2))
。
第二个问题是您 uid2 使用 ,
而不是 .
表示浮点数(我不知道 , 是否用于小数点或作为千位分隔符)并且您需要添加一个 replace()
.
int(float(uid2.replace(",", "."))) if comma is used for decimal points
int(float(uid2.replace(",", ""))) if comma is used as a thounsands separator
我不断收到此错误 Invalid literal for int() with base 10 从我的 django 项目运行一段时间后的观点来看,
Traceback (most recent call last):
File "/srv/cc/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/srv/cc/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/cc/env/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "./core/views.py", line 133, in send_message
if int(uid2) == uid:
Exception Type: ValueError at /msg/new
Exception Value: invalid literal for int() with base 10: '4,016'
这是我的 views.py 样子
def chat_detail(request, pk):
chat = Chat.objects.get(pk=pk)
added, lm = chat.new(request.user)
if lm:
lm.msg = chat.current_last_msg
lm.save()
recepient = chat.recepient(request.user)
return render(request, 'core/chat.html', {'chat': chat, 'recepient': recepient})
def chat_with_user(request, uid):
chat = Chat.get_or_create(request.user.id, int(uid))
return redirect(f'/chat/{chat.id}')
def chats(request):
user_chats = []
for c in Chat.of_user(request.user):
user_chats.append({'rec': c.recepient(request.user),
'unread': len(c.new(request.user)[0]),
'chat': c})
return render(request, 'core/chats.html', {'user_chats': user_chats})
def send_message(request):
if request.method == 'POST':
m = Message()
uid = request.user.id
uid2 = request.POST.get('uid2', None)
if uid2 is None:
return {}
if int(uid2) == uid:
return {}
m.chat = Chat.get_or_create(uid, int(uid2))
m.sender = request.user
m.text = request.POST.get('text')
m.save()
return redirect(f'/chat/{m.chat.id}')
else:
return {}
所有消息都在这里models.py
class LastMessage(models.Model):
chat = models.ForeignKey('Chat', on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
msg = models.ForeignKey(Message, on_delete=models.CASCADE)
class Chat(models.Model):
users = models.ManyToManyField(User, blank=True)
@property
def msgs(self):
return self.message_set.all()
def new(self, user):
lm = self.last_msg_for(user)
if lm:
return self.added(lm.msg), lm
else:
return [], None
def added(self, start_msg):
return self.message_set.filter(id__gt=start_msg.id).all()
@property
def current_last_msg(self):
try:
return list(self.msgs)[-1]
except:
return None
def last_msg_for(self, user):
lm = self.current_last_msg
if lm:
lm_user = LastMessage.objects.filter(chat=self, user=user).first()
if lm_user is None:
lm_user = LastMessage.objects.create(chat=self, user=user, msg=lm)
return lm_user
return None
def recepient(self, cur_user):
for u in self.users.all():
if u.id != cur_user.id:
return u
return None
@classmethod
def get_or_create(cls, uid, uid2):
chat = cls.objects.filter(users=uid).filter(users=uid2).first()
if chat is None:
chat = cls()
chat.save()
chat.users.set([uid, uid2])
chat.save()
return chat
@classmethod
def of_user(cls, user):
return cls.objects.filter(users=user).all()
请帮助我,我是 django 的初学者
第一个问题是,如果 uid2 是表示 float
的字符串,您应该使用 int(float(uid2))
。
第二个问题是您 uid2 使用 ,
而不是 .
表示浮点数(我不知道 , 是否用于小数点或作为千位分隔符)并且您需要添加一个 replace()
.
int(float(uid2.replace(",", "."))) if comma is used for decimal points
int(float(uid2.replace(",", ""))) if comma is used as a thounsands separator