2015-09-07 12:41:44 +00:00
# django-markdownx [](https://pypi.python.org/pypi/django-markdownx/)
[](https://pypi.python.org/pypi/django-markdownx/) [](https://pypi.python.org/pypi/django-markdownx/) [](https://pypi.python.org/pypi/django-markdownx/) [](https://pypi.python.org/pypi/django-markdownx/)
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
Django Markdownx is a markdown editor built for Django.
It is simply an extension of the Django's Textarea widget made for editing Markdown with a live preview and image uploads. It supports uploading images (stored locally in `MEDIA_ROOT` folder! yay!) with drag& drop functionality and auto tag insertion. Also, django-markdownx supports multiple editors on one page.
Template is highly customizable, so you can easily use i.e. Bootstrap to layout editor pane and preview pane side by side (as in preview animation below).
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
*Side note: Just to keep it simple, all UI editing controls are unwelcome – this is Markdown editor not a web MS Word imitation.*
2014-11-03 16:16:33 +00:00
2015-09-07 08:32:28 +00:00
##### Preview
2015-09-07 08:31:11 +00:00

*(using Bootstrap for layout and styling)*
2015-09-07 08:32:28 +00:00
##### Menu
2015-09-07 08:29:07 +00:00
* [Quick Start ](#quick-start )
* [Usage ](#usage )
* [Model ](#model )
* [Form ](#form )
* [Django Admin ](#django-admin )
* [Customization ](#customization )
* [Settings ](#settings )
* [Widget's template ](#widgets-template )
* [Dependencies ](#dependencies )
* [Changelog ](#changelog )
* [License ](#license )
* [TODO ](#todo )
----
2015-09-06 07:41:45 +00:00
# Quick Start
2014-11-01 21:02:31 +00:00
2015-09-06 18:58:04 +00:00
1. Install `django-markdownx` package.
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
```python
pip install django-markdownx
```
2014-11-01 21:02:31 +00:00
2015-09-06 18:58:04 +00:00
1. Add `markdownx` to your `INSTALLED_APPS` .
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
```python
#settings .py
INSTALLED_APPS = (
[...]
'markdownx',
2015-09-06 18:58:04 +00:00
)
2015-09-06 07:41:45 +00:00
```
2015-09-06 18:58:04 +00:00
1. Add url pattern to your `urls.py` .
2014-11-01 22:16:52 +00:00
2015-09-06 07:41:45 +00:00
```python
#urls .py
urlpatterns = [
[...]
url(r'^markdownx/', include('markdownx.urls')),
]
```
2015-09-06 18:58:04 +00:00
1. Collect included `markdownx.js` and `markdownx.css` (for django admin styling) to your `STATIC_ROOT` folder.
2015-09-06 07:41:45 +00:00
```python
python manage.py collectstatic
```
2015-09-06 18:58:04 +00:00
1. ...and don't forget to include jQuery in your html file.
2015-09-06 07:41:45 +00:00
```html
< head >
[...]
< script src = "//code.jquery.com/jquery-2.1.1.min.js" > < / script >
< / head >
```
# Usage
2015-09-07 08:29:07 +00:00
## Model
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
```python
#models.py
from markdownx.models import MarkdownxField
class MyModel(models.Model):
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
myfield = MarkdownxField()
```
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
...and then, include a form's required media in the template using `{{ form.media }}` :
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
```html
< form method = "POST" action = "" > {% csrf_token %}
{{ form }}
< / form >
{{ form.media }}
```
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
## Form
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
```python
#forms.py
from markdownx.fields import MarkdownxFormField
class MyForm(forms.Form):
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
myfield = MarkdownxFormField()
```
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
...and then, include a form's required media in the template using `{{ form.media }}` :
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
```html
< form method = "POST" action = "" > {% csrf_token %}
{{ form }}
< / form >
{{ form.media }}
```
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
## Django Admin
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
When using included `MarkdowxModel` class in your models, just use `MarkdownxModelAdmin` as follows:
2015-09-06 21:13:50 +00:00
2015-09-07 08:29:07 +00:00
```python
#admin.py
from django.contrib import admin
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
from markdownx.admin import MarkdownxModelAdmin
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
from .models import MyModel
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
admin.site.register(MyModel, MarkdownxModelAdmin)
```
2015-09-06 07:41:45 +00:00
2015-09-07 08:29:07 +00:00
However, when you want to use `markdownx` with other classes – lets say `TextField` – than override default widget as below:
2015-09-06 21:13:50 +00:00
2015-09-07 08:29:07 +00:00
```python
#admin.py
from django.db import models
from django.contrib import admin
2015-09-06 21:13:50 +00:00
2015-09-07 08:29:07 +00:00
from markdownx.widgets import AdminMarkdownxWidget
2015-09-06 21:13:50 +00:00
2015-09-07 08:29:07 +00:00
from .models import MyModel
2015-09-06 21:13:50 +00:00
2015-09-07 08:29:07 +00:00
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': AdminMarkdownxWidget},
}
2015-09-06 21:13:50 +00:00
2015-09-07 08:29:07 +00:00
admin.site.register(MyModel, MyModelAdmin)
```
2015-09-06 21:13:50 +00:00
2015-09-06 07:41:45 +00:00
2014-12-10 22:19:57 +00:00
# Customization
## Settings
2014-11-01 21:02:31 +00:00
2015-09-06 18:58:04 +00:00
Place settings in your `settings.py` to override default values:
2014-11-02 19:44:46 +00:00
2014-11-01 22:09:46 +00:00
```python
2014-11-01 23:20:50 +00:00
#settings.py
2015-09-06 07:41:45 +00:00
MARKDOWNX_MARKDOWN_EXTENSIONS = []
2014-11-01 22:09:46 +00:00
MARKDOWNX_MEDIA_PATH = 'markdownx/' # subdirectory, where images will be stored in MEDIA_ROOT folder
2015-09-06 07:41:45 +00:00
MARKDOWNX_UPLOAD_MAX_SIZE = 52428800 # 50MB
MARKDOWNX_UPLOAD_CONTENT_TYPES = ['image/jpeg', 'image/png']
MARKDOWNX_IMAGE_MAX_SIZE = {'size': (500, 500), 'quality': 90,}
MARKDOWNX_EDITOR_RESIZABLE = True # update editor's height to inner content height while typing
2014-11-01 22:09:46 +00:00
```
2014-11-01 21:02:31 +00:00
2015-09-06 18:58:04 +00:00
**NOTE:** `MARKDOWNX_IMAGE_MAX_SIZE` dict properties:
2014-11-01 21:02:31 +00:00
2014-11-02 19:44:46 +00:00
* **size** – (width, height). When `0` used, i.e.: (500,0), property will figure out proper height by itself
2015-09-06 18:58:04 +00:00
* **quality** – default: `90` – image quality, from `0` (full compression) to `100` (no compression)
2014-11-01 21:02:31 +00:00
* **crop** – default: `False` – if `True` use `size` to crop final image
2014-11-02 19:44:46 +00:00
* **upscale** – default: `False` – if image dimensions are smaller than those in `size` , upscale image to `size` dimensions
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
## Widget's template
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
Default widget's template looks like:
2014-11-01 21:02:31 +00:00
2014-11-01 22:09:46 +00:00
```html
2015-09-06 07:41:45 +00:00
< div class = "markdownx" >
2014-11-02 20:10:09 +00:00
{{ markdownx_editor }}
2015-09-06 07:41:45 +00:00
< div class = "markdownx-preview" > < / div >
2014-11-01 22:09:46 +00:00
< / div >
```
2015-09-06 07:41:45 +00:00
2015-09-06 18:58:04 +00:00
When you want to use Bootstrap 3 and side-by-side panes (as in preview image above), just place `templates/markdownx/widget.html` file in your project with:
2014-11-01 21:02:31 +00:00
2014-11-01 22:09:46 +00:00
```html
2015-09-06 07:41:45 +00:00
< div class = "markdownx row" >
< div class = "col-md-6" >
2014-11-02 20:10:09 +00:00
{{ markdownx_editor }}
2014-11-01 22:09:46 +00:00
< / div >
2015-09-06 07:41:45 +00:00
< div class = "col-md-6" >
< div class = "markdownx-preview" > < / div >
2014-11-01 22:09:46 +00:00
< / div >
< / div >
```
2014-11-01 21:02:31 +00:00
2014-11-01 23:08:20 +00:00
# Dependencies
2015-09-06 07:41:45 +00:00
* Markdown
* Pillow
* jQuery
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
# Changelog
2014-11-01 21:02:31 +00:00
2015-09-06 21:13:50 +00:00
###### v1.0.1
* Moved html logic from FormField to Widget to be able to override model objects other than included MarkdownxModel
* Fixed default value for `MARKDOWNX_EDITOR_RESIZABLE`
###### v1.0.0
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
* Warning: no backward compatibility
* Admin, Model and Form custom objects
* Django admin styles for compiled markdown
* Settings variables changed:
* MARKDOWNX_MAX_SIZE => MARKDOWNX_IMAGE_MAX_SIZE
* MARKDOWNX_MARKDOWN_KWARGS => MARKDOWNX_MARKDOWN_EXTENSIONS
* MARKDOWNX_MAX_UPLOADSIZE => MARKDOWNX_UPLOAD_MAX_SIZE
* MARKDOWNX_CONTENT_TYPES => MARKDOWNX_UPLOAD_CONTENT_TYPES
2014-11-01 21:02:31 +00:00
2015-09-06 07:41:45 +00:00
###### v0.4.2
2014-12-10 21:25:09 +00:00
* Path fix by argaen
2015-09-06 07:41:45 +00:00
###### v0.4.1
2014-12-03 19:32:27 +00:00
* Better editor height updates
* Refresh preview on image upload
* Small JS code fixes
2015-09-06 07:41:45 +00:00
###### v0.4.0
2014-11-14 11:29:37 +00:00
* editor auto height
2015-09-06 07:41:45 +00:00
###### v0.3.1
2014-11-14 10:11:36 +00:00
* JS event fix
2015-09-06 07:41:45 +00:00
###### v0.3.0
2014-11-13 09:40:51 +00:00
* version bump
2015-09-06 07:41:45 +00:00
###### v0.2.9
2014-11-12 15:04:21 +00:00
* Removed any inlcuded css
* Removed JS markdown compiler (full python support now with Markdown lib)
2015-09-06 07:41:45 +00:00
###### v0.2.0
2014-11-05 22:11:33 +00:00
* Allow to paste tabs using Tab button
2015-09-06 07:41:45 +00:00
###### v0.1.4
2014-11-03 10:04:22 +00:00
* package data fix
2015-09-06 07:41:45 +00:00
###### v0.1.3
2014-11-03 09:31:41 +00:00
* README.md fix on PyPi
2015-09-06 07:41:45 +00:00
###### v0.1.2
2014-11-03 09:14:41 +00:00
* critical setuptools fix
2015-09-06 07:41:45 +00:00
###### v0.1.1
2014-11-02 20:10:09 +00:00
* change context name `editor` to `markdownx_editor` for better consistency
2015-09-06 07:41:45 +00:00
###### v0.1.0
2014-11-01 21:02:31 +00:00
* init
2014-11-01 23:08:20 +00:00
2015-09-06 07:41:45 +00:00
# License
django-markdown is licensed under the open source BSD license
# TODO
* python 3 compatibility
* tests
Would be nice to have some help with those!
2014-11-01 23:08:20 +00:00
# Notes
2015-09-06 21:13:50 +00:00
**django-markdownx** was inspired by great [django-images ](https://github.com/mirumee/django-images ) and [django-bootstrap-markdown ](http://thegoods.aj7may.com/django-bootstrap-markdown/ ) packages.