I am new to Django and am trying to create a favorite application. I would like to have the liked button change based on whether or not the user has already favorited the physician.
I pass physicians and liked list to my template but not sure what to do next to extract liked from the array, and not sure if this the most elegant way to approach this.
Any help would be much appreciated!
models.py
class Physician(models.Model):
speciality = models.CharField(null=False, blank=False, max_length=30)
first_name = models.CharField(null=False, blank=False, max_length=50)
last_name = models.CharField(null=False, blank=False, max_length=50)
title = models.CharField(null=False, blank=False, max_length=2)
address1 = models.CharField(null=False, blank=True, max_length=50)
address2 = models.CharField(null=True, blank=True, max_length=20)
city = models.CharField(null=False, blank=False, max_length=50)
state = models.CharField(null=False, blank=False, max_length=2)
zip = models.CharField(null=False, blank=False, max_length=12)
likes = models.IntegerField(default=1)
def __str__(self):
template = '{0.last_name} {0.first_name} {0.speciality}'
return template.format(self)
class Voter(models.Model):
user = models.ForeignKey(User)
physician = models.ForeignKey(Physician)
My template is
{% if physicians %}
{% for physician in physicians %}
<tr>
<td>
<a href="/getmd/{{physician.id}}/">
{{ physician.last_name }}, {{ physician.first_name }} </a>
</td>
<td>{{ physician.state }}</td>
<td>{{ physician.speciality }}</td>
<td>{{ physician.id}}</td>
<td><strong id="like_count{{physician.id}}">{{ physician.likes }}</strong> </td>
<td>
{% if user.is_authenticated %}
{% if likedalready.physician].liked %}
<button id="alreadyvoted" data-catid="{{physician.id}}" class="likes btn btn-secondary" type="button" title="Already faved">
<span class="glyphicon glyphicon-check"></span>
Faved
</button>
{%else%}
<button id="{{physician.id}}like" data-catid="{{physician.id}}" class="likes btn btn-primary" type="button" title="Fave Me">
<span class="glyphicon glyphicon-thumbs-up"></span>
Fav
</button>
{% endif %}
{% endif %}
</td>
</tr>
{% endfor %}
{% else %}
<p>Sign in to fave</p>
{% endif %}
</tbody>
my views.py def physicians(request):
user = User;
liked = False;
args = {}
args.update(csrf(request))
physicians = Physician.objects.all().order_by('last_name')
page = request.GET.get('page', 1)
paginator = Paginator(physicians, 10)
try:
physicians = paginator.page(page)
except PageNotAnInteger:
physicians = paginator.page(1)
except EmptyPage:
physicians = paginator.page(paginator.num_pages)
#Create list of already liked physicians
return_list = []
for physician in physicians:
liked = Voter.objects.filter(physician_id=physician.id, user_id=request.user.id).exists()
return_list.append((physician, {'liked': liked}))
args['physicians'] = physicians
return render(request, 'physicians.html', args, {'liked': return_list})
I would have a different model, Like:
class Like(models.Model):
user = models.ForeignKey(User)
physician = models.ForeignKey(Physician)
This table will have a relation between a user and a physician. You can know if a user has liked a physician.
Like.objects.filter(user=user, physician=physician).exists()
Delete the relation if the user un-favorites the physician and so on.
To get all the ids of physicians liked by the user, you could do:
physicians_liked = Like.objects.filter(user=user).values_list('physician', flat=True)
And later in your template is as easier as checking:
{% if physician.id in physicians_liked %}
...
{% endif %}