Advertisement
Wilfred-kun

django tinymce struggles

Sep 25th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. # admin.py
  2. from django.contrib import admin
  3. from tinymce.widgets import TinyMCE
  4. from django.db import models
  5. from .models import User, Announcement
  6.  
  7. class AnnouncementAdmin(admin.ModelAdmin):
  8.     fields = ["title",
  9.               "content",
  10.               "author",
  11.               "published"
  12.             ]
  13.  
  14.     formfield_overrides = {
  15.             models.TextField: {'widget': TinyMCE()}
  16.         }
  17.  
  18. admin.site.register(User)
  19. admin.site.register(Announcement, AnnouncementAdmin)
  20.  
  21. # models.py
  22. from django.db import models
  23. from django.utils import timezone
  24.  
  25. class User(models.Model):
  26.     username = models.CharField(max_length=30)
  27.  
  28.     def __str__(self):
  29.         return self.username
  30.  
  31. class Announcement(models.Model):
  32.     title = models.CharField(max_length=50)
  33.     content = models.TextField()
  34.     author = models.CharField(max_length=30)
  35.     published = models.DateTimeField('date published', default=timezone.now)
  36.  
  37.     def __str__(self):
  38.         return self.title
  39.  
  40. # settings.py
  41. INSTALLED_APPS = [
  42.     ...
  43.     'tinymce',
  44.     ...
  45. ]
  46. ...
  47. # tinymce settings
  48. TINYMCE_DEFAULT_CONFIG = {
  49.     'height': 360,
  50.     'width': 1120,
  51.     'cleanup_on_startup': True,
  52.     'custom_undo_redo_levels': 20,
  53.     'selector': 'textarea',
  54.     'theme': 'modern',
  55.     'plugins': '''
  56.            textcolor save link image media preview codesample contextmenu
  57.            table code lists fullscreen  insertdatetime  nonbreaking
  58.            contextmenu directionality searchreplace wordcount visualblocks
  59.            visualchars code fullscreen autolink lists  charmap print  hr
  60.            anchor pagebreak
  61.            ''',
  62.     'toolbar1': '''
  63.            fullscreen preview bold italic underline | fontselect,
  64.            fontsizeselect  | forecolor backcolor | alignleft alignright |
  65.            aligncenter alignjustify | indent outdent | bullist numlist table |
  66.            | link image media | codesample |
  67.            ''',
  68.     'toolbar2': '''
  69.            visualblocks visualchars |
  70.            charmap hr pagebreak nonbreaking anchor |  code |
  71.            ''',
  72.     'contextmenu': 'formats | link image',
  73.     'menubar': True,
  74.     'statusbar': True,
  75.     }
  76.  
  77. # urls.py
  78. from django.contrib import admin
  79. from django.urls import path, include
  80. from . import views
  81.  
  82. urlpatterns = [
  83.     ...
  84.     path(r'^tinymce/', include('tinymce.urls')),
  85. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement