我的观点中有这个功能。在django 1.9上工作的
def post_create(request):
form = PostForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
print(form.cleaned_data.get("title"))
instance.save()
context = {
"form": form,
}
return render(request, "post_form.html", context)
url.py
urlpatterns = [
url(r'^$', views.post_list),
url(r'^create/$', views.post_create),
url(r'^(?P<id>\d+)/$', views.post_detail, name='detail'),
url(r'^update/$', views.post_update),
url(r'^delete/$', views.post_delete)
]
models.py
from django.db import models
from django.core.urlresolvers import reverse
class Post(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
updated = models.DateTimeField(auto_now=False, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("posts:detail", kwargs={"id": self.id})
但这是我在django 1.10上写的时候得到的:
/ posts / create /中的IntegrityError NOT NULL约束失败:posts_post.updated 请求方法:POST
任何想法?