Creation View doesn't save to database

My creation view redirect to blog main page well after creation but

i can’t find any post’s been created in the posts or admin page posts, can anyone help please

here is my view

def add_post(request):
    if request.method == 'POST':
        post_form = PostForm(request.POST, request.FILES, instance=request.user)
        snippet_form = SnippetForm(request.POST)
        if post_form.is_valid() and snippet_form.is_valid():
            post =
            snpt =
            post.creator = request.user
            return redirect('blog:index')
        post_form = PostForm()
        snippet_form = SnippetForm()
    return render(request, 'blog/add_post.html', {'post': post_form, 'snpt': snippet_form})

what’s wrong in this view cause i been able to save new post from admin add new post but from client it doesn’t save anything

*Any required snippet i will provide but the problem is in this snippet
any help is really appreciable

Screenshot 2022-11-17 at 6.34.50 PM


my Post model

class Post(models.Model):
    creator = models.OneToOneField(settings.AUTH_USER_MODEL...
    snippet = models.OneToOneField(Snippet,...

Couple general comments:

Assuming that PostForm is a form for Post and not User, this instance parameter is not correct. If this is creating a new Post, you don’t want to include the instance parameter at all.

This line that you already have:

will do the right thing here.

It appears that you are also making the assumption that a OneToOne relationship implies that the PK fields would all have the same value. This is not correct. There is no requirement that those values all remain the same. (In fact, you could even have different field types for the PKs of the related tables, which means they cannot even be the same.) You need to assign the references appropriately between Post and Snippet.


This is probably not going to work for you. Python short-circuits boolean operators - this means that the second statement ( is only going to run if returns a “truthy” value. Unless you’ve overridden the save method within your Post object, this will fail.
Separate these two calls into two separate statements.

This is all I can see from what you’ve got posted. To provide any more specific or detailed information would require you to post the complete models and forms being used here.

here is the forms bro

class PostForm(forms.ModelForm):
    add_code = forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'checkbox', 'id': 'post-snippet'}), required=False)# attr. for JS addEvent or css styling
    tag = forms.ModelChoiceField(queryset=Tag.objects.all(), widget=forms.Select())
    genre = forms.ModelChoiceField(queryset=Genre.objects.all(), widget=forms.Select())
    class Meta:
        model = Post
        #fields = '__all__'
        #fields = ['title', 'tag', 'maintainer', 'post_img', 'content', 'snippet', 'genre', 'post_language', 'video', 'post_type', 'urls', 'views', 'status' ]
        exclude = ('creator', 'snippet')

class SnippetForm(forms.ModelForm):
    class Meta:
        model = Snippet
        fields = '__all__'

i was trying everything cause i already was trying the view without instance=request.user
would you mean by changing the relation references appropriately between Post and Snippet, that i have to put ForeignKey relation in Post model ?
you were right about the short circuit and this is the second time i forget that
okay i will try what you recommend and back to you bro

I’m assuming there’s no ForeignKey or OneToOneField in Snippet, so you can save snippet_form without the commit=False. (It’s not needed because there’s no reason to change anything in that model other than what’s in the form.)

This means that you now have snpt as an instance of Snippet. You then assign it to the snippet field of post (e.g. post.snippet = snpt) instead of = (this statement should be removed).

Then, you only need to do the, because that’s the only model changed.

Thanks so much bro that’s right, you really an expert appreciated so much