Debian Codegraph @

README.rst

raw
1 ==================
2 django-markupfield
3 ==================
4
5 .. image:: https://github.com/jamesturk/django-markupfield/workflows/Test/badge.svg
6
7 .. image:: https://img.shields.io/pypi/v/django-markupfield.svg
8 :target: https://pypi.python.org/pypi/django-markupfield
9
10 An implementation of a custom MarkupField for Django. A MarkupField is in
11 essence a TextField with an associated markup type. The field also caches
12 its rendered value on the assumption that disk space is cheaper than CPU
13 cycles in a web application.
14
15 Installation
16 ============
17
18 The recommended way to install django-markupfield is with
19 `pip <https://pypi.python.org/pypi/pip>`_
20
21 It is not necessary to add ``'markupfield'`` to your ``INSTALLED_APPS``, it
22 merely needs to be on your ``PYTHONPATH``. However, to use titled markup you
23 either add ``'markupfield'`` to your ``INSTALLED_APPS`` or add the
24 corresponding translations to your project translation.
25
26 Requirements
27 ------------
28
29 Requires Django >= 2.2 and 3.6+
30
31 * 1.5.x is the last release to officially support Django < 2.2 or Python 2.7
32 * 1.4.x is the last release to officially support Django < 1.11
33 * 1.3.x is the last release to officially support Django 1.4 or Python 3.3
34
35 Settings
36 ========
37
38 To best make use of MarkupField you should define the
39 ``MARKUP_FIELD_TYPES`` setting, a mapping of strings to callables that
40 'render' a markup type::
41
42 import markdown
43 from docutils.core import publish_parts
44
45 def render_rest(markup):
46 parts = publish_parts(source=markup, writer_name="html4css1")
47 return parts["fragment"]
48
49 MARKUP_FIELD_TYPES = (
50 ('markdown', markdown.markdown),
51 ('ReST', render_rest),
52 )
53
54 If you do not define a ``MARKUP_FIELD_TYPES`` then one is provided with the
55 following markup types available:
56
57 html:
58 allows HTML, potentially unsafe
59 plain:
60 plain text markup, calls urlize and replaces text with linebreaks
61 markdown:
62 default `markdown`_ renderer (only if `markdown`_ is installed)
63 restructuredtext:
64 default `ReST`_ renderer (only if `docutils`_ is installed)
65
66 It is also possible to override ``MARKUP_FIELD_TYPES`` on a per-field basis
67 by passing the ``markup_choices`` option to a ``MarkupField`` in your model
68 declaration.
69
70 .. _`ReST`: http://docutils.sourceforge.net/rst.html
71 .. _`markdown`: https://pypi.python.org/pypi/Markdown
72 .. _`docutils`: http://docutils.sourceforge.net/
73
74 Usage
75 =====
76
77 Using MarkupField is relatively easy, it can be used in any model definition::
78
79 from django.db import models
80 from markupfield.fields import MarkupField
81
82 class Article(models.Model):
83 title = models.CharField(max_length=100)
84 slug = models.SlugField(max_length=100)
85 body = MarkupField()
86
87 ``Article`` objects can then be created with any markup type defined in
88 ``MARKUP_FIELD_TYPES``::
89
90 Article.objects.create(title='some article', slug='some-article',
91 body='*fancy*', body_markup_type='markdown')
92
93 You will notice that a field named ``body_markup_type`` exists that you did
94 not declare, MarkupField actually creates two extra fields here
95 ``body_markup_type`` and ``_body_rendered``. These fields are always named
96 according to the name of the declared ``MarkupField``.
97
98 Arguments
99 ---------
100
101 ``MarkupField`` also takes three optional arguments. Either
102 ``default_markup_type`` and ``markup_type`` arguments may be specified but
103 not both.
104
105 ``default_markup_type``:
106 Set a markup_type that the field will default to if one is not specified.
107 It is still possible to edit the markup type attribute and it will appear
108 by default in ModelForms.
109
110 ``markup_type``:
111 Set markup type that the field will always use, ``editable=False`` is set
112 on the hidden field so it is not shown in ModelForms.
113
114 ``markup_choices``:
115 A replacement list of markup choices to be used in lieu of
116 ``MARKUP_FIELD_TYPES`` on a per-field basis.
117
118 ``escape_html``:
119 A flag (False by default) indicating that the input should be regarded
120 as untrusted and as such will be run through Django's ``escape`` filter.
121
122
123 Examples
124 ~~~~~~~~
125
126 ``MarkupField`` that will default to using markdown but allow the user a choice::
127
128 MarkupField(default_markup_type='markdown')
129
130 ``MarkupField`` that will use ReST and not provide a choice on forms::
131
132 MarkupField(markup_type='restructuredtext')
133
134 ``MarkupField`` that will use a custom set of renderers::
135
136 CUSTOM_RENDERERS = (
137 ('markdown', markdown.markdown),
138 ('wiki', my_wiki_render_func)
139 )
140 MarkupField(markup_choices=CUSTOM_RENDERERS)
141
142 .. note::
143 When using ``markdown``, be sure to use ``markdown.markdown`` and not
144 the ``markdown.Markdown`` class, the class requires an explicit ``reset``
145 to function properly in some cases. (See [issue #40](https://github.com/jamesturk/django-markupfield/issues/40)
146 for details.)
147
148
149 Accessing a MarkupField on a model
150 ----------------------------------
151
152 When accessing an attribute of a model that was declared as a ``MarkupField``
153 a special ``Markup`` object is returned. The ``Markup`` object has three
154 parameters:
155
156 ``raw``:
157 The unrendered markup.
158 ``markup_type``:
159 The markup type.
160 ``rendered``:
161 The rendered HTML version of ``raw``, this attribute is read-only.
162
163 This object has a ``__unicode__`` method that calls
164 ``django.utils.safestring.mark_safe`` on ``rendered`` allowing MarkupField
165 objects to appear in templates as their rendered selfs without any template
166 tag or having to access ``rendered`` directly.
167
168 Assuming the ``Article`` model above::
169
170 >>> a = Article.objects.all()[0]
171 >>> a.body.raw
172 u'*fancy*'
173 >>> a.body.markup_type
174 u'markdown'
175 >>> a.body.rendered
176 u'<p><em>fancy</em></p>'
177 >>> print unicode(a.body)
178 <p><em>fancy</em></p>
179
180 Assignment to ``a.body`` is equivalent to assignment to ``a.body.raw`` and
181 assignment to ``a.body_markup_type`` is equivalent to assignment to
182 ``a.body.markup_type``.
183
184 .. important::
185 Keeping in mind that ``body`` is MarkupField instance is particullary important with ``default`` or ``default_if_none`` filter for model that could be blank. If ``body``'s ``rendered`` is ``None`` or empty string (``""``) these filters will *not* evaluate ``body`` as falsy to display default text::
186
187 {{ a.body|default:"<missing body>" }}
188
189 That's because ``body`` is regular non-``None`` MarkupField instance. To let ``default`` or ``default_if_none`` filters to work evaluate ``rendered`` MarkupField attribute instead. To prevent escaping HTML for the case ``rendered`` is truethy, finish chain with ``safe`` filter::
190
191 {{ a.body.rendered|default:"<missing body>"|safe }}
192
193 .. note::
194 a.body.rendered is only updated when a.save() is called
195