diff --git a/articles/static/live-preview.js b/articles/static/live-preview.js index 7cfcaf0..f2e8c26 100644 --- a/articles/static/live-preview.js +++ b/articles/static/live-preview.js @@ -69,6 +69,8 @@ function prepareBody() { const element = document.querySelector(input.selector); body.set(input.to, element[input.property]); } + const tagIds = Array.from(document.querySelector("#id_tags").selectedOptions).map(option => option.value).join(); + body.set("tag_ids", tagIds); return body; } diff --git a/articles/templates/articles/snippets/metadata.html b/articles/templates/articles/snippets/metadata.html index 9c86bcc..248196b 100644 --- a/articles/templates/articles/snippets/metadata.html +++ b/articles/templates/articles/snippets/metadata.html @@ -2,7 +2,7 @@ Published on {% include "articles/snippets/datetime.html" %} · {{ article.get_read_time }} min read {% include "articles/snippets/admin_link.html" %} - {% if article.tags.all %} -
{% for tag in article.tags.all %}{{ tag.name }}{% endfor %} + {% if tags %} +
{% for tag in tags %}{{ tag.name }}{% endfor %} {% endif %}

diff --git a/articles/views/api.py b/articles/views/api.py index dbbc9c7..9b9c24b 100644 --- a/articles/views/api.py +++ b/articles/views/api.py @@ -3,7 +3,7 @@ from django.http import HttpResponse from django.shortcuts import render from django.views.decorators.http import require_POST -from articles.models import Article +from articles.models import Article, Tag @login_required @@ -17,5 +17,6 @@ def render_article(request, article_pk): has_code = request.POST.get("has_code") if has_code is not None: article.has_code = has_code == "true" - html = render(request, template, context={"article": article}) + tags = Tag.objects.filter(pk__in=map(int, request.POST.get("tag_ids").split(","))) + html = render(request, template, context={"article": article, "tags": tags}) return HttpResponse(html) diff --git a/articles/views/html.py b/articles/views/html.py index b92e429..3ffbd34 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -136,3 +136,7 @@ class ArticleDetailView(generic.DetailView): obj.save(update_fields=["views_count"]) return obj + + def get_context_data(self, **kwargs): + kwargs["tags"] = self.object.tags.all() + return super().get_context_data(**kwargs)