Git Product home page Git Product logo

django-wysihtml5's Introduction

Django-wysihtml5

downloads TravisCI

Tested under:

  • Python 3.2 and django 1.6: builds
  • Python 3.2 and django 1.5.5: builds
  • Python 2.7 and django 1.5.5: builds
  • Python 2.7 and django 1.4.10: builds

By Daniel Rus Morales <http://danir.us/>

Simple Django app that provides a Wysihtml5 widget. Wysihtml5 is a reach-text editor.

Includes a demo project and a test suite. If you commit code, please consider adding proper coverage (especially if it has a chance for a regression) in the test suite.

Run the tests with: python setup.py test

Quick start

  1. Add wysihtml5 to INSTALLED_APPS in your settings module.
  2. Create a model with a field of type Wysihtml5TextField.
  3. Create an admin class for that model by inheriting from both wysihtml5.admin.AdminWysihtml5TextFieldMixin and Django's admin.ModelAdmin.
  4. Hit your app's admin URL and see the wysihtml5 editor in action.

Demo site

The demo site (example/demo/) shows the widget in action. Follow the instructions to run the demo and go to the Admin UI to see how the widget looks like. For the public side the demo uses bootstrap-wysihtml5.

  1. Cd into the demo directory: cd django-wysihtml5/example/demo
  2. Run syncdb with --no-input: python manage.py syncdb --noinput (user: admin, pwd: admin)
  3. Run collectstatic: python manage.py collectstatic
  4. Run the dev web server: python manage.py runserver
  5. Hit the demo URL: http://localhost:8000 to see the example article fully formatted with Wysihtml5.
  6. Hit the demo admin URL: http://localhost:8000/admin/ to see the widget in action.
  7. Visit the root URL to see a list of articles and add new ones: http://localhost:8000/

Customization

Three settings allow you to customize the editor:

  • WYSIHTML5_EDITOR -> editor's parameters customization
  • WYSIHTML5_TOOLBAR -> editor's commands and toolbar customization
  • WYSIHTML5_ALLOWED_TAGS -> what HTML tags are allowed

WYSIHTML5_EDITOR setting

Customize the Wysihtml5 Editor parameters, as in here, by defining WYSIHTML5_EDITOR in your settings file. WYSIHTML5_EDITOR is a dictionary that defaults to:

EDITOR_CONF = {
    # Give the editor a name, the name will also be set as class
    # name on the iframe and on the iframe's body
    'name': 'null',
    # Whether the editor should look like the textarea (by adopting styles)
    'style': 'true',
    # Id of the toolbar element, pass falsey value if you don't want
    # any toolbar logic
    'toolbar': 'null',
    # Whether urls, entered by the user should automatically become
    # clickable-links
    'autoLink': 'true',
    # Object which includes parser rules (set this to
    # examples/rules/spec.json or your own spec, otherwise only span
    # tags are allowed!)
    'parserRules': 'wysihtml5ParserRules',
    # Parser method to use when the user inserts content via copy & paste
    'parser': 'wysihtml5.dom.parse || Prototype.K',
    # Class name which should be set on the contentEditable element in
    # the created sandbox iframe, can be styled via the 'stylesheets' option
    'composerClassName': '"wysihtml5-editor"',
    # Class name to add to the body when the wysihtml5 editor is supported
    'bodyClassName': '"wysihtml5-supported"',
    # By default wysihtml5 will insert <br> for line breaks, set this to
    # false to use <p>
    'useLineBreaks': 'true',
    # Array (or single string) of stylesheet urls to be loaded in the
    # editor's iframe
    'stylesheets': '["%s"]' % (settings.STATIC_URL +
                               "wysihtml5/css/stylesheet.css"),
    # Placeholder text to use, defaults to the placeholder attribute
    # on the textarea element
    'placeholderText': 'null',
    # Whether the composer should allow the user to manually resize
    # images, tables etc.
    'allowObjectResizing': 'true',
    # Whether the rich text editor should be rendered on touch devices
    # (wysihtml5 >= 0.3.0 comes with basic support for iOS 5)
    'supportTouchDevices': 'true'
}

WYSIHTML5_TOOLBAR setting

You can customize Wysihtml5 commands configuration by defining WYSIHTML5_TOOLBAR in your settings file. WYSIHTML5_TOOLBAR is a dictionary that defaults to:

