django-gollum Documentation

Similar documents
Django Forme Documentation

alphafilter Documentation

Django Extra Views Documentation

Unit E Step-by-Step: Programming with Python

Django. Jinja2. Aymeric Augustin DjangoCong 2016

django-image-cropping Documentation

django-sticky-uploads Documentation

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

Easy-select2 Documentation

kaleo Documentation Release 1.5 Eldarion

django-contact-form Documentation

HTML version of slides:

wagtailmenus Documentation

django-precise-bbcode Documentation

This document provides a concise, introductory lesson in HTML formatting.

python Adam Zieli nski May 30, 2018

django-pgfields Documentation

django simple pagination Documentation

wagtailmenus Documentation

9 Common Patterns for Forms

django-cms-search Documentation

Django-Select2 Documentation. Nirupam Biswas

django-avatar Documentation

django-model-utils Documentation

Django-CSP Documentation

CIS192 Python Programming

Positioning in CSS: There are 5 different ways we can set our position:

petfinder-api Documentation

Django REST Framework JSON API Documentation

The Object clone() Method

The Dynamic Typing Interlude

Themes and Master Pages

CIS192 Python Programming

CIS192 Python Programming

django-ad-code Documentation

Bricks Documentation. Release 1.0. Germano Guerrini

Flask-Assets Documentation

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Django-frontend-notification Documentation

django-osm-field Release 0.3.1

Sage Construction Anywhere Setup Guide

Django with Python Course Catalog

In this project, you ll learn how to create your own webpage to tell a story, joke or poem. Think about the story you want to tell.

Preferential Resource Delivery Via Web Proxy or Web Browser

Sage Construction Anywhere Setup Guide

c122jan2714.notebook January 27, 2014

Build Site Create your site

Reminders. Full Django products are due next Thursday! CS370, Günay (Emory) Spring / 6

Rockablepress.com Envato.com. Rockable Press 2010

Be careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.

django-generic-filters Documentation

CIS192 Python Programming

Django Admin Sortable Documentation

CIS192 Python Programming

JavaScript. Like PHP, JavaScript is a modern programming language that is derived from the syntax at C.

2. Write style rules for how you d like certain elements to look.

Viewports. Peter-Paul Koch CSS Day, 4 June 2014

django-avatar Documentation

Introduction to Python (All the Basic Stuff)

Django File Picker Documentation

django IN DEPTH JAMES BENNETT PYCON MONTRÉAL 9TH APRIL 2015

django-secure Documentation

T H E I N T E R A C T I V E S H E L L

django-autocomplete-light Documentation

Signals Documentation

Visualforce & Lightning Experience

Dreamweaver Domain 5: Organizing Content by Using Dreamweaver CS5

Quick.JS Documentation

XML. Jonathan Geisler. April 18, 2008

django-intercom Documentation

Moving from MailChimp to GetResponse Guide

L6 Application Programming. Thibault Sellam Fall 2018

Bishop Blanchet Intranet Documentation

welcome to BOILERCAMP HOW TO WEB DEV

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O

DRY CSS A DON T-REPEAT-YOURSELF METHODOLOGY FOR CREATING EFFICIENT, UNIFIED AND SCALABLE STYLESHEETS

fragapy Documentation

RC Justified Gallery User guide for version 3.2.X. Last modified: 06/09/2016

Links Menu (Blogroll) Contents: Links Widget

OO and Ahh! An Introduction to Object Oriented Programming With PHP. Division 1 Systems. John Valance. Copyright John Valance Division 1 Systems

Introduction to Object-Oriented Programming

CS 61B Discussion 5: Inheritance II Fall 2014

How to lay out a web page with CSS

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

A Guide to CMS Functions

the magazine of the Marketing Research and Intelligence Association YEARS OF RESEARCH INTELLIGENCE A FUTURESPECTIVE

django-crucrudile Documentation

SASS Variables and Mixins Written by Margaret Rodgers. Variables. Contents. From Web Team. 1 Variables

Scraping I: Introduction to BeautifulSoup

Java Applets, etc. Instructor: Dmitri A. Gusev. Fall Lecture 25, December 5, CS 502: Computers and Communications Technology

TUTORIAL HOW TO: - Edit an Template with Constant Contact Create an

Building a Django Twilio Programmable Chat Application

