请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

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

图片描述

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 23:41:22

模板里面要这么写

https://img1.sycdn.imooc.com//szimg/5e2c610209ad045d15400730.jpg

按照官方文档的例子,https://neutronx.github.io/django-markdownx/example/

我当时用的2.0.28版本,可能有bug,你现在换成最新的3.0.1吧

1 回复 有任何疑惑可以回复我~
  • 提问者 yu444913387 #1
    大佬,是我的错,我装了3.0.1有bug,我装错版本了,我装回2.0.28,然后bug没有了。
    回复 有任何疑惑可以回复我~ 2020-01-26 14:53:02
  • 慕仔4607872 回复 提问者 yu444913387 #2
    把widget2.html的2去掉就可以了,我也以为是BUG后来发现了
    回复 有任何疑惑可以回复我~ 2020-03-19 19:42:06
  • Jack 回复 慕仔4607872 #3
    还真是,谢谢指正!
    回复 有任何疑惑可以回复我~ 2020-03-21 11:18:10
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信