Задать вопрос
Ответы пользователя по тегу Django
  • Не могу перейти на страницу с одним постом из списка новостей?

    Robertgoodman
    @Robertgoodman
    Backend developer
    <a href="{% url "post" news.id %}"  >
                         <div class="categories">{{ news.title }}</div>
                            <h5>{{ news.text| linebreaks|truncatechars:200 }}</h5>
                   </a>
    Ответ написан
    Комментировать
  • Как сделать заказы на Django?

    Robertgoodman
    @Robertgoodman
    Backend developer
    models
    class Product(models.Model):
        amount = models.IntegerField(default=0, verbose_name='Количество товара')


    html
    <form method="post" action="{% url 'addtoshopcart' product_single.id %}">
                            {% csrf_token %}
                            <div class="quantity">
                                <p>Quantity</p>
                                <div class="pro-qty"><input type="number" value="1" name="quantity" min="1" max="{{ product_single.amount }}"></div>
                            </div>
                            <button type="submit"   {% if product_single.amount < 1  %} disabled {% endif %} class="site-btn">Add to Cart</button>
                        </form>
    Ответ написан
    Комментировать
  • Как сделать систему лайков в Django?

    Robertgoodman
    @Robertgoodman
    Backend developer
    models:
    from django.db import models
    from django.contrib.auth.models import User
    class Video(models.Model):
             likes = models.ManyToManyField(User, blank=True, related_name='likes')
             dislikes = models.ManyToManyField(User, blank=True, related_name='dislikes')


    views:
    from django.shortcuts import render
    from django.contrib.auth.mixins import LoginRequiredMixin
    from .models import  Video
    from django.views import View
    from django.http import HttpResponseRedirect
    from django.urls import revers
    
    class AddLike(LoginRequiredMixin, View):
    
        def post(self, request, pk, *args, **kwargs):
            post = Video.objects.get(pk=pk)
    
            is_dislike = False
    
            for dislike in post.dislikes.all():
                if dislike == request.user:
                    is_dislike = True
                    break
    
    
            if is_dislike:
                post.dislikes.remove(request.user)
    
            is_like = False
    
            for like in post.likes.all():
                if like == request.user:
                    is_like = True
                    break
    
            if not is_like:
                post.likes.add(request.user)
    
            if is_like:
                post.likes.remove(request.user)
    
            return HttpResponseRedirect(reverse('video', args=[str(pk)]))
    
    
    
    class AddDislike(LoginRequiredMixin, View):
    
        def post(self, request, pk, *args, **kwargs):
            post = Video.objects.get(pk=pk)
    
            is_like = False
    
            for like in post.likes.all():
                if like == request.user:
                    is_like = True
                    break
    
            if is_like:
                post.likes.remove(request.user)
    
    
    
            is_dislike = False
    
            for dislike in post.dislikes.all():
                if dislike == request.user:
                    is_dislike = True
                    break
    
            if not is_dislike:
                post.dislikes.add(request.user)
    
            if is_dislike:
                post.dislikes.remove(request.user)
    
            return HttpResponseRedirect(reverse('video', args=[str(pk)]))


    urls
    from django.urls import path
    from . import views
    
    
    urlpatterns = [
        path('<int:pk>/like/', AddLike.as_view(), name='like'),
        path('<int:pk>/dislike/', AddDislike.as_view(), name='dislike')
    ]


    html:
    <form method="post" action="{% url 'like' video.pk %}">
                  {% csrf_token %}
                  <div style="margin-top: 10px; width: 30px; height: 30px; margin-left: 950px;">
                      <input type="hidden" name="text" value="{{ request.path }}">
                      <button style="background: transparent; border: none; box-shadow: none;" type="submit">
                          <img src="{% static 'images/like.png' %}">
                          <span>{{ video.likes.all.count }}</span>
                      </button>
                  </div>
    
              </form>
    
    
               <form method="post" action="{% url 'dislike' video.pk %}">
                  {% csrf_token %}
                  <div style="margin-top: -30px; width: 30px; height: 30px; margin-left: 980px;">
                      <input type="hidden" name="text" value="{{ request.path }}">
                      <button style="background: transparent; border: none; box-shadow: none;" type="submit">
                         <img src="{% static 'images/dislike.png' %}">
                          <span>{{ video.dislikes.all.count }}</span>
                      </button>
                  </div>
               </form>
    Ответ написан