Q: Form not being saved in Database Django I have created a form for a Post. The form loads and when I enter all the value and try to submit the form, it gets refreshed but the database is not updated.
Here is my Post Model:
class Post(models.Model):
    no_people = models.IntegerField()
    no_days = models.IntegerField()
    tour_date = models.CharField(max_length=200, blank=True)
    Gender_types = (
        ('M', 'Male'),
        ('F', 'Female'),
        ('O', 'Others'),
    )
    Gender_prefer = models.CharField(max_length=1, choices=Gender_types)
    location = models.CharField(max_length=200, blank=True)
    pic_location = models.ImageField(blank=True, upload_to="posts/")
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    detail = models.TextField() 
    liked = models.ManyToManyField(Yatru, blank=True, related_name= 'likes')
    author = models.ForeignKey(Yatru, on_delete=models.CASCADE, related_name = 'posts',null=True)

    def __str__(self):
        return str(self.location)

    def no_likes(self):
        return self.liked.all().count()
    
    class Meta:
        ordering = ('-created',)

    def save(self, *args, **kwargs):
        self.location = self.location.upper()
        return super(Post, self).save(*args, **kwargs)

    LIKE_CHOICES = (
    ('Like', 'Like'),
    ('Unlike', 'Unlike'),
    )

Here is the form.py:
from django import forms
from .models import Post

class PostModelForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('no_people','no_days','tour_date', 'Gender_prefer','location','detail')

Here is the views.py:
def post_add(request):
    user = request.user
    profile = Yatru.objects.get(user=user)
    form = PostModelForm(request.POST or None, request.FILES or None, instance=profile)
    
    if request.method == 'POST':
        if form.is_valid():
            newpost=form.save(commit=False)
            newpost.user=request.user
            newpost.save()
    return render(request, 'posts/my_post.html',{'form': form})

This is the HTML file:
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button type='submit'>Submit </button>
</form>


A: Hi please follow instructions:

*

*First insert "author" field in field tuple of model form


*In your post_add view change below line
newpost.author=request.user
