1

I have a chat. One view is responsible for displaying the list of chats and chat content:

def chats_view(request, chat_uuid=None):
    if chat_uuid:
            context = {
                # Variables for displaying both the list and chat content
                }
            return render(request, 'chats/chats.html', context)
    else:
        context = {
            # Variables for displaying only the list of chats
            }
        return render(request, 'chats/chats.html', context)

On the "/chats/" URL, only the list of chats should be displayed, and on the "/chats/<chat_uuid>/" URL, both the list of chats and the content of the selected chat should be displayed. When clicking on any chat without reloading the page, a window with the chat content should appear next to it, and the URL should change from "/chats/" to "/chats/<chat_uuid of the selected chat>".

I tried to do this using AJAX, but in the HTML content there are Django template tags and they are rendered as text.

Leonid
  • 121
  • 3
  • Can you provide the relevant parts of the template and ajax code? Also, as an aside, you may want to look into HTMX as a way of handling this with a lot of the legwork done for you. – SamSparx Jun 20 '23 at 18:30
  • i already removed the ajax code because it doesn't work :/ but it was something like that - $.ajax(...) – Leonid Jun 20 '23 at 19:13
  • If you need to use the curly braces in your script without them being converted to text try using https://docs.djangoproject.com/en/dev/ref/templates/builtins/#verbatim – raphael Jun 20 '23 at 19:16
  • I need to display django template tags like {% if %} {% endif %}; {% for %} {% endfor %} and so on. In JavaScript – Leonid Jun 20 '23 at 19:17
  • 1
    If your JavaScript is in a separate file, then you can't do this since Django does not render that file. It only generates the HTML template. What you CAN do is put your JavaScript within ` – raphael Jun 21 '23 at 01:22
  • If you want to do this in javascript, check these two SO questions https://stackoverflow.com/questions/824349/how-do-i-modify-the-url-without-reloading-the-page and https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page – Mr. Kenneth Jun 21 '23 at 05:56
  • Have not tried to do this in plain js but I think you dont need to run ajax on this. Try to explore the history api – Mr. Kenneth Jun 21 '23 at 05:58
  • 1
    raphael, Thank you! This is the solution to my problem! – Leonid Jun 21 '23 at 06:31
  • I sent a JsonResponse from the view, but I had to use render() – Leonid Jun 21 '23 at 06:32

0 Answers0