I'm keep getting this error Invalid literal for int() with base 10 from the views in my django project after it worked for a while ,
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'
Here is My views.py looks like
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 {}
And here all the message 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()
please help me I'm beginner in django
CodePudding user response:
The first problem is that you should use int(float(uid2))
if uid2 is a string representing a float
.
The second problem is that you uid2 represents a float using ,
instead of .
(I don't know if , is used for decimal points or as a thounsands separator) and you need to add a replace()
.
int(float(uid2.replace(",", "."))) if comma is used for decimal points
int(float(uid2.replace(",", ""))) if comma is used as a thounsands separator