django-markdownx/docs-src/example.rst

80 lines
1.9 KiB
ReStructuredText
Raw Normal View History

2017-04-23 12:52:52 +00:00
Example
=======
2017-04-25 00:17:10 +00:00
Have you:
- successfully :doc:`installed MarkdownX<installation>`?
- followed the instructions on how to :doc:`get started<getting_started>`?
If so, you are set for the next step. Here you can find comprehensive examples of how to use different **MarkdownX**
features to your advantage.
2017-04-23 12:52:52 +00:00
Model
-----
2017-04-25 00:17:10 +00:00
This is how you implement a **MarkdownX** field in your models. In your :guilabel:`app/models.py`:
2017-04-23 12:52:52 +00:00
.. code-block:: python
:linenos:
from markdownx.models import MarkdownxField
class MyModel(models.Model):
myfield = MarkdownxField()
2017-04-25 00:17:10 +00:00
... and then, include the form media in the relevant template using ``{{ form.media }}``, like so:
2017-04-23 12:52:52 +00:00
.. code-block:: html
<form method="POST" action="">{% csrf_token %}
{{ form }}
</form>
{{ form.media }}
2017-04-25 00:17:10 +00:00
.. note::
The field extends Django's own TextField_ and is saved in the database accordingly.
2017-04-23 12:52:52 +00:00
Form
----
2017-04-25 00:17:10 +00:00
You can also implement **MarkdownX** through the forms. This will be done in your :guilabel:`app/forms.py` as follows:
2017-04-23 12:52:52 +00:00
.. code-block:: python
:linenos:
from markdownx.fields import MarkdownxFormField
class MyForm(forms.Form):
myfield = MarkdownxFormField()
... and then, include a form's required media in the template using ``{{ form.media }}``:
.. code-block:: html
<form method="POST" action="">{% csrf_token %}
{{ form }}
</form>
{{ form.media }}
Django Admin
------------
When using included ``MarkdowxModel`` class in your models, just use ``MarkdownxModelAdmin`` in
2017-04-25 00:17:10 +00:00
your :guilabel:`app/admin.py` as follows:
2017-04-23 12:52:52 +00:00
.. code-block:: python
:linenos:
from django.contrib import admin
from markdownx.admin import MarkdownxModelAdmin
from .models import MyModel
admin.site.register(MyModel, MarkdownxModelAdmin)
2017-04-25 00:17:10 +00:00
.. _TextField: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.TextField