Advertisement
shinhosuck1973

Untitled

Apr 2nd, 2024
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.11 KB | None | 0 0
  1. {% extends 'index.html' %}
  2. {% load static %}
  3.  
  4. <title>
  5.     {% block title %}
  6.         Post List
  7.     {% endblock title %}
  8. </title>
  9.  
  10.  
  11. {% block content %}
  12.     <div class="row">
  13.         <div class="container">
  14.             <form id="post-form-id" class="post-form" action="{% url 'posts:post-list' %}" method="POST" enctype="multipart/form-data">
  15.                 {% csrf_token%}
  16.                 {{ form }}
  17.                 <div class="form-btns">
  18.                     <button type="submit">Submit</button>
  19.                 </div>
  20.             </form>
  21.             <div id="post-container">
  22.                 <!-- posts go here -->
  23.             </div>
  24.         </div>
  25.     </div>
  26.  
  27.     <script>
  28.         const postContainer = document.querySelector('#post-container')
  29.         postContainer.innerHTML = `<h4>Loading...</h4>`
  30.         const postForm = document.querySelector('#post-form-id')
  31.  
  32.         const handlePostFormSubmit = (e)=> {
  33.             e.preventDefault()
  34.             // const {content} = Object.fromEntries(new FormData(e.target))
  35.             const form = e.target
  36.             const formData = new FormData(form)
  37.             const url = form.getAttribute('action')
  38.             const method = form.getAttribute('method')
  39.             const xhr = new XMLHttpRequest()
  40.  
  41.             xhr.open(method, url)
  42.             xhr.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest')
  43.             xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
  44.             xhr.onload = ()=> {
  45.                 console.log(JSON.parse(xhr.response))
  46.             }
  47.             xhr.send(formData)
  48.             e.target.reset()
  49.         }
  50.  
  51.         postForm.addEventListener('submit', handlePostFormSubmit)
  52.  
  53.  
  54.         const createInnerHtmlElement = (post)=> {
  55.             const element = `
  56.                 <div class="post-content">
  57.                     <a class="content" href=${post.url}>${post.content}</a>
  58.                     <p>${post.user} ${post.total_posts} ${parseInt(post.total_posts) > 1 ? 'Posts' : "post"}</p>
  59.                     <button
  60.                        class="post-like-btn"
  61.                        data_id=${post.id}
  62.                        onclick=handleLike("${post.id}","${post.likes}")
  63.                        type="button"
  64.                    >
  65.                         ${post.likes} ${parseInt(post.likes) > 1 ? "Likes" : "Like"}
  66.                     </button>
  67.                 </div>
  68.             `
  69.             return element
  70.         }
  71.  
  72.         const loadPosts = ()=> {
  73.             const xhr = new XMLHttpRequest()
  74.             const method = 'GET'
  75.             const url = '/posts?str=js-request'
  76.             const responseType= 'json'
  77.  
  78.             xhr.responseType = responseType
  79.             xhr.open(method, url)
  80.             xhr.onload = ()=> {
  81.                 const response = xhr.response
  82.                 const elementArray = response.json_data.map((post)=> {
  83.                     return createInnerHtmlElement(post)
  84.                 }).join('')
  85.                 postContainer.innerHTML = elementArray
  86.             }
  87.             xhr.send()
  88.         }
  89.         loadPosts()
  90.     </script>
  91. {% endblock content %}
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement