Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何避免django integrityerror更新现有博客文章_Python_Django - Fatal编程技术网

Python 如何避免django integrityerror更新现有博客文章

Python 如何避免django integrityerror更新现有博客文章,python,django,Python,Django,我在我的froms.py中使用了这个干净的方法,以防止用户使用我的网站中已经存在的重复标题或标题创建博客文章。但是当我要更新我的旧博文时,我也得到了这个积分错误。看到图片了吗 如何避免更新旧博客文章的完整性错误。这是我的密码: #froms.py from .models import * from django import forms from django.core.exceptions import ValidationError from django.db import Integ

我在我的froms.py中使用了这个干净的方法,以防止用户使用我的网站中已经存在的重复标题或标题创建博客文章。但是当我要更新我的旧博文时,我也得到了这个积分错误。看到图片了吗

如何避免更新旧博客文章的完整性错误。这是我的密码:

#froms.py

from .models import *
from django import forms
from django.core.exceptions import ValidationError
from django.db import IntegrityError

class BlogPost(forms.ModelForm):
    def clean_title(self):
             title = self.cleaned_data['title']
             if IntegrityError(title):
                 raise forms.ValidationError('This title already exists. Please use different title')
             return title
          
    class Meta:
      model = Post
      fields = ['title','author','body']
      
      widgets = {
         'title': forms.TextInput(attrs={'class':'form-control'}),
         'author': forms.Select(attrs={'class':'form-control'}),
         'body': RichTextField(),
      }
  
class blog_update_view(UpdateView):
      model = Post
      template_name = "blog_update_post.html"
      form_class = BlogPost
  
       
       
视图.py

from .models import *
from django import forms
from django.core.exceptions import ValidationError
from django.db import IntegrityError

class BlogPost(forms.ModelForm):
    def clean_title(self):
             title = self.cleaned_data['title']
             if IntegrityError(title):
                 raise forms.ValidationError('This title already exists. Please use different title')
             return title
          
    class Meta:
      model = Post
      fields = ['title','author','body']
      
      widgets = {
         'title': forms.TextInput(attrs={'class':'form-control'}),
         'author': forms.Select(attrs={'class':'form-control'}),
         'body': RichTextField(),
      }
  
class blog_update_view(UpdateView):
      model = Post
      template_name = "blog_update_post.html"
      form_class = BlogPost
  
       
       

我不想用slagify。只是想避免更新现有博客帖子的integrityerror。

请添加相关模型。您真的需要
if IntegrityError(title)
条件吗?我只是不太清楚原因。