文章发表和发布草稿箱,发布后都是进草稿
来源:7-5 用户发表文章与保存草稿
归来也无风雨也无晴
2021-08-27
$(function () {
$(".publish").click(function () {
$("input[name='status']").val("P");
console.log('P', '-------', $('input[name="status"]').val());
$("#article-form").submit();
});
$(".update").click(function () {
$("input[name='status']").val("P");
//$("input[name='edited']").prop("checked");
$("input[name='edited']").val("True");
$("#article-form").submit();
});
$(".draft").click(function () {
$("input[name='status']").val("D");
console.log('D', '-------', $('input[name="status"]').val());
$("#article-form").submit();
});
});
console打印出来是:
我改了前端:
$("input[name='status']").val("P");
$("#article-form").append('<input type="hidden" name="status" value="P" /> ');
console.log('P', '-------', $('input[name="status"]').val());
$("#article-form").submit();
现在前端中确定能发送过去status了。
console能打印出来。
但还是发表草稿箱。
class CreateArticleView(LoginRequiredMixin, CreateView):
"""创建文章"""
model = Article
message = "您的文章已创建成功!" # Django框架中的消息机制
form_class = ArticleForm
template_name = 'articles/article_create.html'
def form_valid(self, form):
form.instance.user = self.request.user
return super(CreateArticleView, self).form_valid(form)
def get_success_url(self):
"""创建成功后跳转"""
messages.success(self.request, self.message) # 消息传递给下一次请求
return reverse('articles:list')
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ['title', 'content', 'image', 'tags']
```@python_2_unicode_compatible
class Article(models.Model):
STATUS = (("D", "Draft"), ("P", "Published"))
title = models.CharField(max_length=255, null=False, unique=True, verbose_name='标题')
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, related_name="author", on_delete=models.SET_NULL,
verbose_name='作者')
image = models.ImageField(upload_to='articles_pictures/%Y/%m/%d/', verbose_name='文章图片')
slug = models.SlugField(max_length=80, null=True, blank=True, verbose_name='(URL)别名')
status = models.CharField(max_length=1, choices=STATUS, default='D', verbose_name='状态') # 默认存入草稿箱
content = models.TextField(verbose_name='内容')
edited = models.BooleanField(default=False, verbose_name='是否可编辑')
tags = TaggableManager(help_text='多个标签使用,(英文)隔开', verbose_name='标签')
created_at = models.DateTimeField(db_index=True, auto_now_add=True, verbose_name='创建时间')
updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
objects = ArticleQuerySet.as_manager()
class Meta:
verbose_name = '文章'
verbose_name_plural = verbose_name
ordering = ("created_at",)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
# 根据作者和标题生成文章在URL中的别名
self.slug = slugify(self.title)
super(Article, self).save(*args, **kwargs)
写回答
1回答
-
归来也无风雨也无晴
提问者
2021-08-28
解决l012021-09-03
相似问题