how can i get the "post id"(foreign key field connected with comment model) to the django comments form without post_detail page in django

I am creating a single page blog site how can i get the post id to the comments form , I created django forms with necessary field but the problem is ,I have to select the post id from a drop down menu manually while commenting, for that I passed post object as an input value of a form to views.py file but django needs instance to save in database what should I do now note :I am not using post_detail

models.py

class comments(models.Model):
    name=models.CharField(max_length=255)
    content=models.TextField()
    post=models.ForeignKey(blog,related_name="comments",on_delete=models.CASCADE) 
    #blog is the model to which comment is related
    date=models.DateTimeField(auto_now_add=True)

forms.py

class commentform(ModelForm):
    class Meta:
        model=comments
        fields=('name','content','post')

        widgets={
            'name' : forms.TextInput(attrs={'class':'form-control','placeholder':'type your name here'}),
            'content' : forms.Textarea(attrs={'class':'form-control'}),
            'post' : forms.Select(attrs={'class':'form-control'})
        }
    

Html

<form method='POST' action="comment_action" class='form-group'>
{%csrf_token%}
{{form.name}}
{{form.content}}
<input type="text" id="objid" name="objid" value="{{objs.id}}" hidden>
<button class="btn btn-primary btn-sm shadow-none"  type="submit">Post comment</button>

views.py

def comment_action(request):
    name=request.POST.get('name')
    content=request.POST.get('content')
    objid=request.POST.get('objid')
    to_db=comments.objects.create(name=name,content=content,post=objid)
    print(name,content,objid)
    return redirect('/homepage')
    return render(request, 'index.html')

ERROR :

Exception Type: ValueError Exception Value:
Cannot assign "'48'": "comments.post" must be a "blog" instance.

-->48 is my blog id

i know that database will only accept instance post field because that was a foreign key my question is how to pass it ?


You assign it with post_id:

def comment_action(request):
    name = request.POST.get('name')
    content = request.POST.get('content')
    objid = request.POST.get('objid')
    to_db = comments.objects.create(name=name, content=content, post_id=objid)
    print(name,content,objid)
    return redirect('/homepage')

Note: It is better to use a Form [Django-doc] than to perform manual validation and cleaning of the data. A Form will not only simplify rendering a form in HTML, but it also makes it more convenient to validate the input, and clean the data to a more convenient type.


Note: normally a Django model is given a singular name, so Comment instead of comments.