diff options
| author | Bobby <[email protected]> | 2022-09-20 02:03:36 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-20 02:03:36 -0400 |
| commit | c5254b6f39939bc0bc6719b854b67928af74c9ea (patch) | |
| tree | 692510e10ba5619f70b1b3df23352721cfac7159 /blog_admin | |
| parent | e29f7902c8d6dc6e6ee02964b56284d2f69f1ac5 (diff) | |
| download | thatcomputerscientist-c5254b6f39939bc0bc6719b854b67928af74c9ea.tar.xz thatcomputerscientist-c5254b6f39939bc0bc6719b854b67928af74c9ea.zip | |
Added Edit post function
Diffstat (limited to 'blog_admin')
| -rw-r--r-- | blog_admin/urls.py | 1 | ||||
| -rw-r--r-- | blog_admin/views.py | 37 |
2 files changed, 37 insertions, 1 deletions
diff --git a/blog_admin/urls.py b/blog_admin/urls.py index b0710851..a8f6bc49 100644 --- a/blog_admin/urls.py +++ b/blog_admin/urls.py @@ -10,6 +10,7 @@ urlpatterns = [ path('posts', views.posts, name='posts'), path('posts/new', views.new_post, name='new-post'), path('posts/search', views.posts_search, name='posts-search'), + path('posts/<str:slug>/edit', views.edit_post, name='edit-post'), path('comments', views.comments, name='comments'), path('categories', views.categories, name='categories'), path('categories/new', views.new_category, name='new-category'), diff --git a/blog_admin/views.py b/blog_admin/views.py index 7732866a..ca107390 100644 --- a/blog_admin/views.py +++ b/blog_admin/views.py @@ -43,7 +43,6 @@ def new_post(request): if request.method == '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') @@ -69,6 +68,42 @@ def new_post(request): else: return redirect('blog:home') +def edit_post(request, slug): + if request.user.is_authenticated and (request.user.is_superuser or request.user.is_staff): + categories = Category.objects.all() + post = Post.objects.get(slug = slug) + if request.method == '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.strip() for tag in tags] + tags = [Tag.objects.get_or_create(slug = tag, name = tag)[0] for tag in tags] + post.title = title + post.body = body + post.category = category + post.slug = slug + post.author = request.user + post.tags.set(tags) + post.save() + messages.success(request, 'Post edited successfully!') + return redirect('blog-admin:posts') + except Exception as e: + messages.error(request, 'Error: {}'.format(e), extra_tags='edit_post_create_error') + return render(request, 'blog_admin/edit_post.html', { 'title': 'Edit Post', 'categories': categories, 'blog_title': title, 'blog_body': body, 'blog_category': category, 'blog_tags': tags, 'blog_slug': slug, 'post': post }) + else: + messages.error(request, 'Error: All fields are required!', extra_tags='edit_post_create_error') + return render(request, 'blog_admin/edit_post.html', { 'title': 'Edit Post', 'categories': categories, 'blog_title': title, 'blog_body': body, 'blog_category': category, 'blog_tags': tags, 'blog_slug': slug, 'post': post }) + else: + return render(request, 'blog_admin/edit_post.html', { 'title': 'Edit Post', 'categories': categories, 'blog_title': post.title, 'blog_body': post.body, 'blog_category': post.category.slug, 'blog_tags': ','.join([tag.slug for tag in post.tags.all()]), 'blog_slug': post.slug, 'post': post }) + else: + return redirect('blog:home') + def comments(request): pass |
