Django: DRY Internal links in TextFields - django

I have a site with a bunch of 'Projects' which often refer to each other in their descriptions (stored in a TextField).
Rather than hard-coding the links between projects in their descriptions, I'd like to keep things DRY by referring to them using some sort of token, for example, in the description field:
Blabla text describing this project, this project was inspired by
{{ project "ProjectB"}} and lead to the development of {{ project "ProjectC" }}.
Which is then processed and turned in to:
Blabla text describing this project, this project was inspired by
ProjectB and lead to the development
of ProjectC.
To be clear: the description is free text which can contain none to many references to other items as hyperlinks at various points in the text. In a CMS this effect is usually achieved through some way to link to items by node/object ID - so that if the link changes, the reference can still be followed.
I've considered:
Evaluating the text field as a Template and using the 'url' templatetag in descriptions. Seems like the easiest solution but that templatetag isn't particularly friendly for content editors and evaluating each description through the entire Template renderer seems a bit cumbersome.
Implementing a templatetag which just re-implements a basic faux-templating system to just parse out a nice simple tag just for this purpose.
Extending the TextField to pre-process the description before it's saved to the database.
Has anyone done anything similar? What would you suggest?

I just answered a similar question on SO, and it seems like it might solve your problem as well (if by chance you're still looking for an answer three years later).
I wrote a template filter to parse the custom internal link format in the Textfield before display. I'm using Markdown to parse my textfields, so I made the links return in Markdown format, but they could easily be written as HTML instead.
Check out my answer here.
Update: I posted a revised version on djangosnippets.org that resolves internal links inside a markdown-formatted link, as well as on their own.

if I got your problem, you should use a custom template processor to pass a dictionary to your templates:
in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"myapp.myprocessor.foo",
)
in myapp/myprocessor.py:
from django import template
def foo(request):
ProjectA = get_Project_from_database
t = template.Template(ProjectA.html)
c = template.Context({'name': ProjectA.name})
rendered_ProjectA = t.render(c)
return { 'rendered_ProjectA': rendered_ProjectA }
or if you don't wanna use Django template system you can use regular expressions (import re)

Related

How can I configure VS Code to work with Prettier HTML formatter?

I am trying to have VS Code format my Django HTML files but I am getting:
There is no document formatter for 'django-html'-files installed.
The solution I found on the web works with Beautify, not Prettier.
How can I make it work with Prettier?
#Tedkovsky's answer might technically address the error you're getting, but once you're past that, you'll see that Prettier will mangle your templates since it tries to break long lines containing template tags like {{ }} and {% %}.
This is because Prettier currently (as of 2021-01-09) doesn't support Jinja or Django templates, and for the time being, it looks like the developers aren't interested in adding this functionality. There are 2 (closed) tickets about it here:
https://github.com/prettier/prettier/issues/5581
https://github.com/prettier/prettier/issues/5754
I wasn't able to find a plugin for it either, so it doesn't look like there's a solution for using Prettier with Django templates.
edit: I've been following this thread in the Django forum about autoformatters for Django templates. Perhaps something might materialize there.
later edit: Looks like djhtml can handle indentation, although it's separate from Prettier. It doesn't do full autoformatting, though.
even later edit: djlint can also be used for formatting templates
In settings.json, try
"[django-html]": {
"editor.defaultFormatter": "prettier"
},

django - ckeditor bug renders text/string in raw html format

