Well, I am building a blogging website, where I want users to register themselves and then post their articles. But I am having trouble in saving the article. When I click "Send" button the post doesn't get submitted. I have used django forms.py for the purpose. After investigating I found out that the form is not getting validated. form.is_valid() is returning false. How to get it done? Here is my code:
class Writer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.TextField()
username = models.CharField(max_length=15, unique=True)
email = models.EmailField(unique=True)
profile_pic = models.ImageField(upload_to = 'Bloggers')
bio = models.TextField()
locality = models.TextField()
state = models.TextField()
country = models.TextField()
instagram = models.URLField(unique=True, null=True)
linkedin = models.URLField(unique=True, null=True)
facebook = models.URLField(unique=True, null=True)
def __str__(self):
return self.name + ' , aka, ' + self.username
class Post(models.Model):
blogChoice = (
('Sneak Peek', 'Sneak Peek'),
('Events', 'Events'),
('Lifestyle', 'Lifestyle'),
('Trends', 'Trends'),
)
blogType = models.CharField(max_length=25, choices=blogChoice,null=True)
title = models.CharField(max_length=25)
cover = models.ImageField(upload_to='Blog Image')
content = models.TextField()
author = models.OneToOneField(Writer, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True, blank=True)
approve = models.BooleanField(default=False)
recommend = models.BooleanField(default=False)
time = models.IntegerField()
def __str__(self):
return self.title + ' by ' + self.author
class PostForm(ModelForm):
class Meta:
model = Post
fields = '__all__'
exclude = ['timestamp', 'approve', 'recommend']
def writewithus(request):
form = PostForm()
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
print("hi")
if form.is_valid():
print("hey")
c = form.save()
if c:
print("hello")
return redirect('/')
# only "hi" gets printed
context = {'form':form}
return render(request, 'Blog/writewithus.html', context)
<form class="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="file btn btn-lg btn-primary mt-3"
style="background: linear-gradient(to right, #00d2ff, #3a7bd5);">
Upload Article cover
{{form.cover}}
</div>
<div class="form-group">
{{form.title}}
</div>
<div class="form-group">
{{form.content}}
</div>
<div class="form-group">
{{form.author}}
</div>
<div class="form-group">
{{form.blogType}}
</div>
<div class="form-group">
{{form.time}}
</div>
<div class="form-check mt-3">
<input type="checkbox" class="form-check-input" id="exampleCheck1" required>
<label class="form-check-label" for="exampleCheck1">I agree to the totellexpress <a href="">Terms of
service</a> and <a href="">Privacy Policy.</a></label>
</div>
<button type="submit" class="btn btn-dark mt-3">Send</button>
</form>
Checkout form.errors
for validation errors. Then you will know, why it doesn't save.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.