Data doesn’t show on template – django

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>No chat history available.
</code>
<code>No chat history available. </code>
No chat history available.

How can I fix this?

We have models on models.py:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật