forms.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from ...admin.forms import YesNoSwitch
  4. from ..models import MenuItem
  5. from ..cache import clear_menus_cache
  6. class MenuItemForm(forms.ModelForm):
  7. title = forms.CharField(label=_("Title"))
  8. url = forms.URLField(
  9. label=_("URL"), help_text=_("URL where this item will point to.")
  10. )
  11. menu = forms.ChoiceField(
  12. label=_("Menu"),
  13. choices=MenuItem.MENU_CHOICES,
  14. help_text=_("Menu in which this item will be displayed."),
  15. )
  16. css_class = forms.CharField(
  17. label=_("CSS class"),
  18. help_text=_('If you want to set custom value for link\'s "class".'),
  19. required=False,
  20. )
  21. target_blank = YesNoSwitch(
  22. label=_("Open this link in new window"),
  23. help_text=_(
  24. 'Enabling this option will result in the target="_blank" attribute being '
  25. "added to this link's HTML element."
  26. ),
  27. required=False,
  28. )
  29. rel = forms.CharField(
  30. label=_("Rel attribute"),
  31. help_text=_(
  32. 'Optional "rel" attribute that this item will use (ex. "nofollow").'
  33. ),
  34. required=False,
  35. )
  36. class Meta:
  37. model = MenuItem
  38. fields = ["title", "url", "menu", "css_class", "target_blank", "rel"]
  39. def save(self):
  40. item = super().save()
  41. clear_menus_cache()
  42. return item