Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Create models comment и like;:
- from django.db import models
- from django.contrib.auth.models import User
- from my_holiday.destination.models import Place
- class Comment(models.Model):
- user = models.ForeignKey(User, on_delete=models.CASCADE)
- place = models.ForeignKey(Place, on_delete=models.CASCADE)
- text = models.TextField()
- created_at = models.DateTimeField(auto_now_add=True)
- def __str__(self):
- return f"{self.user.username} - {self.place.name} - {self.created_at}"
- class Like(models.Model):
- user = models.ForeignKey(User, on_delete=models.CASCADE)
- place = models.ForeignKey(Place, on_delete=models.CASCADE, related_name='likes')
- def __str__(self):
- return f"{self.user.username} - {self.place.name}"
- 2. Update place_details.html
- {% extends 'base.html' %}
- {% block main_content %}
- <section id="place-details">
- <h1>Place Details</h1>
- <div class="info-section">
- <div class="place-header">
- <img class="car-img" src="{{ place.image_url }}" alt="{{ place.location }}" />
- <h1>{{ place.category }}</h1>
- <p><span class="description">Name: {{ place.name }} </span></p>
- <p><span class="description">Description: {{ place.description }}</span></p>
- <p><span class="description">Rating: {{ place.rating }}</span></p>
- </div>
- <!-- Edit/Delete buttons -->
- <div class="buttons">
- <a href="#" class="edit-button">Edit</a>
- <a href="#" class="delete-button">Delete</a>
- </div>
- </div>
- <!-- Comments section -->
- <div class="comments-section">
- <h2>Comments</h2>
- <ul>
- {% for comment in place.comments.all %}
- <li>{{ comment.user.username }}: {{ comment.text }}</li>
- {% endfor %}
- </ul>
- <!-- Add comment form -->
- <form action="{% url 'add_comment' place.pk %}" method="post">
- {% csrf_token %}
- {{ comment_form.as_p }}
- <button type="submit">Add Comment</button>
- </form>
- </div>
- <!-- Likes section -->
- <div class="likes-section">
- <h2>Likes</h2>
- <p>{{ place.likes.count }} Likes</p>
- <!-- Like/unlike button -->
- {% if user.is_authenticated %}
- <form action="{% url 'add_like' place.pk %}" method="post">
- {% csrf_token %}
- <button type="submit">{% if user in place.likes.all %}Unlike{% else %}Like{% endif %}</button>
- </form>
- {% endif %}
- </div>
- </section>
- {% endblock %}
- 3. Create Views
- # views.py
- from django.shortcuts import redirect, get_object_or_404
- from django.contrib.auth.decorators import login_required
- from .forms import CommentForm, LikeForm
- from .models import Comment, Like # Importing the models
- @login_required
- def add_comment(request, place_id):
- place = get_object_or_404(Place, pk=place_id)
- if request.method == 'POST':
- form = CommentForm(request.POST)
- if form.is_valid():
- text = form.cleaned_data['text']
- Comment.objects.create(user=request.user, place=place, text=text)
- return redirect('details_place', pk=place_id)
- @login_required
- def add_like(request, place_id):
- place = get_object_or_404(Place, pk=place_id)
- like, created = Like.objects.get_or_create(user=request.user, place=place)
- if not created:
- like.delete() # Unlike if already liked
- return redirect('details_place', pk=place_id)
- 4. Forms
- # forms.py
- from django import forms
- class CommentForm(forms.Form):
- text = forms.CharField(widget=forms.Textarea)
- class LikeForm(forms.Form):
- pass # No fields needed, likes are handled via button click
- 5. Add in urls
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement