0

I'm creating private messages application, and I render my messages this way:

return render(request,'mssg/mssg_page.html', {'inbox':inbox, 'current_mssg':current_mssg,})

Inbox contain all messages. Current message is a one that should be fully displayed. I made a list of messages that look like this in my template:

{% for mssg in inbox %}
    <a href="#"><!-- here -->
       <div class="right">
          <h3>{{ mssg.mssg_from.username }} <small>{{ mssg.delivery_date }}</small></h3>
          <h2>{{ mssg.title|safe }}</h2>
          <p>{{ mssg.text|safe|truncatewords:15  }}</p>
       </div>
    </a>
{% endfor %}

I display full current message in same template too.
I wants to change current message after clicking on link with <!-- here --> commment
I know that I can send message id to view, validate it and send it back to template. But it needs page to be refreshed. However all messages are already in template listed in inbox so there should be no need to refresh page. I also know that it's possible to pick an message from inbox like this {{inbox.0}}
But I have no idea how to make it depended on clicked link. I guess that javascript is needed for it. But I even if I know that I can do something like inbox[0] by placing inbox.0 in template I still don't know how to do something like inbox[varriable].
Is there a way to do it?
I don't want my page to refresh if it don't have to do it.
PS I'm using bootsrap

zekorius
  • 5
  • 4
  • I think you should use an Ajax call to request your view with message so that you can validate that this is requesting for . Ajax call will get you data without refreshing page and you can show the result on the same page. – Piyush S. Wanare Jul 29 '16 at 13:12
  • Possible duplicate of [How do I integrate Ajax with Django applications?](http://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications) – YPCrumble Jul 29 '16 at 15:55

1 Answers1

1

Using AJAX will solve your problem. See this StackOverflow answer which explains how to do that:

$('a').click(function() { // This is a listener to the link you mentioned.
    $.ajax({
        url: '127.0.0.1:8000/your_ajax_url', // You will need to create a URL for your AJAX call.
        type: 'get', // This is the default though, you don't actually need to always mention it
        success: function(data) {
            alert(data);
        },
        failure: function(data) { 
            alert('Got an error dude');
        }
    }); 
}
Community
  • 1
  • 1
YPCrumble
  • 26,610
  • 23
  • 107
  • 172