Django 2.1.2 views.py

from django.shortcuts import render, render_to_response from django.http import HttpResponse from django.views.generic import View from django.contrib import messages from guess.models import Post, Comments from guess.forms import PostForm, CommentsForm # Create your views here. class GuessBook(View): def get(self, request): get_date=Post.objects.all() context = {'post':get_date} return render_to_response('guessApp/form.html', context) def post(self,request): print(request.POST) form = PostForm(request.POST) if form.is_valid(): db = form.save() else: print('error') c = {'form':form} return render(request,'guessApp/form.html', c ) 

models.py

 from django.db import models from django.utils import timezone # Create your models here. class Post(models.Model): article = models.CharField(max_length=25) text=models.TextField(max_length=200) is_created=models.DateTimeField('Date published') def __str__(self): return self.article 

forms.py

 from django import forms from django.core.exceptions import ValidationError from guess.models import Post class PostForm(forms.ModelForm): class Meta: model = Post fields=[ 'article', 'text' ] 

Here is what print (request.POST) gives:

QueryDict: {'csrfmiddlewaretoken': ['3H3Zqte3y11v645QOTUMqcak5ve7bxWyYsS6oUR2Y1hxvHixuqrTeAeHOmCXYeqw'], 'Article': '

Whatever I do, the form does not pass validation.

  • Well, you at least read what errors are written by not passed the validation form - andreymal
  • The only thing this form writes to me is The Post couldn’t have been created because the data didn't validate. - Vladislav
  • In form.errors are normal error texts - andreymal
  • one
    Most likely the error occurs due to the date field ( is_created ), because It does not have a default value or the ability to set null. Add the auto_now_add=True parameter in the Post model to the is_created field; when creating the object, it will automatically record the current date. - floydya

0