0
votes
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']

1

1 Answers

1
votes

Not sure what the get_content_type func does in your post model, from the error, it is returning the ContentType object.

ContentType matching query does not exist

1st option: can you first add .model in value, so it should look like this

initial_data = {
        "content_type": instance.get_content_type.model,
        "object_id": instance.id
}

and see if it works.

2nd option: if the 1st option shows you an error please remove .model so you keep your code as it is

initial_data = {
        "content_type": instance.get_content_type,
        "object_id": instance.id
}

and remove those:

c_type = form.cleaned_data.get("content_type")
content_type = ContentType.objects.get(model=c_type)

and add one of these

content_type = ContentType.objects.get_for_model(instance)

or

content_type = ContentType.objects.get(model='post')

see if it works, hope it helps...

also, why do you sign two different auth?

if not request.user.is_staff or not request.user.is_superuser:
if request.user.is_authenticated:

The first if statement already allows only super and staff.