Reputation: 311
I hope someone can help me to figure out what I am doing wrong.
I am trying to filter the blog articles by tags, so that certain articles are render in all-articles.html
when the user clicks on the tag.
Any help is appreciated!
Below is the error I get in the console with the context.
Error in Terminal
/Users/haashe/Documents/Projects/LensTok/lenstok-main/django/lenstokyo_backend/blogapp/views.py:30: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'blogapp.models.Articles'> QuerySet. paginator = Paginator(articles_list, 12) Internal Server Error: /all-articles Traceback (most recent call last): File "/Users/haashe/.local/share/virtualenvs/django-FSWjPP8U/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/Users/haashe/.local/share/virtualenvs/django-FSWjPP8U/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/haashe/.local/share/virtualenvs/django-FSWjPP8U/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/Users/haashe/Documents/Projects/LensTok/lenstok-main/django/lenstokyo_backend/blogapp/views.py", line 39, in allarticles data = serializers.serialize('json', Articles.objects.filter(field_name=tag)) AttributeError: module 'rest_framework.serializers' has no attribute 'serialize'
All-articles.html
<div class="tag-nav-links">
<form action="" method="post">
{% csrf_token %} {% for i in all_tags%}
<p>{{ i.tag }}</p>
{% endfor %}
</form>
</div>
app.js
$(document).ready(function () {
$(".tag-nav-links").on("click", function (e) {
e.stopPropagation();
return $.ajax({
type: "POST",
url: "",
data: { filter: `${e.target.textContent}` },
});
});
});
views.py
from django.views.decorators.csrf import csrf_exempt
from rest_framework import serializers
@csrf_exempt
def allarticles(request):
articles_list = Articles.objects.all()
paginator = Paginator(articles_list, 12)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
all_tags = Tags.objects.all()
if request.is_ajax():
tag = request.POST['filter']
data = serializers.serialize('json', Articles.objects.filter(field_name=tag))
return JsonResponse(data, safe=False)
context = {'page_obj': page_obj, 'all_tags': all_tags}
return render(request, "All-articles.html", context)
models.py
class ArticleTags(models.Model):
article = models.ForeignKey('Articles', models.DO_NOTHING)
tag = models.ForeignKey('Tags', models.DO_NOTHING)
class Tags(models.Model):
tag = models.CharField(unique=True, max_length=75)
tagslug = models.SlugField(max_length=200, unique=True, default=None)
class Articles(models.Model):
title = models.CharField(max_length=155)
metatitle = models.CharField(max_length=155)
slug = models.SlugField(unique=True, max_length=155)
summary = models.TextField(blank=True, null=True)
field_created = models.DateTimeField(db_column='_created', blank=True, null=True)
cover = models.ImageField(upload_to="cover", blank=True, default='logo-00-06.png')
Upvotes: 1
Views: 302