Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
Django- Problems with like/follow button's

I want to create a like/follow system on user posts.

Due to many users being able to like a post and users being able to like many posts I made 2 separate models.

Models.py:

class Question(models.Model):
user= models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.TextField()
content = models.TextField()
date_published = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(_('Last Edited'), auto_now=True)
slug = models.SlugField(max_length=255)

class Question_Follows(models.Model): 
question = models.ForeignKey(Question, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
count = models.IntegerField(default=0)

views.py

class QuestionDetailView(DetailView):

model = Question
slug  = 'slug'
template_name = "questions/question_detail.html" 


 def get_context_data(self, **kwargs):
    context = super(QuestionDetailView, self).get_context_data(**kwargs)
    context['now'] = timezone.now()

    #obj= Question_Follows.objects.filter(id=question.id)
    #context['follow_count'] = Question_Follows.objects.filter(id='question.id')
    #obj.count <----- none of these have worked so far.


    return context

detailview.html:

<div id="question_container">
<input type='hidden' id="{{ object.id }}" name="id">  <--- tried this as a way to get question id -->
<h1>{{ object.title }}</h1>
<p>{{ object.content }}</p>
<p>Published: {{ object.date_published|date }}</p>
<p>Date: {{ now |date }}</p>

<button class="follow"><span id='follow-span'>Follow| <strong id='follow-count'>{{ follow_count }}</strong> </button>
</span></button>

I'm having problems 'collecting' the right question once the like button has been pressed and also showing the right number of likes for the question i.e the follow count for each question.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A few points:

  1. If I'm understanding your intention properly, you should not need count = models.IntegerField(default=0) on the Question_Follows model. A new row is created in the database table for this model every time a user follows a question. Can a user follow the same question more than once? If not, then you don't need a count field.
  2. The number of follows for the question should be available in the view using something like:

    def get_context_data(self, kwargs):

    context = super(QuestionDetailView, self).get_context_data(**kwargs)
    context['now'] = timezone.now()
    context['follow_count'] = self.object.follows.count()
    return context
    

Note that you'll need to add a related name to your foreign key, so it will be question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='follows') for this to work.

  1. It's conventional to name Django models in CamelCase (no underscores), and in singular form - so Question_Follows would be QuestionFollow.

See my comment for further questions related to your template issues - I suspect you are just using the wrong template file name.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda