Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23390

Data doesn't show on template - django

$
0
0

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:

No chat history available.

How can I fix this?

We have models on models.py:

# Main Imports# Django Importsfrom django.db import modelsfrom django.contrib.auth.models import Userfrom django.utils import timezone# My Module Importsfrom 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:

 \`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 userscurrent_basic_user = get_current_user(request, User, ObjectDoesNotExist)current_basic_user_profile = get_current_user_profile(    request,    User,    BasicUserProfile,    ObjectDoesNotExist)# Topics to followtopics_to_follow = get_topics_to_follow(Topic, ObjectDoesNotExist, random)# Who to follow box cellswho_to_follow = get_who_to_follow(    BasicUserProfile)# Get the current followingstry:    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 = Nonetry:    current_reciever = BasicUserProfile.objects.get(        user=current_reciever_user    )except ObjectDoesNotExist:    current_reciever = Noneif current_reciever_user == None:    return HttpResponseRedirect("/")# Getting the chat history feedchat_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= Noneprint(chat_history_feed)# Chat text from processingif 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:

`<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.


Viewing all articles
Browse latest Browse all 23390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>