我有以下 CreateView
class CreateEmailTemplateView(CreateView):
template_name = 'frontend/emailtemplates/create.html'
model = Templates
fields = '__all__'
def form_valid(self, form):
form.instance.user = self.request.user
return super(CreateEmailTemplateView, self).form_valid(form)
模板模型如下
class Templates(models.Model):
name = models.CharField(max_length=128)
template = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
但是当我提交表单时,它并没有保存在数据库中,也没有看到任何错误消息,它只是重定向到同一页面,表单方法是POST
< / p>
我想念什么?提交表单后如何显示一些错误/成功消息?
答案 0 :(得分:1)
form_valid
在发布有效表单数据后被调用。
您能否检查您的数据是否有效?
您还可以将日志记录添加到form_valid
方法中,以确保达到该目的:
import logging
logger = logging.getLogger(__name__)
class CreateEmailTemplateView(CreateView):
template_name = 'frontend/emailtemplates/create.html'
model = Templates
fields = '__all__'
def form_valid(self, form):
logger.info('form_valid called')
form.instance.user = self.request.user
return super(CreateEmailTemplateView, self).form_valid(form)