Giter VIP home page Giter VIP logo

snippet-in-page-creation's Introduction

Step-by-step guide how to implement creating a Snippet in a Page with using django-autocomplete-light widget

Requirements

Wagtail 4.1 or higher

Installation

wagtail-generic-chooser

Run: pip install wagtail-generic-chooser

django-autocomplete-light

Run: pip install django-autocomplete-light

  1. In views extend the basic ModelChooserViewSet that has the create_tab_mixin_class parameter and modify the very appearance of our form, i.e. what it should look like and what fields it should have directly in the selector when created on Page
# views.py

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = [
            "title",
            "countries",
        ]
        widgets = {
            "countries": autocomplete.ModelSelect2Multiple(),
        }


class ArticleChooserViewSet(ModelChooserViewSet):
    icon = "plus"
    model = Article
    page_title = _("Choose a article")
    per_page = 20
    order_by = "-title"

    form_class = ArticleForm
  1. Define chooser for Article model in selector
# widgets.py

class ArticleChooser(AdminChooser):
    def __init__(self, *args, **kwargs):
        from home.models import Article

        self.model = Article
        super().__init__(*args, **kwargs)

    choose_one_text = _("Choose a article")
    choose_another_text = _("Choose another article")
    link_to_chosen_text = _("Edit this article")
    model = None
    choose_modal_url_name = "article_chooser:choose"
    icon = "user"
  1. Use of a custom widget
# models.py

class ArticlesOrderable(Orderable):
    """This allows us to select one or more blog authors from Snippets."""

    page = ParentalKey(
        "HomePage", related_name="article_source", blank=True, null=True
    )

    article = models.ForeignKey(
        "Article", on_delete=models.CASCADE, blank=True, null=True
    )

    panels = [
        FieldPanel("article", widget=ArticleChooser),
    ]
  1. Registration view
# wagtail_hooks.py

@hooks.register("register_admin_viewset")
def register_article_chooser_viewset():
    return ArticleChooserViewSet("article_chooser", url_prefix="article-chooser")
  1. Add global css and js to django autocomplete light, otherwise the files will not be downloaded in the selector making the widget not work
# wagtail_hooks.py

@hooks.register("insert_global_admin_css", order=100)
def global_admin_css():
    """Add /static/css/custom.css to the admin."""
    return format_html(
        """
        <link rel="stylesheet" href="{}">
        <link rel="stylesheet" href="{}">
        <link rel="stylesheet" href="{}">
        """,
        static("autocomplete_light/select2.css"),
        static("admin/css/autocomplete.css"),
        static("admin/css/vendor/select2/select2.css"),
    )


@hooks.register("insert_global_admin_js", order=100)
def global_admin_js():
    """Add /static/css/custom.js to the admin."""
    return format_html(
        """
        <script src="{}"></script>
        <script src="{}"></script>
        <script src="{}"></script>
        """,
        static("autocomplete_light/select2.js"),
        static("autocomplete_light/autocomplete_light.js"),
        static("admin/js/vendor/select2/select2.full.js"),
    )

Note. These are just selected pieces of code from the repository

snippet-in-page-creation's People

Contributors

sebastvin avatar

Stargazers

 avatar

Watchers

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