Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/withlogicco/django-prose
Wonderful rich-text editing for your Django project
https://github.com/withlogicco/django-prose
django django-wysiwyg python trix wysiwyg
Last synced: 5 days ago
JSON representation
Wonderful rich-text editing for your Django project
- Host: GitHub
- URL: https://github.com/withlogicco/django-prose
- Owner: withlogicco
- License: mit
- Created: 2019-01-05T19:09:31.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-04-05T09:16:12.000Z (9 months ago)
- Last Synced: 2024-04-09T09:23:18.676Z (9 months ago)
- Topics: django, django-wysiwyg, python, trix, wysiwyg
- Language: Python
- Homepage: https://stateofprogress.blog/2023/02/01/django-prose-wonderful-rich-text-content-editing-for-django
- Size: 295 KB
- Stars: 163
- Watchers: 6
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-django - django-prose - A light weight editor to content creation. (Third-Party Packages / Editors)
README
# Django Prose
![PyPI - Downloads](https://img.shields.io/pypi/dw/django-prose?color=purple) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-prose)
Django Prose provides your Django applications with wonderful rich-text editing capabilities.
## Requirements
- Python 3.8 or later
- Django 3.2 or later
- Bleach 4.0 or later## Getting started
To get started with Django Prose, all you need to do is follow **just four steps**.
1. **Install `django-prose`**
We use and suggest using Poetry, although Pipenv and plain pip will work seamlessly as well
```console
poetry add django-prose
```2. **Add to `INSTALLED_APPS`**
Add `prose` in your Django project's installed apps (example: [`example/example/settings.py`](https://github.com/withlogicco/django-prose/blob/9e24cc794eae6db48818dd15a483d106d6a99da0/example/example/settings.py#L46)):
```python
INSTALLED_APPS = [
# Django stock apps (e.g. 'django.contrib.admin')
'prose',
# your application's apps
]
```3. **Run migrations**
This is required so you can use Django Prose's built-in Document model:
```console
python manage.py migrate prose
```4. **Include URLs**
You need to edit the main `urls.py` file of your Django project and include `prose.urls`:
```python
urlpatterns = [
path('admin/', admin.site.urls),
# other urls ...
path("prose/", include("prose.urls")),
]
```Now, you are ready to go 🚀.
## Usage
There are different ways to use Django prose according to your needs. We will examine all of them here.
### Rendering rich-text in templates
Rich text content essentially is HTML. For this reason it needs to be manually marked as [`safe`](https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#safe), when rendered in Django templates. Example:
```django
{{ document.content | safe}}
```### Small rich-text content
You might want to add rich-text content 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:
```py
from django.db import models
from prose.fields import RichTextFieldclass Article(models.Model):
excerpt = RichTextField()
```As mentioned above, you need to mark the article excerpt as `safe`, in order to render it:
```django
{{ article.excerpt | safe}}
```### Large rich-text content
In case you want to store large rich-text content, like the body 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 content in a separate database table, which is better for performance. Example:
```py
from django.db import models
from prose.fields import RichTextField
from prose.models import AbstractDocumentclass ArticleContent(AbstractDocument):
passclass Article(models.Model):
excerpt = RichTextField()
body = models.OneToOneField(ArticleContent, on_delete=models.CASCADE)
```Similarly here as well, you need to mark the article's body as `safe`, in order to render it:
```django
{{ article.body.content | safe}}
```### Forms with rich-text editing
You can create forms for your Django Prose models, to provide rich-text editing functionality. In that case, you will also need to render `form.media`, to load Trix with its stylesheets.
```django
{% csrf_token %}
{{ form.as_p }}
{{ form.media }}
Submit```
The same is true also, if you are rendering the forms field manually.
### Attachments
Django Prose can also handle uploading attachments with drag and drop. To set this up, first you need to:
- [x] Set up the `MEDIA_ROOT` and `MEDIA_URL` of your Django project (example in [`example/example/settings.py`](https://github.com/withlogicco/django-prose/blob/9e24cc794eae6db48818dd15a483d106d6a99da0/example/example/settings.py#L130-L131)))
- [x] Include the Django Prose URLs (example in [`example/example/urls.py`](https://github.com/withlogicco/django-prose/blob/9e24cc794eae6db48818dd15a483d106d6a99da0/example/example/urls.py#L13-L14))
- [x] (Optional) Set up a different Django storage to store your files (e.g. S3)- Attachments are uploaded with a path structure of `/YEAR/MONTH/DATE/UUID.EXT`
- By default, only files 5MB or less are allowed.Allowed file size can be overridden by setting `PROSE_ATTACHMENT_ALLOWED_FILE_SIZE` in your Django project's settings file.
```python
# File size in megabytes
PROSE_ATTACHMENT_ALLOWED_FILE_SIZE = 15
```### Full example
You can find a full example of a blog, built with Django Prose in the [`example`](./example/) directory.
## 🔒 A note on security
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.
For this reason Django Prose is using [Bleach](https://bleach.readthedocs.io/en/latest/) to only allow the following tags and attributes:
- **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`
- **Allowed attributes**: `alt`, `class`, `id`, `src`, `srcset`, `href`, `media`## Screenshots
### Django Prose Documents in Django Admin
![Django Prose Document in Django Admin](./docs/django-admin-prose-document.png)
## Video tutorial
If you are more of a video person, you can watch our video showcasing how to **Create a blog using Django Prose** on YouTube
[![Watch the video](https://img.youtube.com/vi/uICZjUpAbhQ/hqdefault.jpg)](https://youtu.be/uICZjUpAbhQ)
## Real world use cases
- **Remote Work Café**: Used to edit location pagess, like [Amsterdam | Remote Work Café](https://remotework.cafe/locations/amsterdam/)
- In production by multiple clients of [LOGIC](https://withlogic.co), from small companies to the public sector.If you are using Django Prose in your application too, feek free to open a [Pull Request](https://github.com/withlogicco/django-prose/pulls) to include it here. We would love to have it.
## Development for Django Prose
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 and Development Containers.
To get started run these commands in the provided order:
```console
docker compose run --rm migrate
docker compose run --rm createsuperuser
docker compose up
```If you are using Visual Studio code, just open this repository in a container using the [`Dev Containers: Open Folder in Container`](https://code.visualstudio.com/docs/devcontainers/containers#_quick-start-open-an-existing-folder-in-a-container).
---
🦄 Built with LOGIC. 🦄