WYSIHTML5_TOOLBAR = {
    "formatBlockHeader": {
        "active": True,
        "command_name": "formatBlock",
        "render_icon": "wysihtml5.widgets.render_formatBlockHeader_icon"
    },
    "formatBlockParagraph": {
        "active": True,
        "command_name": "formatBlock",
        "render_icon": "wysihtml5.widgets.render_formatBlockParagraph_icon"
    },
    "bold": {
        "active": True,
        "command_name": "bold",
        "render_icon": "wysihtml5.widgets.render_bold_icon"
   },
    "italic": {
        "active": True,
        "command_name": "italic",
        "render_icon": "wysihtml5.widgets.render_italic_icon"
    },
    "underline": {
        "active": True,
        "command_name": "underline",
        "render_icon": "wysihtml5.widgets.render_underline_icon"
    },
    "justifyLeft": {
        "active": True,
        "command_name": "justifyLeft",
        "render_icon": "wysihtml5.widgets.render_justifyLeft_icon"
    },
    "justifyCenter": {
        "active": True,
        "command_name": "justifyCenter",
        "render_icon": "wysihtml5.widgets.render_justifyCenter_icon"
    },
    "justifyRight": {
        "active": True,
        "command_name": "justifyRight",
        "render_icon": "wysihtml5.widgets.render_justifyRight_icon"
    },
    "insertOrderedList": {
        "active": True,
        "command_name": "insertOrderedList",
        "render_icon": "wysihtml5.widgets.render_insertOrderedList_icon"
    },
    "insertUnorderedList": {
        "active": True,
        "command_name": "insertUnorderedList",
        "render_icon": "wysihtml5.widgets.render_insertUnorderedList_icon"
    },
    "insertImage": {
        "active": True,
        "command_name": "insertImage",
        "render_icon": "wysihtml5.widgets.render_insertImage_icon",
        "render_dialog": "wysihtml5.widgets.render_insertImage_dialog"
    },
    "createLink": {
        "active": True,
        "command_name": "createLink",
        "render_icon": "wysihtml5.widgets.render_createLink_icon",
        "render_dialog": "wysihtml5.widgets.render_createLink_dialog"
    },
    "insertHTML": {
        "active": True,
        "command_name": "insertHTML",
        "command_value": "<blockquote>quote</blockquote>",
        "render_icon": "wysihtml5.widgets.render_insertHTML_icon"
    },
    "foreColor": {
        "active": True,
        "command_name": "foreColor",
        "render_icon": "wysihtml5.widgets.render_foreColor_icon"
    },
    "changeView": {
        "active": True,
        "command_name": "change_view",
        "render_icon": "wysihtml5.widgets.render_changeView_icon"
    },
}

Two of the commands allow dialog customization too:

Customize commands by declaring them in the WYSIHTML5_TOOLBAR dictionary. You can:

  • Disable commands by setting "active": False.
  • Redefine a command name to provide your own Wysihtml5 command implementation. Change the setting "command_name": "yourNewCommand", and write your function command in JavaScript. See link to an example below.
  • Render your own command icons by writing a render_icon function.
  • Render your own widget dialogs for createLink and insertImage.

Only declare your customized commands and attributes, django-wysihtml5 will use the default settings for the rest.

WYSIHTML5_ALLOWED_TAGS setting

Filter what HTML tags are allowed in the Django field by using this setting. Be careful about what tags you do allow as it is a potential source of malicious code. Only listed tags are allowed. By default only the following tags are allowed:

h1 h2 h3 h4 h5 h6 div p b i u ul ol li span img a blockquote

Have questions?

Go and make happy your users!

django-wysihtml5's People

Contributors

danirus avatar jonashaag avatar komarserjio avatar nolski avatar paramburu avatar ufodriver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-wysihtml5's Issues

Not on PyPI

the docs say it can be installed from PyPI but it's not there.

pip install is broken

I am trying to use the pip installer however there is an error in the:

username@twingeeks:~$ sudo pip install django-wysihtml5
Downloading/unpacking django-wysihtml5
Downloading django-wysihtml5-1.1b1.linux-x86_64.tar.gz (179Kb): 179Kb downloaded
Running setup.py egg_info for package django-wysihtml5
Traceback (most recent call last):
File "", line 14, in
IOError: [Errno 2] No such file or directory: '/home/winterfell/build/django-wysihtml5/setup.py'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "", line 14, in

IOError: [Errno 2] No such file or directory: '/home/winterfell/build/django-wysihtml5/setup.py'

Static Files

Setuptools doesn't copy the static directory...

KeyError with createLink and insertImage -> False

I just try to set some of the Toolbar buttons in WYSIHTML5_TOOLBAR to False. Its all fine, except for "createLink" and "insertImage".
If i try to set one of this to "active":False, than the page raise a KeyError.
Is it a bug or maybe am I doing something wrong?

hardcoded inline-block?

Hi Daniel,

I have a small but annoying issue with django-wysihtml5. I am rather new to Django. I think the issue comes from django-wysihtml5, but I may be wrong!

I've put 'django-wysihtml5' in my INSTALLED_APPS, and I also installed the latest https://github.com/bootstrap-wysiwyg/bootstrap3-wysiwyg with Bower. It works. But for some reason, the editor isn't taking the full width of the page. I attach an image below.

