|
1 | 1 | # Django Prose |
2 | 2 |
|
3 | | -Wonderful rich-text editing in Django. |
| 3 | +  |
4 | 4 |
|
5 | | -## Install |
| 5 | +Django Prose provides your Django applications with wonderful rich-text editing. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- Python 3.6.2 or later |
| 10 | +- Django 3.0 or later |
| 11 | +- Bleach 4.0 or later |
| 12 | + |
| 13 | +## Getting started |
| 14 | + |
| 15 | +To get started with Django Prose, first make sure to install it. We use and suggest using Poetry, although Pipenv and pip will work seamlessly as well |
6 | 16 |
|
7 | 17 | ```console |
8 | | -pip install django-prose |
| 18 | +poetry add django-prose |
| 19 | +``` |
| 20 | + |
| 21 | +Then, add `prose` in Django's installed apps (example: [`prose_example/prose_example/settings.py`](https://github.com/withlogicco/django-prose/blob/55fb9319e55d873afe43968817a2f5ea3f055d11/prose_example/prose_example/settings.py#L46)): |
| 22 | + |
| 23 | +```python |
| 24 | +INSTALLED_APPS = [ |
| 25 | + # Django stock apps (e.g. 'django.contrib.admin') |
| 26 | + |
| 27 | + 'prose', |
| 28 | + |
| 29 | + # your application's apps |
| 30 | +] |
| 31 | +``` |
| 32 | + |
| 33 | +Last, run migrations so you can use Django Prose's Document model: |
| 34 | + |
| 35 | +``` |
| 36 | +python manage.py migrate prose |
| 37 | +``` |
| 38 | + |
| 39 | +Now, you are ready to go 🚀. |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +There are different ways to use Django prose according to your needs. We will examine all of them here. |
| 44 | + |
| 45 | +### Small rich-text information |
| 46 | + |
| 47 | +You might want to add rich-text information in a model that is just a few characters (e.g. 140), like an excerpt from an article. In that case we suggest using the `RichTextField`. Example: |
| 48 | + |
| 49 | +```py |
| 50 | +from django.db import models |
| 51 | +from prose.fields import RichTextField |
| 52 | + |
| 53 | +class Article(models.Model): |
| 54 | + excerpt = RichTextField() |
| 55 | +``` |
| 56 | + |
| 57 | +Then you can display the article excerpt in your HTML templates by marking it as [`safe`](https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#safe) |
| 58 | + |
| 59 | +```django |
| 60 | +<div class="article-excerpt">{{ article.excerpt | safe}}</div> |
9 | 61 | ``` |
10 | 62 |
|
| 63 | +### Large rich-text information |
| 64 | + |
| 65 | +In case you want to store large rich-text information, like the content of an article, which can span to quite a few thousand characters, we suggest you use the `AbstractDocument` model. This will save large rich-text information in a separate database table, which is better for performance. Example: |
| 66 | + |
| 67 | +```py |
| 68 | +from django.db import models |
| 69 | +from prose.fields import RichTextField |
| 70 | +from prose.models import AbstractDocument |
| 71 | + |
| 72 | +class ArticleContent(AbstractDocument): |
| 73 | + pass |
| 74 | + |
| 75 | +class Article(models.Model): |
| 76 | + excerpt = RichTextField() |
| 77 | + body = models.OneToOneField(ArticleContent, on_delete=models.CASCADE) |
| 78 | +``` |
| 79 | + |
| 80 | +Similarly here you can display the article's body by marking it as `safe` |
| 81 | + |
| 82 | +```django |
| 83 | +<div class="article-body">{{ article.body.content | safe}}</div> |
| 84 | +``` |
| 85 | + |
| 86 | +### Attachments |
| 87 | + |
| 88 | +Django Prose can also handle uploading attachments with drag and drop. To set this up, first you need to: |
| 89 | + |
| 90 | +- [x] Set up the `MEDIA_ROOT` of your Django project (example in [`prose_example/prose_example/settings.py`](https://github.com/withlogicco/django-prose/blob/55fb9319e55d873afe43968817a2f5ea3f055d11/prose_example/prose_example/settings.py#L132))) |
| 91 | +- [x] Include the Django Prose URLs (example in [`prose_example/prose_example/urls.py`](https://github.com/withlogicco/django-prose/blob/9073d713f8d3febe5c50705976dbb31063270886/prose_example/prose_example/urls.py#L9-L10)) |
| 92 | +- [x] (Optional) Set up a different Django storage to store your files (e.g. S3) |
| 93 | + |
| 94 | +## 🔒 A note on security |
| 95 | + |
| 96 | +As you can see in the examples above, what Django Prose does is provide you with a user friendly editor ([Trix](https://trix-editor.org/)) for your rich text content and then store it as HTML in your database. Since you will mark this HTML as safe in order to use it in your templates, it needs to be **sanitised**, before it gets stored in the database. |
| 97 | + |
| 98 | +For this reason Django Prose is using [Bleach](https://bleach.readthedocs.io/en/latest/) to only allow the following tags and attributes: |
| 99 | + |
| 100 | +- **Allowed tags**: `p`, `ul`, `ol`, `li`, `strong`, `em`, `div`, `span`, `a`, `blockquote`, `pre`, `figure`, `figcaption`, `br`, `code`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `picture`, `source`, `img` |
| 101 | +- **Allowed attributes**: `alt`, `class`, `id`, `src`, `srcset`, `href`, `media` |
| 102 | + |
| 103 | +## Screenshots |
| 104 | + |
| 105 | +### Django Prose Documents in Django Admin |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +## Development for Django Prose |
| 111 | + |
| 112 | +If you plan to contribute code to Django Prose, this section is for you. All development tooling for Django Prose has been set up with Docker. To get started run these commands in the provided order: |
| 113 | + |
| 114 | +```console |
| 115 | +docker compose run --rm migrate |
| 116 | +docker compose run --rm createsuperuser |
| 117 | +docker compose up |
| 118 | +``` |
11 | 119 |
|
12 | 120 | --- |
13 | 121 |
|
14 | 122 | <p align="center"> |
15 | | - <i>Built with ❤️ in Athens, Greece.</i> |
| 123 | + <i>🦄 Built with ❤️ by <a href="https://withlogic.co/">LOGIC</a>. 🦄</i> |
16 | 124 | </p> |
0 commit comments