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.
form.errorsare normal error texts - andreymalis_created), because It does not have a default value or the ability to set null. Add theauto_now_add=Trueparameter in the Post model to theis_createdfield; when creating the object, it will automatically record the current date. - floydya