PIC 10A Objects/Classes

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Pluggable Patterns. For Reusable Django Applications

django-xross Documentation

Python StatsD Documentation

pykafka Release dev.2

mincss Documentation Release 0.1 Peter Bengtsson

Python Problems MTH 151. Texas A&M University. November 8, 2017

Transcription:

django-gollum Documentation Release 1.0.0 Luke Sneeringer December 11, 2016

Contents 1 Installation 3 2 Dependencies 5 3 Getting Started 7 4 Getting Help 9 5 License 11 6 Index 13 6.1 Using django-gollum........................................... 13 6.2 Specifying HTML Attributes....................................... 13 6.3 Specifying CSS Classes......................................... 14 6.4 Settings.................................................. 15 i

ii

django-gollum Documentation, Release 1.0.0 This is django-gollum, a better way to mess with styling Django forms. gollum provides a better way to specify just the HTML Attributes and CSS classes you need on a Form subclass. Contents 1

django-gollum Documentation, Release 1.0.0 2 Contents

CHAPTER 1 Installation To install django-gollum, just use: pip install django-gollum 3

django-gollum Documentation, Release 1.0.0 4 Chapter 1. Installation

CHAPTER 2 Dependencies Python version django-gollum is tested on Python 2.7 and Python 3.3. It is probable that it will run on Python 2.6, but Python 2.6 is not explicitly tested. Django version django-gollum is tested against Django 1.4+ on Python 2.7, and Django 1.5+ on Python 3.3. Other dependencies dict.sorted six All dependencies are handled for you if you install using pip. 5

django-gollum Documentation, Release 1.0.0 6 Chapter 2. Dependencies

CHAPTER 3 Getting Started Adding HTML or CSS to a Django Form with gollum is easy: 1. Subclass gollum.forms.form or gollum.forms.modelform. 2. Add a Attrs or CSS inner class specifying fields with extra HTML attributes or CSS, respectively. Here s an example: from gollum import forms class MyForm(forms.Form): foo = models.charfield(max_length=50) bar = models.integerfield() class Attrs: bar = { 'placeholder': 25 } class CSS: foo = 'green' bar = ['purple', 'translucent'] When this form is rendered in the template, the foo <input> tag will have the green CSS class applied, and the bar <input> will have both purple and translucent applied. Additionally, the bar <input> would have placeholder="25" set as an HTML attribute. 7

django-gollum Documentation, Release 1.0.0 8 Chapter 3. Getting Started

CHAPTER 4 Getting Help If you think you ve found a bug in django-pgfields itself, please post an issue on the Issue Tracker. For usage help, you re free to e-mail the author, who will provide help (on a best effort basis) if possible. 9

django-gollum Documentation, Release 1.0.0 10 Chapter 4. Getting Help

CHAPTER 5 License New BSD 11

django-gollum Documentation, Release 1.0.0 12 Chapter 5. License

CHAPTER 6 Index 6.1 Using django-gollum In order to use gollum, you must use gollum s Form and ModelForm superclass, rather than the ones that ship with Django. This is extremely important: nothing documented here will work unless the gollum Form classes are superclasses of your form. This also gives you the ability to opt-in to gollum forms on an as-needed basis, if you prefer. The easiest way to do this is just to import your forms module from gollum rather than django: from gollum import forms class MyGollumForm(forms.Form): [...] It s that simple. Read on to learn how to make use of what gollum provides. The next topic is specifying HTML attributes. 6.2 Specifying HTML Attributes 6.2.1 In Form Classes gollum gives you the ability to specify HTML attributes easily on a form class. You can specify HTML attributes on any number of fields on your form by using an Attrs inner class within your form: from gollum import forms class MyGollumForm(forms.Form): foo = forms.integerfield() bar = forms.integerfield() class Attrs: foo = { 'disabled': 'true' } The above code will cause the form, when rendered, to add a disabled="true" HTML attribute to the foo field (but do nothing to the bar field). A common use-case for this, if you re using HTML5 forms, is to set a placeholder attribute. This causes the browser to display default text in the input field until it gains focus: 13

