项目作者: jhotujec

项目描述 :
Django application to add 'django-crispy-forms' layout objects for Bulma.io
高级语言: Python
项目地址: git://github.com/jhotujec/crispy-forms-bulma.git
创建时间: 2017-11-05T13:22:18Z
项目社区:https://github.com/jhotujec/crispy-forms-bulma

开源协议:MIT License

下载


django-crispy-bulma

This project is an early work in progress. You should not use this package yet, as it is poorly documented and is missing many important features. We’ll remove this header when it’s ready to use.

About

This is a Django application to add django-crispy-forms layout objects for Bulma.
It is a fork of crispy-forms-bulma by Jure Hotujec, with the intention
of adding support for Django 2.0+, as well as for components found in the bulma-extensions library.

Installation

You can install django-crispy-bulma from PyPI by running pip install django-crispy-bulma. Make sure you also have django-crispy-forms installed, as it will not work without it. In order to activate it, you’ll need to modify your projects settings.py file.

First, add django-crispy-bulma to your INSTALLED_APPS:

  1. INSTALLED_APPS = [
  2. 'crispy_forms',
  3. 'django_crispy_bulma',
  4. # ...
  5. ]

Next, add the following to the bottom of the file in order to configure crispy_forms to use the bulma template pack:

  1. CRISPY_ALLOWED_TEMPLATE_PACKS = (
  2. "bootstrap",
  3. "uni_form",
  4. "bootstrap3",
  5. "bootstrap4",
  6. "bulma",
  7. )
  8. CRISPY_TEMPLATE_PACK = "bulma"

You may also need to use Layout objects or form objects from django_crispy_bulma in order to build certain objects, like the UploadField. See the documentation below for specifics on objects like these.

EmailField

The EmailField looks like this:

EmailField

An EmailField can be created simply, like any other field in your form. For example:

  1. from django.forms import Form
  2. from django_crispy_bulma.forms import EmailField
  3. class MyForm(Form):
  4. my_email = EmailField(
  5. label="email",
  6. required=True
  7. )

IconField

If you’d like to render a field with an icon in it, you’ll need to make use of the Crispy Forms layout object,
and the IconField from our package. See below for an example:

IconField

  1. from crispy_forms.helper import FormHelper
  2. from crispy_forms.layout import Layout
  3. from django.forms import Form, CharField
  4. from django_crispy_bulma.layout import IconField
  5. class SetupForm(Form):
  6. def __init__(self, *args, **kwargs):
  7. super().__init__(*args, **kwargs)
  8. self.helper = FormHelper(self)
  9. self.helper.layout = Layout(
  10. IconField("username", icon_prepend="user"),
  11. )
  12. username = CharField(
  13. label="Username",
  14. required=True,
  15. )

Note that IconField also supports an icon_append keyword argument. This field only supports font-awesome icons.

UploadField

The UploadField looks like this:

UploadField

To create these with CrispyForms, you’ll need both a form object and a layout object from our package. Here’s an example of how to use them.

  1. from crispy_forms.helper import FormHelper
  2. from crispy_forms.layout import Layout
  3. from django import forms
  4. from django_crispy_bulma.layout import UploadField
  5. from django_crispy_bulma.forms import ImageField, FileField
  6. class MyForm(forms.Form):
  7. my_image = ImageField(
  8. label="Upload an image of your dog",
  9. required=False
  10. )
  11. my_file = FileField(
  12. label="Upload your actual dog in .dog format",
  13. required=True
  14. )
  15. def __init__(self, *args, **kwargs):
  16. super().__init__(*args, **kwargs)
  17. self.helper = FormHelper()
  18. self.helper.layout = Layout(
  19. UploadField("my_image"),
  20. UploadField("my_file"),
  21. )

A little bit of javascript is needed in order to get the filename to display after a file upload is successful.

Written in vanilla JS, this might look something like this:

  1. window.onload = function() {
  2. // Apply this to all upload fields
  3. const upload_fields = document.querySelectorAll(".file");
  4. for (let i = 0; i < upload_fields.length; i++) {
  5. let input = upload_fields[i].querySelector(".file-input");
  6. let filename = upload_fields[i].querySelector(".file-name");
  7. input.onchange = function() {
  8. filename.textContent = input.files[0].name;
  9. }
  10. }
  11. };

For your convenience, we provide a script that handles this in our companion package, django-simple-bulma. We highly recommend you use these two packages together.