编辑1
Models.py
class Post(models.Model):
category = models.ForeignKey(Category)
pub_date = models.DateTimeField(auto_now_add = True)
title = models.CharField(max_length = 100)
content = FroalaField()
url = models.URLField(max_length=250, blank=True, null=True)
moderator = models.ForeignKey(User)
rank_score = models.FloatField(default= 1)
views = models.IntegerField(default=0)
image = models.ImageField(upload_to="images",blank=True, null=True)
slug = models.CharField(max_length=100, unique=True)
objects = models.Manager() # default manager
@property
def domain(self):
return urlparse(urlsplit(self.url)).netloc if self.url else "a"
这里; s我的forms.py
class PostForm(forms.ModelForm):
title = forms.CharField(max_length=128, help_text="plz enter")
url = forms.URLField(max_length=200,
help_text="Please enter the URL of the page.")
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
content = forms.CharField(widget=FroalaEditor)
class Meta:
model = Post
exclude = ['pub_date', 'moderator', 'rank_score', 'image','slug']
我从这里得到无效的语法错误:return urlparse(urlsplit(self.url))。netloc if self.url else" a"
答案 0 :(得分:0)
尝试更改:
url = forms.URLField(max_length=200,
help_text="Please enter the URL of the page.")
为:
url = forms.URLField(max_length=200,
help_text="Please enter the URL of the page.",
required=False)
答案 1 :(得分:0)
您正在使用ModelForm,因此不必覆盖表单中的url字段。
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'url', 'views', 'content', 'moderator', 'slug', 'objects')
widgets = {
'content': FroalaEditor(), # Not sure if this widget is valid
'views':forms.HiddenInput()
}
您还可以将帮助文本添加到模型中(请参阅help_text
docs)