Modifying the django-cms 'add plugin' template on a per plugin basis? - django

The template I'm specifically talking about is the one that is used when a user add's a plugin to a page. Both in the admin area, and when modifying pages directly, it is displayed using an iframe.
The template itself is located cms/templates/admin/cms/page/plugin_change_form.html.
My problem is that I need some javascript to populate a drop down list within the form. All the javascript is run before the iframe is added to the page though, so I thought if I managed to edit the template I can tell the iframe to load some specific js. I can obviously just change the template directly, but that's a bit of an undesirable solution. I would rather keep it within the django application and even better have the js run only on specific plugins.
Any thoughts are appreciated.

You can set the change_form_template on your CMSPluginBase subclass, as CMSPluginBase is a subclass of django.contrib.admin.options.ModelAdmin.

Related

For development purpose, how can I open a Django Jinjia2 template in browser with preliminary rendering (extending, including)?

Problem description
I am starting working on a Django project and the server-side rendering template is a little hard to work with. Usually I develop the front-end application with hot module reload server so that I can see the rendered page during development.
However, for Django project I can only view the site by serving and open from browser. If I open the template directly, the include, extends are not processed, hence CSS and JavaScript are not loaded in the base template.
My question is, is there any tool or workflow that can help develop the Django template in offline so that the style and layout can be rendered properly during development?
Edit: justification
As comment mentioned there are some plugins that supports reload the Django page. However, I would like to know whether it is possible to work with the template HTML totally off the Django server, i.e. work with the html static page? There are some scenarios where I feel it is not suitable:
A page that refreshes slowly: e.g., slow database query before the page can be rendered.
A template that is not accessible normally: e.g., a part of html inside {% if %} that is not normally accessible, such as an error message.
A template that is not yet registered in the urlpatterns routes.
Thank you

How to import a Wagtail page on all the other wagtail pages

I want to add some content on one of my Wagtail pages and I am trying to import that Wagtail page on all my other wagtail pages. The reason I am trying to do this is that if in the future I make a change on the content it should consistently reflect on all the other Wagtail pages.
Is there a way that I can import a Wagtail page on all my other Wagtail pages, if so please let me know.
Thanks in advance!
I have a website which has the following Configurations:
1) Django-2.0.8
2) Wagtail-2.2.4
A custom template tag is a good way to achieve this, as it provides a place to run custom Python code (for retrieving the necessary data) before outputting the results to the template, either directly as a string or by rendering a template. For example, if you had a footer_text field on a HomePage model, and wanted to display the footer text of the HomePage with slug 'home' on every page, you could define a custom tag as follows:
#register.inclusion_tag('myapp/includes/footer.html')
def footer():
homepage = HomePage.objects.get(slug='home')
return {'footer_text': homepage.footer_text}
You could also look at Wagtail's site settings module as a way to define global content to be re-used across a site (although it's missing a few features that you'd get from defining it on a page model, such as moderation workflow and revision history).

Adding Wagtail to an existing Django app

I'm quite new to Django and Wagtail, and I'm having some difficulty with what I think is a very basic use.
How do I allow Wagtail to edit an existing view's template, while serving that template using Django's serving mechanism?
Assume I have an app (HomePage) created to serve the site's main index (/). I have the HomePage's views set up to render template and certain elements dynamically. Now I want that template to be editable via Wagtail's CMS interface. Something as simple as an image on the frontpage, or a headline.
The closest I've gotten so far has been to follow the Wagtail beginner's tutorial to override the base HomePage class in my app's models.py. That only made my pages available via the /pages/ URL.
Thank you for any help.
Since your site's home page is not a Page object in the Wagtail sense, I'd suggest looking at Wagtail's facilities for managing non-page content - snippets and ModelAdmin would be possible candidates, but I reckon the site settings module would be the best fit.
A Setting model gives you a set of fields which can be configured for display in the Wagtail admin using a 'panels' definition, just like you'd get for a page model - with the important property that only one settings record exists per site. You can retrieve this record within your homepage view or template as shown in the docs, and output it on your template as desired.
One way do that, is to let Wagtail serve your homepage. You will need to change your project's url configuration accordingly, to make wagtail's urls serve the root of your site.
Then, you can pack your dynamic content into a custom template_tag and include in your homepage html template.

how to render a sitecore partial without pulling it in as a datasource?

You can create a versioned image in Sitecore by referencing its item url like so:
<img src="~/media/CEB3BE892F3E47E9BCEC3F357F974606.ashx">
Is there a similar way to reuse a portion of html? Say a header or footer or dropdown menu, for example.
I'm familiar with how to do this in Rails, but don't have the user privileges I'd need in Sitecore to be able to do this in anything other than html/css/js. I can't modify the templates or go into the C# code.
Depending on whether you're using Web Forms or MVC you'll want to know about either Asp.Net MVC Partial Views, or Asp.Net User Controls to create a sublayout/rendering.
However since you don't have access to the code, I don't think there's really anything you can do.

Custom Django admin panel

I want to use Django for a web application I'm building that will have an admin panel. I know that you need to just activate the admin app and you're ready to go. However, I would like to have a custom panel, I mean, I want to design the layout myself, I want to add menus and forms for the admin to insert new data in the database etc. Is it possible? or I should write a similar application that will have such features?
For more control over the layout (custom menus etc.) you should check django-admin-tools.
And if you take a look at Django's docs you'll learn that you can easily tweak and override most parts of the admin. For example here is a demonstration on how to use a custom form:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin
So the admin is pretty customizable. But the question if you should build your own app or reuse the admin depends pretty much on your specific needs. At least make sure you know in which directions the admin can be easily bend.
The sole purpose for Django's admin is to allow you to manipulate (add/edit/remove) data in your database. I think you should at least try to check what the admin is capable of before trying to reinvent the wheel. You'll soon discover the level of complex insight the admin allows you to have. Then you'll discover that making it yourself is unnecessary excess of work and you'll end up with modifying a couple of admin-templates and CSS styles.
Yes, you can customize Django Admin Panel, Django provides some sort of customization for showing database tables structure from their own, for that you can follow DJANGO ADMIN SITE DOC , it will really help you.
For the customizations beyond the Django admin site settings, you can customize admin panel add manual layout, by adding manual detailing in Django template files which are stored in Django environment, django/django/contrib/admin/templates/admin/index.html of your current Django version. You can update its HTML, CSS, and JS according to need.