0
這個特定的布爾型字段確定帖子是否爲草稿。默認設置爲True。如何自動更改布爾字段的值?
當用戶編輯草稿時,我希望將值自動更改爲False,以便將其與所有其他帖子(而不是草稿列表)一起列出。
提交按鈕是否可以提交帖子並更改布爾值?理想情況下,我認爲,一旦草稿被輸入,該字段就會被更改。
下面是相關代碼: 模板編輯草稿
{{ title }}</B></h3></div>
<div class="row">
<div class="col-sm-12-md-7 col-sm-offset-0 col-sm-8">
<div class="panel panel default">
<div class="panel-body">
<p class="well col-sm-offset-3 col-sm-12">{{ summary }}</p>
<form class="form-horizontal" method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
{% include 'posts/form-template.html' %}
<div class="form-group col-sm-offset-3 col-sm-6">
<div class="col-sm-offset-9 col-sm-10">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
這是我的形式編輯草案
class UpdateForm(forms.ModelForm):
class Meta:
model = Post
fields = [
"title", "content", "categories", "tags",
]
這是Post模型
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=100)
content = HTMLField()
draft = models.BooleanField(default=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
upvote = VotableManager()
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
categories = models.ManyToManyField(
'category.Category',
help_text='Categorize this item.'
)
tags = models.ManyToManyField(
'category.Tag',
help_text='Tag this item.'
)
suggest = models.ForeignKey('Suggest', blank=True, null=True, default=0)
def get_absolute_url(self):
return reverse('posts:detail', kwargs={'pk': self.pk})
def __str__(self):
return self.title
下面是編輯草案
def post_update(request, id=None):
instance = get_object_or_404(Post, id=id)
model = Post
form = UpdateForm(request.POST or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
context = {
"title": instance.title,
"instance": instance,
"form": form,
"summary": instance.summary,
}
return render(request, "posts/suggest_form_update.html", context)
認爲
「**下面是編輯草案**的觀點:」你的觀點? –
@PrakharTrivedi謝謝。它現在被添加。 –
什麼阻止你在視圖中自己做這件事?你有這個實例,爲什麼你不能設置draft = False? –