from django.contrib.contenttypes.models import ContentType
def posts_detail(request,slug=None):
instance = get_object_or_404(Post, slug=slug)
if instance.publish > datetime.datetime.now().date() or instance.draft:
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
share_string = quote_plus(instance.content)
initial_data = {
"content_type": instance.get_content_type,
"object_id": instance.id
}
form = CommentForm(request.POST or None, initial=initial_data)
if form.is_valid() and request.user.is_authenticated:
c_type = form.cleaned_data.get("content_type")
content_type = ContentType.objects.get(model=c_type)
obj_id = form.cleaned_data.get('object_id')
content_data = form.cleaned_data.get("content")
new_comment, created = Comment.objects.get_or_create(
user = request.user,
content_type= content_type,
object_id = obj_id,
content = content_data,
)
if created:
print("yeah it worked")
comments = instance.comments
context = {
"title": instance.title,
"instance": instance,
"share_string": share_string,
"comments": comments,
"comment_form":form,
}
return render(request, "post_detail.html", context)
Showing following error
ContentType matching query does not exist.
Request Method: POST
Request URL: http://127.0.0.1:8000/posts/gchgjvhbk/
Django Version: 3.0.7
Exception Type: DoesNotExist
Exception Value:
ContentType matching query does not exist.
Exception Location: C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py in get, line 417
Python Executable: C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\python.exe
Python Version: 3.7.6
Python Path:
['C:\Users\ANUPYADAV\Desktop\try19\src',
'C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\Scripts',
'C:\Users\ANUPYADAV\Desktop\try19\src',
'C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\python37.zip',
'C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\DLLs',
'C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\lib',
'C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37',
'C:\Users\ANUPYADAV\AppData\Roaming\Python\Python37\site-packages',
'C:\Users\ANUPYADAV\AppData\Local\Programs\Python\Python37\lib\site-packages']