diff options
| author | Bobby <[email protected]> | 2022-09-20 00:22:28 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-20 00:22:28 -0400 |
| commit | 71362e284887e628f411eef3a39387699060a08f (patch) | |
| tree | 5f60de9ca73b44185285959a021535df7ea142fe /blog_admin | |
| parent | cdf4502054453b101c9bf00d5c33241d767fcd94 (diff) | |
| download | thatcomputerscientist-71362e284887e628f411eef3a39387699060a08f.tar.xz thatcomputerscientist-71362e284887e628f411eef3a39387699060a08f.zip | |
Creating posts and auto create tags
Diffstat (limited to 'blog_admin')
| -rw-r--r-- | blog_admin/views.py | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/blog_admin/views.py b/blog_admin/views.py index b139a351..ff11ba26 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -39,31 +39,32 @@ 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() if request.method == 'POST': - print(request.POST) - # title = request.POST.get('title') - # body = request.POST.get('body') - # category = request.POST.get('category') - # tags = request.POST.get('tags') - # slug = request.POST.get('slug') - # if title and body and category and tags and slug: - # try: - # category = Category.objects.get(slug = category) - # tags = tags.split(',') - # tags = [Tag.objects.get(slug = tag) for tag in tags] - # post = Post.objects.create(title = title, body = body, category = category, slug = slug, author = request.user) - # post.tags.set(tags) - # post.save() - # messages.success(request, 'Post created successfully!') - # return redirect('blog-admin:posts') - # except Exception as e: - # messages.error(request, 'Error: {}'.format(e), extra_tags='new_post_create_error', data = { 'title': title, 'body': body, 'category': category, 'tags': tags, 'slug': slug }) - # return redirect('blog-admin:new-post') - # else: - # messages.error(request, 'Error: All fields are required!', extra_tags='new_post_create_error', data = { 'title': title, 'body': body, 'category': category, 'tags': tags, 'slug': slug }) - # return redirect('blog-admin:new-post') + title = request.POST.get('title') + body = request.POST.get('body') + print(body) + category = request.POST.get('category') + tags = request.POST.get('tags') + slug = request.POST.get('slug') + if title and body and category and tags and slug: + try: + category = Category.objects.get(slug = category) + tags = tags.split(',') + tags = [tag.strip() for tag in tags] + tags = [Tag.objects.get_or_create(slug = tag, name = tag)[0] for tag in tags] + post = Post.objects.create(title = title, body = body, category = category, slug = slug, author = request.user) + post.tags.set(tags) + post.save() + messages.success(request, 'Post created successfully!') + return redirect('blog-admin:posts') + except Exception as e: + messages.error(request, 'Error: {}'.format(e), extra_tags='new_post_create_error') + return render(request, 'blog_admin/new_post.html', { 'title': 'New Post', 'categories': categories, 'blog_title': title, 'blog_body': body, 'blog_category': category, 'blog_tags': tags, 'blog_slug': slug }) + else: + messages.error(request, 'Error: All fields are required!', extra_tags='new_post_create_error') + return render(request, 'blog_admin/new_post.html', { 'title': 'New Post', 'categories': categories, 'blog_title': title, 'blog_body': body, 'blog_category': category, 'blog_tags': tags, 'blog_slug': slug }) else: - categories = Category.objects.all() return render(request, 'blog_admin/new_post.html', { 'title': 'New Post', 'categories': categories }) else: return redirect('blog:home') |
