大佬在吗?我这个 为什么没有预览标签?

来源:7-6 实现Markdown编辑与实时预览

yu444913387

2020-01-25

图片描述

FROM_RENDERER = 'django.forms.renderers.TemplatesSetting'

DJANGO_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',  # 友好的标签模板
    'django.forms',
]
THIRD_PARTY_APPS = [
    'crispy_forms',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.github',
    'sorl.thumbnail',
    'taggit',
    'markdownx'

]```
from django import forms
from markdownx.fields import MarkdownxFormField

from zanhu.articles.models import Article


class ArticleForm(forms.ModelForm):
    content = MarkdownxFormField()

    class Meta:
        model = Article
        fields = ['title', 'content', 'image', 'tags']

Create your models here.

coding:utf-8

from future import unicode_literals

from django.conf import settings
from django.db import models
from markdownx.models import MarkdownxField
from markdownx.utils import markdownify
from slugify import slugify
from taggit.managers import TaggableManager

class ArticleQuerySet(models.query.QuerySet):
def get_published(self):
return self.filter(status=“P”)

def get_drafts(self):
    return self.filter(status="D")

def get_counted_tags(self):
    tag_dict = {}
    query = self.get_published().annotate(tagged=models.Count('tags')).filter(tags__gt=0)
    for obj in query:
        for tag in obj.tags.names():
            if tag not in tag_dict:
                tag_dict[tag] = 1
            else:
                tag_dict[tag] += 1

        return tag_dict.items()

class Article(models.Model):
    STATUS = (("D", "Draft"), ("P", "Published"))
    title = models.CharField(max_length=255, unique=True, verbose_name='标题')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
                             blank=True, on_delete=models.SET_NULL,
                             related_name='author', verbose_name='作者')
    image = models.ImageField(upload_to='articles_pictures/%Y/%m/%d/',
                              verbose_name='文章图片')
    slug = models.SlugField(max_length=255, verbose_name='(URL)别名')
    status = models.CharField(max_length=1, choices=STATUS, default="D",
                              verbose_name="状态")
    content = MarkdownxField(verbose_name='内容')
    edited = models.BooleanField(default=False, verbose_name="是否可编辑")
    tags = TaggableManager(help_text="多个标签使用,(英文)隔开", verbose_name="标签")
    created_at = models.DateTimeField(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, force_insert=False, force_update=False, using=None, update_fields=None):
        self.slug = slugify(self.title)
        super(Article, self).save()

    def get_markdown(self):
        return markdownify(self.content)

urlpatterns = [
                  path('', NewsListView.as_view(), name='home'),
                  # 用户管理
                  path('users/', include('users.urls', namespace='users')),
                  path('accounts/', include('allauth.urls')),

                  path('markdownx/', include('markdownx.urls')),

                  path('news/', include('news.urls', namespace='news')),
                  path('articles/', include('articles.urls', namespace='articles'))
              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
写回答

1回答

Jack

2020-01-25

0
1
yu444913387
非常感谢!
2020-01-28
共1条回复

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程