Git Product home page Git Product logo

kaushalaman / django-pagedown Goto Github PK

View Code? Open in Web Editor NEW

This project forked from timmyomahony/django-pagedown

0.0 2.0 0.0 707 KB

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

License: BSD 3-Clause "New" or "Revised" License

Python 40.78% CSS 32.42% JavaScript 24.35% HTML 2.44%

django-pagedown's Introduction

Django Pagedown

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


Screenshot of Django Admin with Pagedown initialised


Installation

  1. Get the code: pip install django-pagedown
  2. Add pagedown to your INSTALLED_APPS
  3. Make sure to collect the static files: python manage.py collectstatic --noinput

Note that this package will install a cloned copy (git submodule)of the Pagedown library from http://github.com/timmyomahony/pagedown/.

If you don't like (or are having problems with) PyPi, you can alternatively install:

  • Via pip from GitHub: pip install -e git+https://[email protected]/timmyomahony/django-pagedown.git#egg=django-pagedown
  • Manually clone from Github:
    • git clone https://[email protected]/timmyomahony/django-pagedown.git
    • cd django-pagedown
    • git submodule update --init

Markdown Safety

Remember that this library doesn't render your markdown for you outside of the admin widget nor does it do any internal sanitization. Markdown can accept any valid HTML so you have to be careful and make sure you are rendering the output of any untrusted input safely (with django-markdown-deux for example), otherwise you could have users embedding scripts in your pagedown text areas


Usage

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 pagedown.widgets import AdminPagedownWidget
from django.db import models


class FooModelAdmin(models.ModelAdmin):
	formfield_overrides = {
    	models.TextField: {'widget': AdminPagedownWidget },
	}

Alternatively, to only use it on particular fields, first create a form (in forms.py):

from pagedown.widgets import AdminPagedownWidget
from django import forms
from models import FooModel


class FooModelForm(forms.ModelForm):
    a_text_field = forms.CharField(widget=AdminPagedownWidget())
    another_text_field = forms.CharField(widget=AdminPagedownWidget())

    class Meta:
        model = FooModel

and in your admin.py:

from forms import FooModelForm
from models import FooModel
from django.contrib import admin


class FooModelAdmin(admin.ModelAdmin):
	form = FooModelForm

admin.site.register(FooModel, FooModelAdmin)

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 pagedown.widgets import PagedownWidget
from django import forms
from models import FooModel


class FooModelForm(forms.ModelForm):
    a_text_field = forms.CharField(widget=PagedownWidget())
    another_text_field = forms.CharField(widget=PagedownWidget())

    class Meta:
    model = FooModel

Then define your urls/views:

from forms import FooModelForm
from django.views.generic import FormView
from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^$', FormView.as_view(
        template_name="baz.html",
        form_class=FooModelForm)),
)

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>

Showing/Hiding the Preview Box

You can control whether or not to show the dynamically rendered preview box below the pagedown widget in two ways:

  • Globally: by using the PAGEDOWN_SHOW_PREVIEW option in your settings.py (this is mentioned further down the page). This will enable/disable the preview for all pagedown widgets throughout your application.

  • Per Widget: by supplying a show_preview keyword argument when initialising your widget instance in your form. This gives you finer control over which of the fields can make use of the preview when rendering the pagedown widget. Note that this approach will take preference over the PAGEDOWN_SHOW_PREVIEW option.

     ...
    
     class FooModelForm(forms.ModelForm):
     	foo = forms.CharField(widget=PagedownWidget(show_preview=False))
     
     	class Meta:
     		model = FooModel
    

Customizing the Widget Template/HTML

If you want to customize the HTML used to render the pagedown widget altogether, you can. There are two ways:

  • Globally: by default, the template used to render the pagedown widget is located at pagedown/widgets/default.html.

    • You can override this template by creating pagedown/widgets/default.html within your own template directory. This will take preference if you are using Django's default template loading system
    • You can use the PAGEDOWN_DEFAULT_TEMPLATE settings to point to a different template file
  • Per Widget: by supplying a template keyword argument when initialising your widget instance in your form. This should be the path to the template you wish to use to render this instance.

      ...
      
      class FooModelForm(forms.ModelForm):
      	foo = forms.CharField(widget=PagedownWidge(template="path/to/template.html"))
      
      	class Meta:
      		model = FooModel
    

Customizing the CSS

If you want to change the CSS used to display the widgets, you also can. Again, there are two ways:

  • Globally: You can specify the CSS files to be included by the widget by providing a tuple of paths via a PAGEDOWN_WIDGET_CSS variable in your settings.py

     # Import the default pagedown css first, then our custom CSS sheet
     # to avoid having to specify all the default styles
     PAGEDOWN_WIDGET_CSS = ('pagedown/demo/browser/demo.css', "pagedown/custom.css",)
    
  • Per Widget: by supplying a css keyword argument when initialising your widget instance in your form

      ...
      
      class FooModelForm(forms.ModelForm):
      	foo = forms.CharField(widget=PagedownWidge(css=("custom/css1.css", "custom/css2.css")))
      
      	class Meta:
      		model = FooModel
    

Options

The following options can be added to your default settings.py file to control certain aspects of django-pagedown. Note that changing these will affect all instances of the pagedown widget throughout your app.:

  • PAGEDOWN_SHOW_PREVIEW (boolean): whether or not to show the dynamic markdown preview below the markdown text area for the pagedown widgets. The default is True.
  • PAGEDOWN_DEFAULT_TEMPLATE (string): the template used to render the pagedown widget. The default template is located in pagedown/widgets/default.html.
  • PAGEDOWN_WIDGET_CSS (tuple): the path to the CSS file to be used by the pagedown widget. The default path is `pagedown/

Rendering Markdown In Your Template

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>
...

TODO

  • Add support for images uploading or hooks into the likes of django-filer etc.

django-pagedown's People

Contributors

fin avatar timmyomahony avatar ivanvenosdel avatar brianhicks avatar danirus avatar anno1337 avatar nkunihiko avatar ondrejsika avatar sabberworm avatar

Watchers

James Cloos avatar Aman Kaushal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.