Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda