So, i'm trying to allow a user to favorite a post (in my case Deals), which will later be displayed on their profile page.
**UPDATED --The problem I'm having--when the form to save a favorite is submitted, the favorite is not saving in database.
I got my initial inspiration from this post here
So i added a 'favorites' field to my User model with a Many to Many relationship on my Deal model...like so:
favorites = models.ManyToManyField(Deal, related_name='favorited_by')
My html template where a user can favorite a 'Deal' looks like this:
<form id="favorite{{deal_detail.id}}" method="POST" action="{% url 'deals:favorite' deal_detail.id %}">
{% csrf_token %}
<input type="hidden" name="supporttype" />
<input type="submit" value="Add Deal to Favorites" />
My url for favorites is this:
url(r'^(?P<pk>[0-9]+)/favorite', favorite, name='favorite')
this is the view for 'deal_detail' and 'favorite'
def deal_by_detail(request, slug):
deal_detail = Deal.objects.get(slug=slug)
return render(request, 'deals/deal_detail.html', {'deal_detail': deal_detail})
def favorite(request, pk):
if request.method == 'POST':
deal = Deal.objects.get(pk=pk)
deal.save()
messages.add_message(request, messages.INFO, 'Deal Favorited.')
return redirect('home')
and here are my full User and Deals models:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, error_messages={'unique':"This email has already been registered."})
username = models.CharField(max_length=40, default='')
first_name = models.CharField(max_length=40, default='', blank=True)
last_name = models.CharField(max_length=40, default='', blank=True)
date_joined = models.DateTimeField(default=timezone.now)
favorites = models.ManyToManyField(Deal, related_name='favorited_by', null=True, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
class Deal(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=140, unique=True)
description = models.TextField(default='')
retailer = models.ForeignKey(Retailer)
image = VersatileImageField('deal image',
upload_to=deal_upload_path,
null=True,
blank=True)
link = models.URLField(default='')
category = models.ForeignKey(Category)
date_added = models.DateField(default=timezone.now)
date_expires = models.DateField(default=timezone.now)
price = models.CharField(max_length=140)
secondary_price = models.CharField(max_length=140, default='')
likes_total = models.IntegerField(default=1)
Okay, so i'm not sure if this is the perfect way to do this, but it appears to be working. I created a variable 'favorite' to grab the selected item and then then saved in the user object.
def favorite(request, pk):
if request.method == 'POST':
favorite = Deal.objects.get(pk=pk)
user = request.user
user.favorites.add(favorite)
messages.add_message(request, messages.INFO, 'Deal Favorited.')
return redirect('home')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.