Package: linkit
🔗 LinkItLinkIt gives you a new field typeLinkFieldto use on your models which allows you to effortlessly link to different models on your site. This could be a django-cms Page model,
a filer File or anything custom like a News model.InstallationInstall the latest version with pip and addlinkitto yourINSTALLED_APPS- and you're good to go.$ pip install LinkItUsageYou're now able to use the newLinkFieldon any of your models:fromdjango.db.modelsimportModel,CharFieldfromlinkit.model_fieldsimportLinkFieldclassFoo(Model):title=CharField(max_length=255)link=LinkField(types=['page','file','input'])# <-- Yay!If you register this model in django admin you'll get a dropdown field where yru can choose between cms pages, filer files or just a plain input field.
Your model is now able to link to any of these entities with one single field.In a template you could use this link field like this:<ahref="{{ instance.link.href }}"target="{{ instance.link.target }}">{{ instance.link.label }}</a>ConfigurationTheLinkFieldtakes some options which will define how the rendered widget looks and what options the content editor has:types: list = NoneDefines which link types are allowed (see more bellow in the section «Types»)allow_target: bool = FalseIf set to true, the widget renders a checkbox so the editor can choose the_targetof the linkallow_label: bool = TrueRenders an additonal input field so a custom label can be setallow_no_follow: bool = FalseIf set to true, the widget renders a checkbox so the editor can choose therel="nofollow"for the linkTypesOut of the Box LinkIt ships with three types: input, file, page. TheLinkTypebase class makes it easy to implement your own link type, whatever
it may be. If you want to link to another existing model in your app, e.g. News, all you need to do is register a new link type.Create a filelink_types.pyin any of your apps:fromlinkit.types.modelimportModelLinkTypeclassNewsLinkType(ModelLinkType):identifier='news'type_label='News'model=NewsRegister the new type, preferably in aAppConfigreadymethod:fromdjango.appsimportAppConfigfromdjango.utils.translationimportgettext_lazyas_classContentConfig(AppConfig):name='contents'defready(self):fromcontents.link_typesimportNewsLinkTypefromlinkit.typesimporttype_manageraslinkit_managerlinkit_manager.register(NewsLinkType)Profit! You can now create a field like this on any of your models:link = LinkField(types=['news', 'page])and link to any of your news or cms pages.Checklinkit/typesto see how the core types are implemented.EmailType exampleSay we have a totally different new type we want to implement and can't just extend from theModelLinkType. See the example bellow
of a link type used to link to e-mail addresses with a optional subject field.classEmailTypeForm(TypeForm):address=EmailField(label='E-Mail',required=True)subject=CharField(label='Subject',required=False,max_length=20)classEmailType(LinkType):identifier='email'type_label='E-Mail'form_class=EmailTypeFormdefreal_value(self):returnself.link.data('value')@propertydefhref(self)->Optional[str]:mail=self.real_value().get('address')subject=self.real_value().get('subject','')returnf'mailto:{mail}?subject={subject}'@propertydeflabel(self)->Optional[str]:returnself.real_value().get('address')