django-gollum Documentation, Release 1.0.0 from gollum import forms class UserForm(forms.Form): first_name = forms.charfield(max_length=30) last_name = forms.charfield(max_length=30) email = forms.emailfield(max_length=75) phone = forms.charfield(max_length=15, required=false) class Attrs: phone = { 'placeholder': 'Optional' } This code would cause the phone widget to look like this: <input name="phone" id="id_phone" type="text" placeholder="optional"> 6.2.2 In Templates Even though the Django documentation only exposes a way to set attributes in form widgets themselves, one could argue that a better place for HTML attribute information to live is in the template itself, especially since a form could be used in different templates and need different attributes set. gollum does not expose a special mechanism to do this (yet). However, this can be accomplished by directly calling the as_widget method of a bound field in Django. The problem: as_widget takes arguments, so you ll either need to write a template tag to send the necessary arguments to it, or use a template language that supports arguments (such as Jinja). Here s a quick sample of the latter option: {% for field in user_form %} {{ field.as_widget(attrs={ 'placeholder': field.label }) }} {% endfor %} This method allows you not to specify HTML attributes on your form class at all, and may be preferable, especially if the HTML attributes change depending on where the form is rendered. 6.2.3 CSS It would be possible to specify CSS classes in this way, by writing directly to the class HTML attribute. But don t; gollum also exposes a way to specify CSS classes. 6.3 Specifying CSS Classes Much like HTML attributes, gollum provides a way to specify CSS classes both in forms and also exposes a new way to do so in templates. However, unlike HTML attributes, where the most recent attribute specified wins, the CSS class specification understands to take the union of any CSS classes sent to it. That makes it preferable to use this mechanism rather than specifying class as an HTML attribute directly. 14 Chapter 6. Index

django-gollum Documentation, Release 1.0.0 6.3.1 In Form Classes In order to specify a CSS class on a form, declare a CSS inner class in your Form class, and specify any classes to apply: from gollum import forms class MyForm(forms.Form): foo = models.charfield(max_length=50) bar = models.integerfield() class CSS: foo = {'spam', 'eggs'} The above code, when rendered in an template, will cause the <input> tag for the foo field to have two CSS classes: spam and eggs. You can specify CSS classes here using a list, set, or tuple. You can also use a string if you have only one CSS class, or you can even use a space-separated string like you would in actual HTML markup. The following CSS inner-class is identical to the one in the example above: class CSS: foo = 'spam eggs' Order of class specification doesn t matter; it ll be normalized to a Python set, which is unordered. Duplicate classes don t matter either, for the same reason. 6.3.2 In Templates It may be preferable for your use case to specify CSS classes in templates rather than in the form itself. (This does seem like where such information naturally belongs.) Again, like in HTML Attributes, the solution is the as_widget method. The story s a little different this time, though: Django doesn t provide a clean way to specify CSS classes, so gollum actually subclasses BoundField to provide one. That mechanism is the css_classes keyword argument: {% for field in form %} {{ field.as_widget(css_classes='myclass') }} {% endfor %} Much like the specification above, you can send a list (or other, similar iterable) or a string, and gollum will do the right thing. 6.4 Settings gollum exposes a small number of settings that you can apply to get certain global behaviors on gollum forms. 6.4.1 FORM_REQUIRED_CSS_CLASS Default: (not set) Type: str 6.4. Settings 15

django-gollum Documentation, Release 1.0.0 If specified, the given CSS class will be added to every required field on every gollum form. 6.4.2 FORM_ERROR_CSS_CLASS Default: (not set) Type: str If specified, the given CSS class will be added to every field that is in an error state on every gollum form. 6.4.3 FORM_GLOBAL_CSS_CLASS Default: (not set) Type: str If specified, the given CSS class will be added to every field, period, the end. 6.4.4 FORM_REQUIRED_ATTRS Default: (not set) Type: dict If specified, the given HTML attributes will be applied to every required field on every gollum form. Example: FORM_REQUIRED_ATTRS = { 'required': 'true' } 6.4.5 FORM_OPTIONAL_ATTRS Default: (not set) Type: dict If specified, the given HTML attributes will be applied to every field that is not required on any gollum form. Example: FORM_OPTIONAL_ATTRS = { 'placeholder': 'Optional' } 6.4.6 FORM_GLOBAL_ATTRS Default: (not set) Type: dict If specified, the given HTML attributes will be applied to every form field on every gollum form. 16 Chapter 6. Index