'When I make a post it shows this errorField ‘id’ expected a number but got ‘d’. NOTE: the post gets created but i get this error
I have tried a lot of things to resolve this issue but they are not working that's why I want to ask somebody that's better than me here. Ive tried using a slug field it doesnt show that, but i wanna use id that when it starts showing the error. NOTE: the post gets created perfectly well and that it then shows this error but the posts was made. I tried making the post using a forms from my frontend
let me show some code views.py
def blogpost(request):
if request.method == "POST":
form = BlogPostForm(request.POST, request.FILES)
if form.is_valid():
form.instance.creator = request.user
form.save() # ← no commit=False
messages.success(request, f'Hi, Your Post have been sent for review and would be live soon!')
return redirect('blog:home')
else:
form = BlogPostForm()
context = {
'form': form
}
return render(request, 'blog/AddPost.html', context)
# this is the blog list view
def blog_list(request):
posts = Blog.objects.filter(status='published').order_by('-created')
categoriess = Category.objects.all()
context = {
'posts': posts,
'categories': categoriess,
}
return render(request, 'blog/bloghome.html', context)
#this is the blog detail view
def blog_detail(request, post_id):
post = get_object_or_404(Blog, id=post_id)
# post = Blog.objects.filter(slug=blog_slug)
categories = Category.objects.all()
comments = post.comments.filter(active=True)
new_comment = None
if request.method == "POST":
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.post = post
new_comment.name = request.user
new_comment.save()
else:
comment_form = CommentForm()
context = {
'post': post,
'comments': comments,
'comment_form': comment_form,
'new_comment': new_comment,
'categories': categories,
}
return render(request, 'blog/blog-details.html', context)
forms.py
class BlogPostForm(forms.ModelForm):
image = forms.ImageField(widget=forms.ClearableFileInput(attrs={'multiple': True}), required=True)
# content = forms.CharField(widget=forms.Textarea(attrs={'class': 'input is-medium'}), required=True)
tags = forms.CharField(widget=forms.TextInput(attrs={'class': 'input is-medium'}), required=True)
class Meta:
model = Blog
fields = ('title', 'content', 'image', 'category', 'tags')
addpost.html
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form|crispy}}
<div class="form-group">
<button class="btn theme-bg rounded" type="submit">Send Message</button>
</div>
</form>
urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.blog_list, name="home"),
# path('post/<uuid:posts_id>', views.blog_detail, name="blog-details"),
path('<uuid:post_id>', views.blog_detail, name='blog-details'),
path('post/categories/<slug:category_slug>', views.category, name="category"),
path('post/tags/<slug:tag_slug>', views.tag, name="tags"),
path('post/create/', views.blogpost, name="add-post"),
]
any help would be appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
