спасибо за помощь
post.js:
document.addEventListener('DOMContentLoaded', function() {
// Use buttons to toggle between views
const index = document.querySelector('#index')
const following = document.querySelector('#following')
if(index) {
index.addEventListener('click', () => load_data('index'));
}
if (following) {
following.addEventListener('click', () => load_data("following"));
}
// By default, load the index
load_data('index');
});
function load_data(type) {
console.log("type:", type);
let url;
if (type == "index") {
url = "/post"
} else if (type == "following") {
url = "/following"
} else {
return
}
console.log("url: ", url)
fetch(url)
.then(response => response.json())
.then(posts => {
console.log("posts:", posts);
const postsContainer = document.querySelector("#postsContainer");
if(postsContainer) {
postsContainer.innerHTML = '';
}
posts.forEach(post => {
.....
}
layout.html:
[*]
All Posts
{% if user.is_authenticated %}
[*]
New Post
[*]
Following
views.py:
@login_required
def following(request):
user = request.user
user_followings = Follow.objects.filter(follower=user)
posts = []
for user_following in user_followings:
posts.extend(Post.objects.filter(user=user_following.followed).order_by('-date'))
return JsonResponse([post.serialize() for post in posts], safe=False)
Подробнее здесь: https://stackoverflow.com/questions/793 ... e-the-inde
Мобильная версия