forms.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from ..models import MenuLink
  4. from ..cache import clear_menus_cache
  5. class MenuLinkForm(forms.ModelForm):
  6. link = forms.URLField(
  7. label=_("Link"),
  8. help_text=_("URL where the link should point to."),
  9. required=True,
  10. )
  11. title = forms.CharField(
  12. label=_("Title"), help_text=_("Title that will be used"), required=True
  13. )
  14. position = forms.ChoiceField(
  15. label=_("Position"),
  16. choices=MenuLink.LINK_POSITION_CHOICES,
  17. help_text=_("Position/s the link should be located"),
  18. )
  19. css_class = forms.CharField(
  20. label=_("CSS Class"),
  21. help_text=_(
  22. "Optional CSS class used to customize this link appearance in templates."
  23. ),
  24. required=False,
  25. )
  26. target = forms.CharField(
  27. label=_("Target"),
  28. help_text=_(
  29. "Optional target attribute that this link will use (ex. '_blank')."
  30. ),
  31. required=False,
  32. )
  33. rel = forms.CharField(
  34. label=_("Rel"),
  35. help_text=_("Optional rel attribute that this link will use (ex. 'nofollow')."),
  36. required=False,
  37. )
  38. class Meta:
  39. model = MenuLink
  40. fields = ["link", "title", "position", "css_class", "target", "rel"]
  41. def save(self):
  42. link = super().save()
  43. clear_menus_cache()
  44. return link