• Stars
    star
    566
  • Rank 76,004 (Top 2 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created about 12 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A django app that allows the easy addition of Stack Overflow's "PageDown" markdown editor to a django form field, whether in a custom app or the Django Admin

Django Pagedown

Add Stack Overflow's "Pagedown" Markdown editor to your Django Admin or custom form.

Screenshot of Django Admin with Pagedown initialised

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

  1. Get the code: pip install django-pagedown
  2. Add pagedown.apps.PagedownConfig to your INSTALLED_APPS in settings.py
  3. Configure and 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 deprecated 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>
...

Image Uploads

You can enable image uploads, allowing your users to upload new images to the server and have them automatically inserted into the Pagedown widget (instead of just adding image URLs):

Screenshot of Django Admin with image upload enabled

To do so:

  1. Make sure you have set a MEDIA_URL and MEDIA_ROOT so that uploads will be propertly saved
  2. Add PAGEDOWN_IMAGE_UPLOAD_ENABLED=True to your settings
  3. Include the pagedown paths in your urls.py so that the upload endpoint is available
 # ...
 urlpatterns = [
     path('', include('pagedown.urls')),
     # ...
 ]

This will add the URL /pagedown/image-upload/ endpoint to your project. You can see the default view that handles the upload here

The following options are available via your settings to tweak how the image upload works:

  • PAGEDOWN_IMAGE_UPLOAD_PATH can be used to change the path within your media root (default is pagedown-uploads)
  • PAGEDOWN_IMAGE_UPLOAD_EXTENSIONS can be used to limit the extensions allowed for upload (default is jpg, jpeg, png, webp)
  • PAGEDOWN_IMAGE_UPLOAD_UNIQUE can be used to ensure all uploads are stored in a uniquely named subfolder, e.g. f748e009-c3cb-40f3-abf2-d103ab0ad259/my-file.png (default is False)

Check out the pagedown_init.js script to see how the upload is being performed on the client side.

Example

You can see a fully-fledged example of the widget in django-pagedown-example

More Repositories

1

shopify-email-templates

Makes it easier to create custom Shopify email templates by allowing you to edit and compile them offline locally before copy/pasting them to Shopifys admin
Liquid
39
star
2

pagedown

A fork of the Google code repository for StackOverflow's pagedown markdown editor. This fork adds the ability to initialise the pagedown editor on elements with custom #id selectors as opposed to the default #wmd-* selectors
JavaScript
28
star
3

django-charsleft-widget

A django widget that displays a normal text input with a remaining character count beside it
Python
26
star
4

craft-matrix-field-preview

Configure a screenshot preview for all your matrix field blocks, giving your clients a better publishing experience.
PHP
17
star
5

craft-remote-sync

Sync your database and assets across Craft environments using a remote destination like AWS S3
PHP
12
star
6

craft-remote-backup

Backup your database and assets to a remote destination like AWS, Dropbox, Backblaze and Google Drive
PHP
11
star
7

django-blog-examples

Some Django examples from my blog
Python
7
star
8

sentry-searchbutton

A simple sentry plugin that adds a sidebar widget to sentry, making it easy to launch a Google search for the error you are viewing
Python
7
star
9

bitbucket-jekyll-hook

An small flask app to accept bitbucket service hooks and update a jekyll blog installation
Python
6
star
10

standing-desk

Raspberry Pi controller for my standing desk
Python
5
star
11

django-circleci-example

An example CircleCI Django example
Python
4
star
12

cmsplugin-pagedown

A plugin for django-cms that includes the StackOverflow pagedown editor
Python
4
star
13

python-parsnip

Parsnip is a basic python interface to allow the sending of web texts via the main 4 Irish mobile operators websites. It is influenced by/based on Cabbage http://cabbagetexter.com/.
Python
4
star
14

homebridge-standing-desk

A homebridge plugin for a standing desk
TypeScript
2
star
15

django-pagedown-example

An example project for Django Pagedown
Python
2
star
16

django-cms-contrib

Python
2
star
17

timmyomahony-photoblog

A Next.js 13 static photoblog
TypeScript
1
star