I'm using django-favorite, https://bitbucket.org/last_partizan/django-favorites and trying to display "Get only blogpost favorites for user" strategy to get my favorites. in documentation, it says
content_type = get_object_or_404(ContentType, app_label='myblogapp', model='blogpost')
favs = Favorite.objects.favorites_for_user(user).filter(content_type=content_type)
will do the job. So in my views.py I have added
def index(request):
user = MyProfile.objects.get(username=user)
categories = Category.objects.all()
try:
sort = request.GET["sort"].strip()
sort_method = SortMethods[sort]
page = request.GET["page"].strip()
except KeyError:
sort_method = SortMethods.score
page = 1
if sort_method == SortMethods.date:
post_list = Post.objects.order_by("-pub_date")
else:
post_list = Post.objects.all()
post_list = sorted(post_list, key=lambda x: x.get_score(), reverse=True)
paginator = Paginator(post_list, 30)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
content_type = get_object_or_404(ContentType, app_label='main', model='post')
favs = Favorite.objects.favorites_for_user(user).filter(content_type=content_type)
context = {
"posts": posts,
"pages": paginator.page_range,
"sort": sort_method.name,
"categories":categories,
"favs":favs,
}
return render(request, "main/index.html", context)
and to use that fav in my index.html, I have simply put {{fav}}, I'm not even sure this is the right way to call the favorites. But I tried and now it gives me UnboundLocalError at /
local variable 'user' referenced before assignment that's coming from ` user = MyProfile.objects.get(username=user)
` I have userena app, inside userena model
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User,
unique=True,
verbose_name=_('user'),
related_name='my_profile')
intro = models.CharField(_('intro'),
max_length=5, default="Hello")
I'm calling from here to get user...I hope this is right.
Should that first line be using:
request.user
So like this:
user = MyProfile.objects.get(username=request.user)
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.