我最初拥有Django模型的标准默认fls <- list.files(path = ".", pattern = NULL, include.dirs = FALSE)
fls[!file.info(fls)$isdir]
字段。但是我想切换到随机的10-15字母数字字符串。
如果你很好奇(它有效),这是字符串:
id
这是我的模特。我只是在现场添加并def random_string(chars=string.ascii_letters + string.digits):
size = random.randrange(10, 15)
return str(''.join(random.choice(chars) for _ in range(size)))
&amp; migrate
:
makemigrations
预计它不会顺利转换 - 当我发帖时,我得到以下class Post(models.Model):
id = models.CharField(max_length=18, primary_key=True, default=random_string())
user = models.ForeignKey(User, blank=True, null=True)
content = models.TextField(null=True, blank=True)
:
non_field_error
视图
Non Errors: <bound method BaseForm.non_field_errors of <PostForm bound=False, valid=False, fields=(content;id;user)>>
[20/Mar/2018 09:38:37] "POST /post/ HTTP/1.1" 200 11482
任何人都可以告诉我为什么我得到def post(request):
if request.user.is_authenticated():
form_post = PostForm(request.POST or None, request.FILES or None)
if form_post.is_valid():
instance = form_post.save(commit=False)
instance.user = request.user
instance.save()
return HttpResponseRedirect('/')
else:
form_post = PostForm()
print('Errors:', form_post.errors) #prints nothing
print('Non Errors:', form_post.non_field_errors)
context = {
'form_post': form_post,
}
return render(request, 'post/post.html', context)
else:
return HttpResponseRedirect("/accounts/signup/")
?以下是该模型的附带表格:
non_field_error
答案 0 :(得分:1)
non_field_errors
是一种方法,而非errors
的常规属性:
print('Non Errors:', form_post.non_field_errors())