Reputation: 948
I am using Django and attempting to add commenting functionality with AJAX. When I click on the submit button for the comment, the action is not being performed and I am instead seeing a 'django.db.utils.IntegrityError: NOT NULL constraint failed: posts_comment.post_id
' error in the terminal. I have checked my models.py file but am unable to identify the cause of this issue. Could you please provide guidance on how to resolve this error?
model.py
class Comment(MPTTModel):
author = models.ForeignKey(User, related_name='author',
on_delete=models.CASCADE, default=None, blank=True)
parent = TreeForeignKey('self', on_delete=models.CASCADE,
null=True, blank=True, related_name='children')
post = models.ForeignKey(Post ,related_name='comments', on_delete=models.CASCADE)
body = models.TextField()
create_date = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)
view.py
def single_post(request ,*args, **kwargs):
slug=kwargs.get('slug')
print(slug,'slug')
pk=kwargs.get('pk')
print(pk,'pk')
post = get_object_or_404(Post,pk=pk,slug=slug)
allcomments = post.comments.filter(status=True)
comment_form = CommentForm()
return render(
request,
'posts/post_test.html',
{ 'post': post,
'comment_form': comment_form,
'allcomments': allcomments,
}
)
def addcomment(request):
if request.method == 'POST':
comment_form = CommentForm(request.POST)
print(comment_form)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
result = comment_form.cleaned_data.get('body')
user = request.user.username
user_comment.author = request.user
user_comment.save()
return JsonResponse({'result2': result, 'user': user})
urls.py
urlpatterns = [
path('test/<int:pk>/<str:slug>/',views.single_post,name ='post_detail'),
path('commented/',views.addcomment,name='addcomment'),
]
if more information is require than tell me in a comments session. I will update my question with that information. Thank you !
Upvotes: 0
Views: 166
Reputation: 948
Well I have update my url
form path('commented/',views.addcomment,name='addcomment'),
to path('commented/<int:pk>/<str:slug>/',views.addcomment,name='addcomment'),
than i also updated the add comment function from
view.py
def addcomment(request):
if request.method == 'POST':
comment_form = CommentForm(request.POST)
print(comment_form)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
result = comment_form.cleaned_data.get('body')
user = request.user.username
user_comment.author = request.user
user_comment.save()
return JsonResponse({'result2': result, 'user': user})
to : views.py
def addcomment(request,*args, **kwargs):
slug=kwargs.get('slug')
print(slug,'slug')
pk=kwargs.get('pk')
print(pk,'pk')
print(request,'request')
post = get_object_or_404(Post,pk=pk,slug=slug)
if request.method == 'POST':
comment_form = CommentForm(request.POST)
print(comment_form,'postting')
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
result = comment_form.cleaned_data.get('body')
user = request.user.username
user_comment.author = request.user
user_comment.post = post
user_comment.save()
return JsonResponse({'result2': result, 'user': user})
Than it works for me perfectly.
Upvotes: 0