diff options
| author | Bobby <[email protected]> | 2023-03-10 20:48:22 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-03-10 20:48:22 -0500 |
| commit | 86099bb4ae4b5aa6ef1bab9633bf92e2b5252c29 (patch) | |
| tree | 72d16e2e98f5b766eae9d6bec7930fb0a83e2bf0 /blog_admin | |
| parent | 4d1ee5092ae59b2e40d5d7d22e78a5abce3e3159 (diff) | |
| download | thatcomputerscientist-86099bb4ae4b5aa6ef1bab9633bf92e2b5252c29.tar.xz thatcomputerscientist-86099bb4ae4b5aa6ef1bab9633bf92e2b5252c29.zip | |
Tagging fix for admin side with multi select
Diffstat (limited to 'blog_admin')
| -rw-r--r-- | blog_admin/views.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/blog_admin/views.py b/blog_admin/views.py index 29b1f0aa..49e309a3 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -36,12 +36,16 @@ def posts_search(request): def new_post(request): if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): categories = Category.objects.all() + all_tags = [tag.name for tag in Tag.objects.all()] if request.method == 'POST': title = request.POST.get('title') category = request.POST.get('category') - tags = request.POST.get('tags') + tags = request.POST.getlist('tags') slug = request.POST.get('slug') if request.POST.get('slug') else '' post_image = request.FILES['post_image'] if 'post_image' in request.FILES else None + additional_tags = request.POST.get('additional_tags') if request.POST.get('additional_tags') else '' + if additional_tags: + tags += additional_tags.split(',') if request.POST.get('post_id'): # update post @@ -52,7 +56,7 @@ def new_post(request): post.title = title post.category = Category.objects.get(id = category) post.slug = slug - post.tags.set([Tag.objects.get_or_create(slug = tag.strip(), name = tag.strip())[0] for tag in tags.split(',')]) + post.tags.set([Tag.objects.get_or_create(name = tag.strip())[0] for tag in tags]) if post_image: post.post_image = post_image post.save() @@ -64,13 +68,13 @@ def new_post(request): else: if not title or not category or not tags or not post_image: messages.error(request, 'Fields marked with asterisk (*) are required.', extra_tags='new_post_message') - return_object = { 'title_value': title, 'category_value': category, 'tags_value': tags, 'slug_value': slug, 'post_image_value': post_image } + return_object = { 'title_value': title, 'category_value': category, 'tags_value': tags, 'slug_value': slug, 'post_image_value': post_image, 'all_tags': all_tags } return render(request, 'blog_admin/new_post.html', { 'title': 'New Post', 'categories': categories, 'return_object': return_object }) else: # create new post try: post = Post.objects.create(title = title, category = Category.objects.get(id = category), slug = slug, author = request.user, post_image = post_image) - post.tags.set([Tag.objects.get_or_create(slug = tag.strip(), name = tag.strip())[0] for tag in tags.split(',')]) + post.tags.set([Tag.objects.get_or_create(name = tag.strip())[0] for tag in tags]) post.save() return redirect(reverse('blog-admin:edit-post', kwargs = { 'slug': post.slug })) except Exception as e: @@ -86,13 +90,11 @@ def new_post(request): post_id = None mode = 'new' if mode == 'edit' and post_id: - post_tags = post.tags.all() - post_tags = [tag.name for tag in post_tags] - post_tags = ', '.join(post_tags) - post = { 'id': post.id, 'title': post.title, 'category': post.category.id, 'tags': post_tags, 'slug': post.slug, 'post_image': post.post_image } - return render(request, 'blog_admin/new_post.html', { 'title': 'Edit Post', 'categories': categories, 'post': post }) + post_tags = [tag.name for tag in post.tags.all()] + post = { 'id': post.id, 'title': post.title, 'category': post.category.id, 'tags': post_tags, 'slug': post.slug, 'post_image': post.post_image, 'all_tags': all_tags } + return render(request, 'blog_admin/new_post.html', { 'title': 'Edit Post', 'categories': categories, 'post': post, 'all_tags': all_tags }) - return render(request, 'blog_admin/new_post.html', { 'title': 'Create New Post', 'categories': categories }) + return render(request, 'blog_admin/new_post.html', { 'title': 'Create New Post', 'categories': categories, 'all_tags': all_tags }) else: return redirect('blog:home') |