am using django ckeditor. Any text/content entered into its editor renders raw html output on the webpage.
for ex: this is rendered output of ckeditor field (RichTextField) on a webpage;
<p><span style="color:rgb(0, 0, 0)">this is a test file ’s forces durin</span><span style="color:rgb(0, 0, 0)">galla’s good test is one that fails Thereafter, never to fail in real environment. </span></p>
I have been looking for a solution for a long time now but unable to find one :( There are some questions which are similar but none of those have been able to help. It will be helpful if any changes suggested are provided with the exact location where it needs to be changed. Needless to say I am a newbie.
Thanks
You need to mark the relevant variable that contains the html snippet in your template as safe
Obviously you should be sure, that the text comes from trusted users and is safe, because with the safe filter you are disabling a security feature (autoescaping) that Django applies per default.
If your ckeditor is part of a comment form and your mark the entered text as safe, anybody with access to the form could inject Javascipt and other (potentially nasty) stuff in your page.
The whole story is explained pretty well in the official docs: https://docs.djangoproject.com/en/dev/topics/templates/#automatic-html-escaping

Joomla Load Module in Featured Article

What I use:
- Joomla 2.5
- gantry framework
Need:
I must place in my homepage a slideshow module inside the mainbody as a featured article.
What I did:
Installed a slideshow module and placed temporary inside gantry's maintop-a position to see if it worked (it did), then moved in a non-previously-exsisting 'slideshow' position.
Created a new article, with this content: {loadposition slideshow} and set it to featured state.
Result:
the article was published correctly but the introtext char limit trimmed the actual module code to 100 chars making it useless.
What I already tried to fix the issue:
Modified my slideshow article from database adding the loadmodule code in the fulltext field (which was originally empty),
then modified components/com_content/view/featured/tmpl/default_item.php replacing echo $this->item->introtext; with an if to identify my slideshow article id then query the database to find the fulltext field and echo it. this thingy works but the loadmodule function isn't, so it's displayed as simple HTML. I deduce that introtext is treated differently than fulltext since what's inside the brackets is interpreted as code only when echoed as introtext, and I miss that layer.
Modified modules/mod_articles_category/helper.php to break the 100 introtext_limit, sadly finding that's not called for my featured articles (added dump($item, 'some name') which returns only the articles that are inside my news sidebar, even if all my featured articles including slideshow are categorized)
tried a million different combinations of the above (i.e.:{loadmodule slideshow}{module [myslideshow_article_id]} <- a module loader plugin i tried, ...) which are too long to put here.
Searched the whole project folder for files containing 'introtext' inside their code, finding alot, but nothing that actually trims it (except helper.php of course)
Searched the Joomla API
Googled for everything that came into my mind finding no working solutions.
Came Here :)
Thanks
If you want to show a module inside an artice, you can use an component to show them there.
In my experience, this works on normal and featured articles, so this might work for you:
http://www.nonumber.nl/extensions/modulesanywhere
I hope this was helpfull, since this is my first answer here.
Kind Regards,
McBurgerKong

Getting markdown and urlize template tags to play nice

I'm using markdown to format some comments in a Django app.
If I try to combine markdown and urlize, inevitably bad formatting errors happen (links get added where they don't belong or aren't recognized, and of course the errors change depending on which filter I use first).
Basically I'd like a filter that does markdown and automatically turns links into hyperlinks if not done so by markdown.
Otherwise, I suppose I'll have to roll my own filter, which I would so rather not do.
What I do is use the Markdown urlize extension.
Once installed, you can use it in a Django template like this:
{{ value|markdown:"urlize" }}
Or in Python code like this:
import markdown
md = markdown.Markdown(safe_mode=True, extensions=['urlize'])
converted_text = md.convert(text)
Here is the start of the Markdown extension docs in case you need more info.

Customizing Containable Content in Orchard CMS

I am currently trying to understand a bit more about how Orchard handles Lists of Custom Content Types and I have run into a bit of an issue.
I created a Content Type named Story, which has the following parts:
Body
Common
Containable
Route
I created a list that holds these items, and all I am attempting to do is style them in such a way:
Story Title
Story Description (Basically a truncated version of the body?)
However, I cannot seem to figure out how to do the following:
Get the Title to actually appear (Currently all that appears is the body and a more link)
Remove the "more" link (and change this to be the actual Title)
I have looked into changing the Placement.info, and have looked all over in an attempt to find where the "more" link is added in each of the items. Any help would be greatly appreciated.
I finally managed to figure it out - Thanks to the Designer Tools Module, which made it very simple to go look into what was going on behind the scenes during Page Generation.
Basically - all that was necessary to accomplish this was to make some minor changes to the Parts.Common.Body.Summary.cshtml file. (found via ../Core/Common/Views/)
Which initially resembles the following:
#{
[~.ContentItem] contentItem = Model.ContentPart.ContentItem;
string bodyHtml = Model.Html.ToString();
var body = new HtmlString(Html.Excerpt(bodyHtml, 200).ToString()
.Replace(Environment.NewLine,"</p>"+Environment.NewLine+"<p>"));
}
<p>#body #Html.ItemDisplayLink(T("more").ToString(), contentItem)</p>
however by making a few changes (by using the Designer Tools) I change it into the following:
#{
[~.ContentItem] contentItem = Model.ContentPart.ContentItem;
string bodyHtml = Model.Html.ToString();
string title = Model.ContentPart.ContentItem.RoutePart.Title;
string summary = Html.Excerpt(bodyHtml, 100) + "...";
}
<div class='story'>
<p>
#Html.ItemDisplayLink(title, contentItem)
</p>
<summary>
#summary
</summary>
</div>
Although it could easily be shortened a bit - It does make the styling quite a big easier to handle. Anyways - I hope this helps :)
Alternately you can use the placement.info file in your theme assign different fields to your Summary and Detail views. It's much simplier.
http://orchardproject.net/docs/Understanding-placement-info.ashx
But, I used the same method you did till I discovered the .info file as well. It works and gives you a good understanding of how the system works, but the placement.info file seems easier.
Also, you probably don't want to be editing the view files in Core. I think your meant to override views in your theme directory.