I have a chat app but it seems that i have a few mistakes which i don’t know what is.
Users can not see their chat history and their messages in the app.
When Users write their messages on a specific form, I can see their messages on Django admin and that means the messages send successfully.
But the problem starts in views.py, I tried a lot to address the template on chat history feed but when you run the app there is no response except:
<code>No chat history available.
<code>No chat history available.
</code>
No chat history available.
How can I fix this?
We have models on models.py:
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from authentication.models import BasicUserProfile
class Chat(models.Model):
creation_date = models.DateField(default=timezone.now)
id = models.AutoField(primary_key=True)
content = models.TextField()
sender = models.ForeignKey(
on_delete=models.CASCADE,
reciever = models.ForeignKey(
on_delete=models.CASCADE,
return "chat: " + str(self.sender)
<code># Main Imports
# Django Imports
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
# My Module Imports
from authentication.models import BasicUserProfile
# Chat
# ---------
class Chat(models.Model):
creation_date = models.DateField(default=timezone.now)
id = models.AutoField(primary_key=True)
content = models.TextField()
sender = models.ForeignKey(
BasicUserProfile,
on_delete=models.CASCADE,
blank=True,
null=True,
related_name="sender"
)
reciever = models.ForeignKey(
BasicUserProfile,
on_delete=models.CASCADE,
blank=True,
null=True,
related_name="reciever"
)
def __str__(self):
return "chat: " + str(self.sender)
</code>
# Main Imports
# Django Imports
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
# My Module Imports
from authentication.models import BasicUserProfile
# Chat
# ---------
class Chat(models.Model):
creation_date = models.DateField(default=timezone.now)
id = models.AutoField(primary_key=True)
content = models.TextField()
sender = models.ForeignKey(
BasicUserProfile,
on_delete=models.CASCADE,
blank=True,
null=True,
related_name="sender"
)
reciever = models.ForeignKey(
BasicUserProfile,
on_delete=models.CASCADE,
blank=True,
null=True,
related_name="reciever"
)
def __str__(self):
return "chat: " + str(self.sender)
`
and views.py:
<code> `def chat_single(request, username):
""" in this page users can chat with each other """
# Deleting any sessions regarding top-tier type of users
current_basic_user = get_current_user(request, User, ObjectDoesNotExist)
current_basic_user_profile = get_current_user_profile(
topics_to_follow = get_topics_to_follow(Topic, ObjectDoesNotExist, random)
# Who to follow box cells
who_to_follow = get_who_to_follow(
# Get the current followings
current_followings = Follower.objects.filter(
follower=current_basic_user_profile
except ObjectDoesNotExist:
current_followings = None
# Get the current text reciever (user)
current_reciever_user = User.objects.get(username=username)
except ObjectDoesNotExist:
current_reciever_user = None
current_reciever = BasicUserProfile.objects.get(
user=current_reciever_user
except ObjectDoesNotExist:
if current_reciever_user == None:
return HttpResponseRedirect("/")
# Getting the chat history feed
chat_records_self = Chat.objects.filter(
sender=current_basic_user_profile,
reciever=current_reciever
chat_records_reciever = Chat.objects.filter(
reciever=current_basic_user_profile
except ObjectDoesNotExist:
chat_records_reciever= None
# Chat text from processing
if request.method == "POST":
if request.POST.get("chat_send_submit_btn"):
chat_content = request.POST.get("chat_content")
sender=current_basic_user_profile,
reciever=current_reciever,
chat_history_feed.append(chat_content) # Adding the message to chat history feed
return HttpResponseRedirect("/chat/"+current_reciever_user.username+"/")
"current_basic_user": current_basic_user,
"current_basic_user_profile": current_basic_user_profile,
"who_to_follow": who_to_follow,
"topics_to_follow": topics_to_follow,
"current_followings": current_followings,
"current_reciever": current_reciever,
if current_basic_user == None:
return HttpResponseRedirect("/auth/login/")
return render(request, "chat/single.html", data)
"current_basic_user": current_basic_user,
"current_basic_user_profile": current_basic_user_profile,
"who_to_follow": who_to_follow,
"topics_to_follow": topics_to_follow,
"current_followings": current_followings,
"current_reciever": current_reciever,
if current_basic_user == None:
return HttpResponseRedirect("/auth/login/")
return render(request, "chat/single.html", data)`
<code> `def chat_single(request, username):
""" in this page users can chat with each other """
# admin user session pop
# admin user session pop
# Deleting any sessions regarding top-tier type of users
# Get the current users
current_basic_user = get_current_user(request, User, ObjectDoesNotExist)
current_basic_user_profile = get_current_user_profile(
request,
User,
BasicUserProfile,
ObjectDoesNotExist
)
# Topics to follow
topics_to_follow = get_topics_to_follow(Topic, ObjectDoesNotExist, random)
# Who to follow box cells
who_to_follow = get_who_to_follow(
BasicUserProfile
)
# Get the current followings
try:
current_followings = Follower.objects.filter(
follower=current_basic_user_profile
)
except ObjectDoesNotExist:
current_followings = None
# Get the current text reciever (user)
try:
current_reciever_user = User.objects.get(username=username)
except ObjectDoesNotExist:
current_reciever_user = None
try:
current_reciever = BasicUserProfile.objects.get(
user=current_reciever_user
)
except ObjectDoesNotExist:
current_reciever = None
if current_reciever_user == None:
return HttpResponseRedirect("/")
# Getting the chat history feed
chat_history_feed = []
try:
chat_records_self = Chat.objects.filter(
sender=current_basic_user_profile,
reciever=current_reciever
)
chat_records_reciever = Chat.objects.filter(
sender=current_reciever,
reciever=current_basic_user_profile
)
except ObjectDoesNotExist:
chat_records_self = None
chat_records_reciever= None
print(chat_history_feed)
# Chat text from processing
if request.method == "POST":
if request.POST.get("chat_send_submit_btn"):
chat_content = request.POST.get("chat_content")
new_message = Chat(
content=chat_content,
sender=current_basic_user_profile,
reciever=current_reciever,
)
new_message.save()
chat_history_feed.append(chat_content) # Adding the message to chat history feed
return HttpResponseRedirect("/chat/"+current_reciever_user.username+"/")
data = {
"current_basic_user": current_basic_user,
"current_basic_user_profile": current_basic_user_profile,
"who_to_follow": who_to_follow,
"topics_to_follow": topics_to_follow,
"current_followings": current_followings,
"current_reciever": current_reciever,
}
if current_basic_user == None:
return HttpResponseRedirect("/auth/login/")
else:
return render(request, "chat/single.html", data)
data = {
"current_basic_user": current_basic_user,
"current_basic_user_profile": current_basic_user_profile,
"who_to_follow": who_to_follow,
"topics_to_follow": topics_to_follow,
"current_followings": current_followings,
"current_reciever": current_reciever,
}
if current_basic_user == None:
return HttpResponseRedirect("/auth/login/")
else:
return render(request, "chat/single.html", data)`
</code>
`def chat_single(request, username):
""" in this page users can chat with each other """
# admin user session pop
# admin user session pop
# Deleting any sessions regarding top-tier type of users
# Get the current users
current_basic_user = get_current_user(request, User, ObjectDoesNotExist)
current_basic_user_profile = get_current_user_profile(
request,
User,
BasicUserProfile,
ObjectDoesNotExist
)
# Topics to follow
topics_to_follow = get_topics_to_follow(Topic, ObjectDoesNotExist, random)
# Who to follow box cells
who_to_follow = get_who_to_follow(
BasicUserProfile
)
# Get the current followings
try:
current_followings = Follower.objects.filter(
follower=current_basic_user_profile
)
except ObjectDoesNotExist:
current_followings = None
# Get the current text reciever (user)
try:
current_reciever_user = User.objects.get(username=username)
except ObjectDoesNotExist:
current_reciever_user = None
try:
current_reciever = BasicUserProfile.objects.get(
user=current_reciever_user
)
except ObjectDoesNotExist:
current_reciever = None
if current_reciever_user == None:
return HttpResponseRedirect("/")
# Getting the chat history feed
chat_history_feed = []
try:
chat_records_self = Chat.objects.filter(
sender=current_basic_user_profile,
reciever=current_reciever
)
chat_records_reciever = Chat.objects.filter(
sender=current_reciever,
reciever=current_basic_user_profile
)
except ObjectDoesNotExist:
chat_records_self = None
chat_records_reciever= None
print(chat_history_feed)
# Chat text from processing
if request.method == "POST":
if request.POST.get("chat_send_submit_btn"):
chat_content = request.POST.get("chat_content")
new_message = Chat(
content=chat_content,
sender=current_basic_user_profile,
reciever=current_reciever,
)
new_message.save()
chat_history_feed.append(chat_content) # Adding the message to chat history feed
return HttpResponseRedirect("/chat/"+current_reciever_user.username+"/")
data = {
"current_basic_user": current_basic_user,
"current_basic_user_profile": current_basic_user_profile,
"who_to_follow": who_to_follow,
"topics_to_follow": topics_to_follow,
"current_followings": current_followings,
"current_reciever": current_reciever,
}
if current_basic_user == None:
return HttpResponseRedirect("/auth/login/")
else:
return render(request, "chat/single.html", data)
data = {
"current_basic_user": current_basic_user,
"current_basic_user_profile": current_basic_user_profile,
"who_to_follow": who_to_follow,
"topics_to_follow": topics_to_follow,
"current_followings": current_followings,
"current_reciever": current_reciever,
}
if current_basic_user == None:
return HttpResponseRedirect("/auth/login/")
else:
return render(request, "chat/single.html", data)`
template.html:
<code>`<div id="chat-right-middle-box">
{% for chat_record in chats %}
<strong>{{ chat_record.sender }}</strong>: {{ chat_record.content }}
<li>No chat history available.</li>
<div id="clear-float"></div>
<div id="chat-other-box">
{% for chat_record in chat_single %}
<strong>{{ chat_record.content }}</strong>: {{ chat_record }}
<li>No chat history available.</li>
<code>`<div id="chat-right-middle-box">
<div id="chat-self-box">
<ul>
{% for chat_record in chats %}
<li>
<strong>{{ chat_record.sender }}</strong>: {{ chat_record.content }}
</li>
{% empty %}
<li>No chat history available.</li>
{% endfor %}
</ul>
</div>
<div id="clear-float"></div>
<div id="chat-other-box">
<ul>
{% for chat_record in chat_single %}
<li>
<strong>{{ chat_record.content }}</strong>: {{ chat_record }}
</li>
{% empty %}
<li>No chat history available.</li>
{% endfor %}
</ul>
</div>`
</code>
`<div id="chat-right-middle-box">
<div id="chat-self-box">
<ul>
{% for chat_record in chats %}
<li>
<strong>{{ chat_record.sender }}</strong>: {{ chat_record.content }}
</li>
{% empty %}
<li>No chat history available.</li>
{% endfor %}
</ul>
</div>
<div id="clear-float"></div>
<div id="chat-other-box">
<ul>
{% for chat_record in chat_single %}
<li>
<strong>{{ chat_record.content }}</strong>: {{ chat_record }}
</li>
{% empty %}
<li>No chat history available.</li>
{% endfor %}
</ul>
</div>`
the problem starts from here, where users should be able to see their messages on chat_history_feed but when we open the app we just have that response: No chat history available.
Warn: When Users type something on the specific form and submit, I can see their messages on django-admin
Thank you!
I tried to address the chat_history_feed on template, so Users can see history of their messages.
but i got the response that i’ve told before.