I'm trying to integrate Wagtail CMS with my existing Django project. Other than this basic installation, I made a file named wagtail_hooks.py. Everything's good so far, but I need to use WYSIWYG editor on Wagtail CMS. Is there a way to access models.py for Wagtail so that I can use third-party WYSIWYG editor on model level?
MY_APP/wagtail_hooks.py
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
from .models import Store
class StoreAdmin(ModelAdmin):
model = Store
menu_label = 'Store' # ditch this to use verbose_name_plural from model
menu_icon = 'doc-full' # change as required
menu_order = 10 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ['id', 'status', 'typ', 'businessName',]
search_fields = ('businessName', 'created_by__username',)
# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(StoreAdmin)
Wagtail comes with an excellent WYSIWYG editor, Draftail, based on DraftJS. It is highly extensible:
http://docs.wagtail.io/en/v2.4/advanced_topics/customisation/extending_draftail.html
To use it, you can change your model to use wagtail.core.fields.RichTextField instead of TextField.
There are several other WYSIWYG editors available, for example, you can still use the old editor with this setting:
WAGTAILADMIN_RICH_TEXT_EDITORS = {
'default': {
'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
}
}
Good luck!
Related
i am have enabled everything needed to work with spatial data at the database and django setting level, my profile model has a default_location field that is a PointField. as shown below.
from django.contrib.gis import models
class Profile(models.Model):
...
default_location = models.PointField()
i registered the profile model as an inline to be viewed and edited from within a User model (one-to-one relationship between user and profile).code shown below
class ProfileInline(StackedInline):
model = models.Profile
class NewUserAdmin(admin.GISModelAdmin):
gis_widget = OSMWidget
inlines = [ProfileInline]
admin.site.unregister(models.User)
admin.site.register(models.User, NewUserAdmin)
however i keep getting a openlayer map in my django admin page
please can anyone suggest a fix to this. i need open street map because of it detailed street feature.
You can use the django-leaflet package. By default an OpenStreetMap is displayed, and it also has better tools and interface.
After installing you need add leaflet to INSTALLED_APPS in settings.py.
Then you use LeafletGeoAdmin in your ModelAdmin in admin.py.
You can add some customizations by adding this to your settings.py:
LEAFLET_CONFIG = {
'DEFAULT_CENTER': (39.694819, -8.130229),
'DEFAULT_ZOOM': 6,
'MAX_ZOOM': 20,
'MIN_ZOOM':3,
'SCALE': 'both'
}
More information here: https://django-leaflet.readthedocs.io/
I am building an app in Django.
I found there is a very easy way to integrate a widget into django admin that allows the admin to filter model objects by fields values. That is achieved by including the line
list_filter = ['field_to_filter_by_its_values']
into the class mymodelAdmin(ImportExportModelAdmin) in admin.py, as shown below
class target_area_history_dataAdmin(ImportExportModelAdmin):
resource_class = target_area_history_dataResource
list_filter = ['Target_area_input_data__Name']
admin.site.register(target_area_history_data, target_area_history_dataAdmin)
Now, instead of integrate a widget to filter my model objects by that field, is there a way to integrate a widget to sort my model objects by that field?
Note: I am using Django Import-Export in my model.
I'll suggest you use django-treebeard. This allows you to view tree nodes hierarchically in the administration interface, with interface features dependent upon the tree algorithm used.
# admin.py
from django.contrib import admin
from treebeard.admin import TreeAdmin
from .models import Category
class CategoryAdmin(TreeAdmin):
list_display = ("title", "created", "modified",)
list_filter = ("created",)
admin.site.register(Category, CategoryAdmin)
What's cool about this is that you can not only sort (by clicking the header row) but also drag things around, as shown in this image.
I recommend you using the grapelli admin interface that allows what you need and a bit more. here you have the grapelli project page and the https://github.com/sehmaschine/django-grappelli.
It's a well documented package and is plug and play for what you need. It also gives a fresh face to Django Admin and is compatible with Django import/export package.
I'm trying to integrate my Django project with Wagtail CMS. As it is done with existing Django project, I'm trying to follow this documentation.
After having done that, I can see my users on Wagtail, but not my apps. Should I need additional steps to bring my existing Django apps to Wagtail?
What I'm guessing is if the below two code snippets don't matter with it's added position. The documentation says to add them without specifying where exactly before or after.
For INSTALLED_APPS
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
For MIDDLEWAR
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
As xyres explained in the comments, I tried following docs.wagtail.io/en/v2.4/reference/contrib/modeladmin.
As the documentation explain that, what I did is the following:
Add wagtail.contrib.modeladmin in INSTALLED_APPS.
settings.py
INSTALLED_APPS = [
...
'wagtail.contrib.modeladmin',
]
Then, I made a file named wagtail_hooks.py under Django app that I want to be seen in Wagtail CMS and put the below codes in the file.
wagtail_hooks.py
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
from .models import Book
class BookAdmin(ModelAdmin):
model = Book
menu_label = 'Book' # ditch this to use verbose_name_plural from model
menu_icon = 'pilcrow' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ('title', 'author')
list_filter = ('author',)
search_fields = ('title', 'author')
# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(BookAdmin)
With the above done, you will see your App model in Wagtail CMS.
I am building out a restaurant website and using Wagtail CMS Snippets for the owner to manage menu items. The list of menu items are getting rather long and I was wondering if there is any way to add a search input field to the Snippets admin window? Below is an annotated screenshot for visual reference. Thank you.
This can easily be solved by using Wagtail's ModelAdmin module (http://docs.wagtail.io/en/v1.8.1/reference/contrib/modeladmin/), all you need is to add this piece of code to your wagtail_hooks.py file:
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
from .models import Product
class ProductAdmin(ModelAdmin):
model = Product
menu_label = 'Product' # ditch this to use verbose_name_plural from model
menu_icon = 'date' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ('title', 'example_field2', 'example_field3', 'live')
list_filter = ('live', 'example_field2', 'example_field3')
search_fields = ('title',)
# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(ProductAdmin)
It'll create a separate menu entry for your Products model that's customisable much like default Django Admin listing. Which means you can easily add different filters and sorters to a listing.
This is a very powerful feature and I myself don't show clients the "Snippets" section at all; it's just too simple and ugly. Instead, I create a separate ModelAdmin per snippet and this gives me the power of customisation.
The search bar will appear automatically once you set up your model to be indexed with the search system. You can do this by inheriting from the wagtail.wagtailsearch.index.Indexed class and defining a search_fields list on your model, as described here: http://docs.wagtail.io/en/v1.8.1/topics/search/indexing.html#wagtailsearch-indexing-models
(Note that if you're using Elasticsearch, you'll also need to run ./manage.py update_index to add the items to the search index.)
I have set up TinyMCE to work with the Admin panel (as per the instructions in the Django Docs http://code.djangoproject.com/wiki/AddWYSIWYGEditor )
The problem is that I have Inlines and other text areas within my model for which I don't want TinyMCE to render
Does anyone know how to set TinyMCE to only load for particular fields within my model?
Thanks
EDIT
Ok, so I've installed django-tinymce and configured it
I have created the following in the admin.py of the model with the field I want to add tinymce to:
class FooAdminForm(forms.ModelForm):
class Meta:
model = Foo
def __init__(self, *args, **kwards):
self.bar = forms.TextField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
super(FooAdminForm, self).__init__(*args, **kwargs)
Unfortunately this still isn't working
Right, if anyone is looking to do this:
First make sure the tinymce settings are correct:
TINYMCE_JS_ROOT = '/media/tiny_mce/'
TINYMCE_JS_URL = os.path.join(MEDIA_URL, "tiny_mce/tiny_mce_src.js")
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
}
TINYMCE_SPELLCHECKER = True
Then in the admins.py of your model
from django.forms import *
from django.db.models import *
from tinymce.widgets import TinyMCE
class ProductionForm(forms.ModelForm):
some_field = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
class Meta:
model = Production
class ProductionAdmin(admin.ModelAdmin):
form = ProductionForm
That wiki page is about five years old (!) and these days there's a much easier way of integrating TinyMCE, by simply using the django-tinymce project.
However, since you've already done it this way, you can achieve what you want with a simple change to the textareas.js script. The method described at your link uses mode: textareas, which as you note converts all textareas automatically. What you want is this:
mode: "exact",
element: "id_mytextarea",
where "id_mytextarea" is the HTML ID of the field you do want to convert - usually the name of the model field prefixed by "id_". See the TinyMCE documentation.