The problematic line is no 175 in widgets.py:
return mark_safe('

' +

If I change inline-block for 'inline', then my editor is fine! Any reason putting 'inline-block' instead of 'inline'? It would be nice to be able to customize it from my python code.

Thanks a lot for your help.

screen shot 2014-10-20 at 16 59 41

Format Bug

For some reason sentences are not being given a newline when just typing in plan text. To reproduce, set up the demo. Create a new demo article. In the body, add one line of plain text followed by two inputs by the enter key. Even though there are two spaces, they are not being rendered properly.

selection_013

If I save the above, and view it in the admin screen, everything is combined into one line:

selection_014

Feature request

Would it be possible to set the default layout of the wysiwyg editor buttons to be bootstrap buttons? Or at least include "btn" in the class for each button?

I set the model field as the directions state:

comment = Wysihtml5TextField(verbose_name='', blank=False)

Then in my model I usually set the widget attributes:

def __init__(self, *args, **kwargs):                                                                                                    
        super(CommentForm, self).__init__(*args, **kwargs)
        self.fields['comment'].widget.attrs={'class':'textbody', 'placeholder':"Please leave your comment here.", 'rows': 20, 'cols': 80}

The problem is that I get the old layout and not the twitter bootstrap layout. I've been doing research and found your comment here:

http://stackoverflow.com/questions/16165825/how-to-use-django-wysihtml5-in-frontend-forms/16394094#16394094

I tried importing:

from topic.twitter_bootstrap import Wysihtml5BootstrapWidget

self.fields['comment'].Wysihtml5BootstrapWidget.attrs={'class':'textbody', 'placeholder':"Please leave your comment here.", 'rows': 20, 'cols': 80}

However, this did (as I was expecting, but not 100% sure) not work.

Can you think of any other/easier way to get the twitter bootstrap layout by default? I really feel with that layout as the default, this module would become very popular, very quickly.

Also, have you seen: http://mindmup.github.io/bootstrap-wysiwyg/

Check it out in Chrome, as it offers voice input which is incredible.

Include functionality in frontend forms (not only in the admin backend)

If I'm not wrong, this django app is intended to add rich text functionality to admin and that's great because it's really simple to setup.

But I'd like to be able to add this functionality also to the forms that I'm using in the frontend. Is there any way to do so currently, or is it a new enhancement to be done? In case it's a new feature it'd be great to add it to this library!

Thanks! :)

NodeJS Config

I have been really digging this module. I am stuck in one place. I am trying to figure out how to name the

#document
<html>
<body marginwidth="0" marginheight="0" contenteditable="true" class="vLargeTextField wysihtml5-editor placeholder" spellcheck="true" style="background-color: rgb(255, 255, 255); color: rgb(85, 85, 85); cursor: auto; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; letter-spacing: normal; text-align: start; text-decoration: none; text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;">testing text</body>
<html>

I have multiple editors on my screen. I would be great if I could name the body, something like editor1, editor2, etc...

#document
<html>
<body name="editor1" marginwidth="0" marginheight="0" contenteditable="true" class="vLargeTextField wysihtml5-editor placeholder" spellcheck="true" style="background-color: rgb(255, 255, 255); color: rgb(85, 85, 85); cursor: auto; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13.63636302947998px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; letter-spacing: normal; text-align: start; text-decoration: none; text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;">testing text</body>
<html>

The reason I would like this functionality is so that I could do something with javascript where I can update the textarea on the fly and vice versa.

 $("#textarea2").keyup(function () {
 $('[name="editor1"]').contents().find('body').html($('#textarea2').val());
});

I could also do vice versa. Unfortunately all the bodies are exactly the same. So I am having one hell of a time grabbing the .html from from the body that is generated in the iframe.

Essentially I am trying to hook in the textarea into sharejs, which would allow people to collaborate through this editor in near real time. Once I get is set up, I'd love to give you the docs on how I set it up. Its pretty straight forward. :)

Cheers,
Jon

Focusing on Element

Is is possible to focus in on the textarea for the wysihtml5 editor? I'm trying to add some validation to check for text input, however I an not able to focus in on the body to verify that text. Essentially I am trying to do somehting similar to this:

$('#id_textfield').keyup(checkSubmit);

However I do not have an ID, NAME, or way of using the class to get the element. Do you have any idea's of the best way to approach this? This is what is available:

<body marginwidth="0" marginheight="0" contenteditable="true" class="vLargeTextField wysihtml5-editor" spellcheck="true" style="background-color: rgb(255, 255, 255); color: rgb(85, 85, 85); cursor: auto; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; letter-spacing: normal; text-align: start; text-decoration: none; text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;">asdfasdfasdfasdfadsfasdf<br>asdf<br><br><br><div>asdf</div></body>

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.