0
votes

I'm making a messaging system which has a message model and some views for things like inbox and conversation. Inbox view and template works and is linked to the conversation through the id of the sender. However, when I click on the senders name (The senders name is the link (template link is <a href="{% url 'conversation' sender.id %}">{{ sender }}</a>) that is meant to trigger the conversation view which is supposed to render the template convo.html ( the project level url for that is url(r'^messaging/inbox/conversation/(?P<id>[\w-]+)/$', message_views.Conversation.as_view(), name="conversation"),) ), Django changes the url but stays on the same page(So it triggers the inbox view and stays on inbox.html and changes the url but doesn't trigger the conversation view and doesn't render the convo.html template). I've tried changing from a function based view to a class based view and it did nothing. What am I doing wrong?

Note: I'm still new to django so forgive me for any bad practices; I'm attempting to teach myself.

views.py (inbox and conversation views)

def inbox(request):
    messages = Message.objects.filter(receiver=request.user.id)
    senders = {}
    for message in messages:
        if message.sender not in senders:
            senders[message.sender] = message

    return render(request, 'inbox.html', {'senders': senders})

# There is only ever one of this view, either the CBV or FBV
def conversation(request, other_user_id):
    print(request.method)
    if request.method == "POST":
        reply = request.post['reply']
        other_user = CustomUser.objects.get(id=other_user_id)
        message = Message(sender=request.user, receiver=other_user)
        message.save()
        return redirect("conversation")

    else:
        other_user = CustomUser.objects.get(id=other_user_id)
        messages = Message.objects.filter(Q(receiver=request.user) & Q(sender=other_user))

        print(messages)
    return render(request, 'convo.html', {'messages': messages})

# Class based conversation view
class Conversation(View):
    def post(request, other_user_id):
        reply = request.post['reply']
        other_user = CustomUser.objects.get(id=other_user_id)
        message = Message(sender=request.user, receiver=other_user)
        message.save()
        return redirect("conversation")
    def get(request, other_user_id):
        other_user = CustomUser.objects.get(id=other_user_id)
        messages = Message.objects.filter(Q(receiver=request.user) & Q(sender=other_user))
        print(messages)
        return render(request, 'convo.html', {'messages': messages})

template - inbox.html

<!DOCTYPE html>
{% extends "accbase.html" %}
<!--Omitted outbox in favor of conversation-->
{% block content %}
<div class="inbox">
    <h1>Inbox</h1>
    <hr>
        {% for sender in senders %}
         <a href="{% url 'conversation' sender.id %}">{{ sender }}</a>
         <br>
         {{sender.message.created_at}}
        <br>
        {% endfor %}
</div>
{% endblock %}

template - convo.html

<!DOCTYPE html>
<!--Template for reciever side of conversation-->
{% extends "accbase.html"%}
{% block content %}
<div class="conversation">
<h1>Conversation with {{sender}}</h1>
        <p>
        {% for msg in messages %}
            <!--Order is descending ~~ bottom to top: newest to oldest-->
            {{ msg.content }}
            <br>       
        {% endfor %}
            <hr>
        <form method="POST">
            <label>Reply</label>
            <input type="text" name="reply">
        </form>
        </p>
        <button type="submit">Send</button>
</div>
{% endblock %}

project level urls.py

from django.urls import path, re_path, include
from django.conf.urls import url
from messaging import views as message_views
urlpatterns = [
    url(r'^messaging/inbox', message_views.inbox, name="inbox"),
    url(r'^messaging/inbox/conversation/(?P<id>[\w-]+)/$', message_views.Conversation.as_view(), name="conversation"),
] 
1
You need to explain things much more specifically. "When I click on the senders name" what is that? Which link (url) is that exactly? Which url pattern? Which view are you expecting to trigger? Which template are you expecting to be rendered? Please use the actual view names, file names, template names that you show in your code so we don't have to read all the code (it seems a lot and not a minimal example) and guess. Talk us through it. - dirkgroten
I edited it, let me know if it's still not enough information. I included the inbox view and template since I'm redirecting from that, same with the inbox url pattern. I also included both the conversation Class Based View and Function Based View to show exactly what I did just in case I did something wrong - Trippy-Chords
I don't understand your url patterns. You have two patterns named inbox and two patterns named conversation. In the app urls, they also both have the same url. This is wrong. - dirkgroten
originally I didn't even have those app level urls and it didn't make a difference with or without them. I'm teaching myself and still need to learn more about the urls, forgive me. - Trippy-Chords
well, it looks like the app urls aren't used since you don't include them anywhere in your project urls. So probably not causing an issue here. What do you see in your console? Do you see a GET request for /messaging/inbox/conversation/24/? And in your conversation view, you have print command, do your see that printing "GET"? - dirkgroten

1 Answers

0
votes

As Daniel Roseman said in comments " The problem here is that you didn't terminate the regex for your inbox URL, so it also matches the conversation URL. " so I added the needed $ to the end of the inbox url pattern and that fixed it!