我无法理解django-markdownx的用法 [英] I can't understand the django-markdownx's usage

查看:51
本文介绍了我无法理解django-markdownx的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点尴尬.但我似乎无法理解 django-markdownx 关于如何使用的文档该应用程序.我遵循了入门指南,安装了应用程序和依赖项,添加了jquery,它可在Admin后端中使用.但是模板不会将 text = MarkdownxField()呈现为格式正确的markdown,而是纯文本.

It is kind of embarrassing. But I can't seems to understand django-markdownx's documentation on how to use the app. I've followed the Getting Started guide, installed the app and dependencies, added jquery and it works in the Admin backend. But the template doesn't render text = MarkdownxField() as correctly formatted markdown, but as plain text.

我不了解该部分……然后,使用 {{form.media}} 将模板所需的媒体包括在模板中:

I don't understand the part ...and then, include a form's required media in the template using {{ form.media }}:

<form method="POST" action="">{% csrf_token %}
    {{ form }}
</form>
{{ form.media }}

我试图在模板中的商品标签之前添加该代码.

I tried to add that code just before the article tag in my template.

{% extends "base.html" %}
{% block content %}
    <form method="POST" action="">{% csrf_token %}
        {{ form }}
    </form>
    {{ form.media }}
    <article>
        <h1>{{ article.title }}</h1>
        <p>{{ article.text }}</p>
        <div>{{ article.pub_date }} {{ article.category }} {{ article.tag }}</div>
    </article>
{% endblock %}

但是它不能解决问题.

我想念的是什么?我知道这很简单.但是我没有表格方面的经验.

What I'm I missing? I know it is trivial. But I have no experience with forms.

app/models.py

app/models.py

from django.db import models
from django.urls import reverse
from markdownx.models import MarkdownxField

class Article(models.Model):
    title = models.CharField(max_length=250, verbose_name='title')
    text = MarkdownxField()
    pub_date = models.DateField(verbose_name='udgivelsesdato')
    category = models.ForeignKey(Category, verbose_name='kategori', null=True)
    tag = models.ForeignKey(Tag, verbose_name='mærke', null=True)

    def get_absolute_url(self):
        return reverse('article-detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.title

    class Meta():
        verbose_name = 'artikel'
        verbose_name_plural = 'artikler'

settings.py

settings.py

INSTALLED_APPS = [
    ***
    'markdownx',
    'articles',
]

# Markdown extensions
MARKDOWNX_MARKDOWN_EXTENSIONS = [
    'markdown.extensions.sane_lists',
    'markdown.extensions.nl2br',
    'markdown.extensions.extra',
]

urls.conf

urls.conf

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^', include('articles.urls')),
    url(r'^markdownx/', include('markdownx.urls')),
    url(r'^admin/', admin.site.urls),
]

app/urls.py

app/urls.py

from django.conf.urls import url
from django.views.generic.dates import ArchiveIndexView

from articles.models import Article
from articles.views import ArticleDetailView, ArticleListView

urlpatterns = [
    url(r'^arkiv/$',
        ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
        name="article_archive"),
    url(r'^$', ArticleListView.as_view(), name='article_list'),
    url(r'(?P<pk>\d+)/$', ArticleDetailView.as_view(), name='article_detail'),
]

app/templates/app/article_list.html

app/templates/app/article_list.html

{% extends "base.html" %}
{% block content %}
    <article>
    <h1>{{ article.title }}</h1>
    <p>{{ article.text }}</p>
    <div>{{ article.pub_date }}</div>
    </article>
{% endblock %}

app/views.py

app/views.py

from django.views.generic import ListView, DetailView

from articles.models import Article


class ArticleListView(ListView):
    model = Article


class ArticleDetailView(DetailView):
    model = Article

推荐答案

好的,我现在看到您的问题了(感谢您处理我的问题:)).django-markdownx使您能够在表单内部具有Markdown编辑器.但是,当在模板中显示时,它不会格式化标记的格式-只是纯文本.

Ok, I see your issue now (thank you for handling my questions:) ). django-markdownx provides you with the ability to have a Markdown editor inside of your forms. It does not, however, format that markdown when shown in a template - it's just plain text.

根据项目GitHub上的此问题,您需要渲染在您的视图中降价,然后将其传递给模板.这样做的另一种方式,如果我在项目中使用它,我会更喜欢:

According to this issue on the project's GitHub you need to render the markdown in your views and then pass that to your template. Another way of doing this, which I would prefer if I were using this in a project:

from markdownx.utils import markdownify

class Article(models.Model):
    title = models.CharField(max_length=250, verbose_name='title')
    text = MarkdownxField()
    pub_date = models.DateField(verbose_name='udgivelsesdato')
    category = models.ForeignKey(Category, verbose_name='kategori', null=True)
    tag = models.ForeignKey(Tag, verbose_name='mærke', null=True)

    # Create a property that returns the markdown instead
    @property
    def formatted_markdown(self):
        return markdownify(self.text)

    def get_absolute_url(self):
        return reverse('article-detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.title

    class Meta():
        verbose_name = 'artikel'
        verbose_name_plural = 'artikler'

然后在您的模板中:

{% extends "base.html" %}
{% block content %}
    <article>
    <h1>{{ article.title }}</h1>
    <p>{{ article.formatted_markdown|safe }}</p>
    <div>{{ article.pub_date }}</div>
    </article>
{% endblock %}

这篇关于我无法理解django-markdownx的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