Advertisement
horozov86

Comments and Likes

Mar 21st, 2024
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. 1Create models comment и like;:
  2.  
  3.  
  4. from django.db import models
  5. from django.contrib.auth.models import User
  6. from my_holiday.destination.models import Place
  7.  
  8. class Comment(models.Model):
  9.     user = models.ForeignKey(User, on_delete=models.CASCADE)
  10.     place = models.ForeignKey(Place, on_delete=models.CASCADE)
  11.     text = models.TextField()
  12.     created_at = models.DateTimeField(auto_now_add=True)
  13.  
  14.     def __str__(self):
  15.         return f"{self.user.username} - {self.place.name} - {self.created_at}"
  16.  
  17.  
  18. class Like(models.Model):
  19.     user = models.ForeignKey(User, on_delete=models.CASCADE)
  20.     place = models.ForeignKey(Place, on_delete=models.CASCADE, related_name='likes')
  21.  
  22.     def __str__(self):
  23.         return f"{self.user.username} - {self.place.name}"
  24.  
  25.  
  26. 2. Update place_details.html
  27.  
  28. {% extends 'base.html' %}
  29. {% block main_content %}
  30. <section id="place-details">
  31.     <h1>Place Details</h1>
  32.     <div class="info-section">
  33.         <div class="place-header">
  34.             <img class="car-img" src="{{ place.image_url }}" alt="{{ place.location }}" />
  35.             <h1>{{ place.category }}</h1>
  36.             <p><span class="description">Name: {{ place.name }} </span></p>
  37.             <p><span class="description">Description: {{ place.description }}</span></p>
  38.             <p><span class="description">Rating: {{ place.rating }}</span></p>
  39.         </div>
  40.  
  41.         <!-- Edit/Delete buttons -->
  42.         <div class="buttons">
  43.             <a href="#" class="edit-button">Edit</a>
  44.             <a href="#" class="delete-button">Delete</a>
  45.         </div>
  46.     </div>
  47.  
  48.     <!-- Comments section -->
  49.     <div class="comments-section">
  50.         <h2>Comments</h2>
  51.         <ul>
  52.             {% for comment in place.comments.all %}
  53.             <li>{{ comment.user.username }}: {{ comment.text }}</li>
  54.             {% endfor %}
  55.         </ul>
  56.  
  57.         <!-- Add comment form -->
  58.         <form action="{% url 'add_comment' place.pk %}" method="post">
  59.             {% csrf_token %}
  60.             {{ comment_form.as_p }}
  61.             <button type="submit">Add Comment</button>
  62.         </form>
  63.     </div>
  64.  
  65.     <!-- Likes section -->
  66.     <div class="likes-section">
  67.         <h2>Likes</h2>
  68.         <p>{{ place.likes.count }} Likes</p>
  69.  
  70.         <!-- Like/unlike button -->
  71.         {% if user.is_authenticated %}
  72.         <form action="{% url 'add_like' place.pk %}" method="post">
  73.             {% csrf_token %}
  74.             <button type="submit">{% if user in place.likes.all %}Unlike{% else %}Like{% endif %}</button>
  75.         </form>
  76.         {% endif %}
  77.     </div>
  78. </section>
  79. {% endblock %}
  80.  
  81.  
  82. 3. Create Views
  83.  
  84. # views.py
  85.  
  86. from django.shortcuts import redirect, get_object_or_404
  87. from django.contrib.auth.decorators import login_required
  88. from .forms import CommentForm, LikeForm
  89. from .models import Comment, Like  # Importing the models
  90.  
  91. @login_required
  92. def add_comment(request, place_id):
  93.     place = get_object_or_404(Place, pk=place_id)
  94.     if request.method == 'POST':
  95.         form = CommentForm(request.POST)
  96.         if form.is_valid():
  97.             text = form.cleaned_data['text']
  98.             Comment.objects.create(user=request.user, place=place, text=text)
  99.     return redirect('details_place', pk=place_id)
  100.  
  101. @login_required
  102. def add_like(request, place_id):
  103.     place = get_object_or_404(Place, pk=place_id)
  104.     like, created = Like.objects.get_or_create(user=request.user, place=place)
  105.     if not created:
  106.         like.delete()  # Unlike if already liked
  107.     return redirect('details_place', pk=place_id)
  108.  
  109.  
  110. 4. Forms
  111.  
  112. # forms.py
  113.  
  114. from django import forms
  115.  
  116. class CommentForm(forms.Form):
  117.     text = forms.CharField(widget=forms.Textarea)
  118.  
  119. class LikeForm(forms.Form):
  120.     pass  # No fields needed, likes are handled via button click
  121.  
  122.  
  123. 5. Add in urls
  124.  
  125.  
  126.  
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement