A Django app that allows the easy addition of Stack Overflow's 'PageDown' markdown editor to a django form field
Project description
Django Pagedown
Add Stack Overflow's "Pagedown" Markdown editor to your Django Admin or custom form.
Requirements
Version >= 2.0.0 of django-pagedown
requires Django 2.1.0 or above (previous versions should support Django all the way back to around 1.1).
Installation
- Get the code:
pip install django-pagedown
- Add
pagedown.apps.PagedownConfig
to yourINSTALLED_APPS
- Collect the static files:
python manage.py collectstatic
Usage
The widget can be used both inside the django admin or independendly.
Inside the Django Admin:
If you want to use the pagedown editor in a django admin field, there are numerous possible approaches:
-
To use it in all
TextField
's in your admin form:from django.contrib import admin from django.db import models from pagedown.widgets import AdminPagedownWidget class AlbumAdmin(admin.ModelAdmin): formfield_overrides = { models.TextField: {'widget': AdminPagedownWidget }, }
-
To only use it on particular fields, first create a form (in
forms.py
):from django import forms from pagedown.widgets import AdminPagedownWidget from music.models import Album class AlbumForm(forms.ModelForm): name = forms.CharField(widget=AdminPagedownWidget()) description = forms.CharField(widget=AdminPagedownWidget()) class Meta: model = Album fields = "__all__"
and in your
admin.py
:from django.contrib import admin from forms import FooModelForm from models import FooModel @admin.register(FooModel) class FooModelAdmin(admin.ModelAdmin): form = FooModelForm fields = "__all__"
Outside the Django Admin:
To use the widget outside of the django admin, first create a form similar to the above but using the basic PagedownWidget
:
from django import forms
from pagedown.widgets import PagedownWidget
from music.models import Album
class AlbumForm(forms.ModelForm):
name = forms.CharField(widget=PagedownWidget())
description = forms.CharField(widget=PagedownWidget())
class Meta:
model = Album
fields = ["name", "description"]
Then define your urls/views:
from django.views.generic import FormView
from django.conf.urls import patterns, url
from music.forms import AlbumForm
urlpatterns = patterns('',
url(r'^$', FormView.as_view(template_name="baz.html",
form_class=AlbumForm)),)
then create the template and load the javascipt and css required to create the editor:
<html>
<head>
{{ form.media }}
</head>
<body>
<form ...>
{{ form }}
</form>
</body>
</html>
Customizing the Widget
If you want to customize the widget, the easiest way is to simply extend it:
from pagedown.widgets import PagedownWidget
class MyNewWidget(PagedownWidget):
template_name = '/custom/template.html'
class Media:
css = {
'all': ('custom/stylesheets.css,)
}
js = ('custom/javascript.js',)
Rendering Markdown
contrib.markdown
was depreciated in Django 1.5 meaning you can no longer use the markdown
filter in your template by default.
@wkcd has a good example of how to overcome by installing django-markdown-deux
:
{% extends 'base.html' %}
{% load markdown_deux_tags %}
...
<p>{{ entry.body|markdown }}</p>
...
Example
You can see a fully-fledged example of the widget in django-pagedown-example
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for django_pagedown-2.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83b30134131a49358196f8c2f0e2e25d730ee7a820482a7fda5ef386c22b1d6c |
|
MD5 | ec9d4a6ee0904d4692aebb39d8459c85 |
|
BLAKE2b-256 | 23fe3fa48b9c1b294bdff867b7f21fd516705a06211ca4915a5096e092847db0 |
Hashes for django_pagedown-2.0.3-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92f45ed52da00526cbdbf8c7cbd33d721f25f7d5757c4be5754d6c41695f7992 |
|
MD5 | a1a9cd463c348df51f2005bc341ddac1 |
|
BLAKE2b-256 | 31346c0c7aa847b7b63167ac649196365a2991b7812cb76582d9a0a6d46ffbbb |