gpt4 book ai didi

python - IntegrityError NOT NULL 约束失败

转载 作者:行者123 更新时间:2023-12-01 02:00:25 26 4
gpt4 key购买 nike

我正在使用 Django 构建一个简单的博客应用程序。我想实现使用表单添加新博客的功能。出现一些问题。

这是我的 models.py

from django.db import models
from django.utils import timezone
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User

class Blog(models.Model):
title=models.CharField(max_length=60)
content=models.TextField()
author=models.ForeignKey('auth.User',on_delete=models.CASCADE,)
date=models.DateTimeField(default=timezone.now)
slug=models.SlugField(null=True,unique=True)

def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Blog, self).save(*args, **kwargs)

def __str__(self):
return self.title

class UserProfile(models.Model):
user=models.OneToOneField(User)
website=models.URLField(blank=True)

def __str__(self):
return self.user.username

表单.py

 from django.template.defaultfilters import slugify
from blog.models import UserProfile
from django.contrib.auth.models import User

class BlogForm(forms.ModelForm):
title=forms.CharField(max_length=60,
help_text="blog title")
content=forms.CharField(help_text="blog content")
author=forms.CharField(help_text="blog author")
date=forms.DateTimeField(help_text="blog date")

class Meta:
model=Blog
fields=('title',)
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())

class Meta:
model=User
fields = ('username','email','password')
class UserProfileForm(forms.ModelForm):
class Meta:
model=UserProfile
fields=('website',)

views.py 中的 add_blog 方法

def add_blog(request):    
form=BlogForm()
if request.method =='POST':
form=BlogForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print(form.errors)

return render(request, 'add_blog.html',{'form':form})

当我想在网页中添加新博客时,无法输入记录。它向我展示了

IntegrityError at /add_blog/
NOT NULL constraint failed: blog_blog.author_id

有人可以帮我解决这个问题吗?非常感谢!

最佳答案

在您的模型中,您的 Blog 类需要:

  1. 标题
  2. 作者,类型为 auth.User
  3. 内容

第一步是从表单中删除作者字段:

class BlogForm(forms.ModelForm):
title=forms.CharField(max_length=60,
help_text="blog title")
content=forms.CharField(help_text="blog content")
# author=forms.CharField(help_text="blog author")
date=forms.DateTimeField(help_text="blog date")

class Meta:
model=Blog
fields=('title','content','date')

接下来,将登录用户添加为您 View 中的作者:

from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required

# makes sure this view is called with a valid user
# https://docs.djangoproject.com/en/2.0/topics/auth/default/#the-login-required-decorator
@login_required
def add_blog(request):
form = BlogForm(request.POST or {})
if form.is_valid():
temp = form.save(commit=False)
temp.author = request.user # add the logged in user, as the
# author
temp.save()
return redirect('/')
return render(request, 'add_blog.html',{'form':form})

关于python - IntegrityError NOT NULL 约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com