How to automatically convert a URL string to a clickable link in a template?

Calendar 👁️ 68

In website content operation, we often encounter such situations: articles, summaries, or comments contain some URL strings that can be seen by users but cannot be directly clicked to jump, which not only affects the user experience but also reduces the readability and practicality of the content.As an AnQiCMS user, you will find that the system provides us with a graceful and efficient solution that can easily convert these ordinary URL strings into clickable links.

AnQiCMS's template engine is powerful and flexible, it draws on the concise syntax of Django templates and has built-in many practical filters (filters).Today, let's talk about how to use these filters to make the URL string in your template 'alive'.

urlizeFilter: Generate links automatically

AnQiCMS provides us with a filter namedurlizewhich is a powerful filter, its function is to intelligently identify URLs in the text (such as those starting withhttp:///https:///www.URLs starting with, even common email addresses), and automatically wrap them up<a href="...">tags to make them clickable.

This filter can not only convert links into clickable forms, but also generate<a>automatically added in the tag.rel="nofollow"Property. This is very important for the website's SEO strategy, it can help you control the flow of the website's 'weight', avoiding unnecessary transfer of weight to external links, and focusing better on the optimization of its own content.

Let's look at a simple example, assuming that your article content includes a website address and email:

{# 假设原始文本中包含URL和邮箱地址 #}
{% set content_string = "欢迎访问安企CMS官网:https://en.anqicms.com,您也可以发送邮件到 [email protected] 联系我们。" %}

<p>原始文本:</p>
<p>{{ content_string }}</p>

<p>应用urlize过滤器后:</p>
<p>{{ content_string|urlize|safe }}</p>

In the code above,{{ content_string }}When output directly, URLs and emails are plain text. But when you apply|urlizeAfter the filter,https://en.anqicms.comand[email protected]will be automatically recognized and converted into clickable links, such as:

<a href="https://en.anqicms.com" rel="nofollow">https://en.anqicms.com</a> <a href="mailto:[email protected]">[email protected]</a>

urlizetruncFilter: Elegant truncation of long links

Sometimes, some URLs can be very long, and displaying them completely can affect the aesthetics and layout of the page, even causing layout to go wrong. At this point,urlizetruncfilters come into play. It isurlizeBased on that, a link text truncation feature has been added, you can specify a length, and the link text will be truncated if it exceeds that length, and it will end with...while maintaining the validity of the link.

urlizetruncThe filter needs a parameter to specify the truncation length. For example, if you want the link text to display at most 30 characters, you can use it like this:

{# 假设有一个非常长的URL #}
{% set long_url_content = "这里有一个非常长的链接,如果您感兴趣可以点击查看:https://en.anqicms.com/category/1/this-is-a-very-long-article-title-with-many-words-and-some-special-characters-to-demonstrate-truncation" %}

<p>原始长链接:</p>
<p>{{ long_url_content }}</p>

<p>应用urlizetrunc过滤器并截断为30个字符后:</p>
<p>{{ long_url_content|urlizetrunc:30|safe }}</p>

after|urlizetrunc:30The content displayed in the browser after processing may look like this:

这里有一个非常长的链接,如果您感兴趣可以点击查看:<a href="https://en.anqicms.com/category/1/this-is-a-very-long-article-title-with-many-words-and-some-special-characters-to-demonstrate-truncation" rel="nofollow">https://en.anqicms.com/c...</a>

The link is still complete, but the text displayed on the page has been cleverly shortened, saving space and improving visual neatness.

about|safeImportant tips for the filter

In the above example, you may have noticed that I addedurlizeandurlizetruncafter|safeFilter. This is an important security mechanism of the AnQiCMS template engine.By default, to prevent cross-site scripting (XSS) attacks, the template engine automatically escapes all output HTML tags, which means<Will become&lt;,>Will become&gt;etc.

urlizeandurlizetruncThe filter generates exactly HTML formatted<a>tags. If missing|safefilters, these<a>The tag will be escaped into plain text, thus it cannot be parsed by the browser as a clickable link.

Use|safeA filter is essentially telling the template engine: "This content is safe, please do not escape it, and parse it directly as HTML output." However, this also means that you need to ensure that the content being filtered is safe.|safeThe content source is trustworthy, otherwise it may pose a security risk. It is usually safe to handle content from the AnQiCMS system or strictly reviewed user content.

Useful scenarios and additional considerations

These filters are very useful in various content operation scenarios:

  • User comments and messages:The URL or email pasted in the comment can be automatically turned into a clickable link, convenient for interaction.
  • Product or service description:If your product or service description needs to guide users to access an external resource or download link.
  • Custom field content:In some custom fields, URLs are stored and it is desired that they automatically become links when displayed on the front end.
  • Article summary or list:When displaying URLs in article lists or summaries, they can be concise and functional.

It should be noted that if your content is already written through a Markdown editor and the Markdown rendering is configured on the front end, then Markdown syntax (such as[文本](链接)or directly pastehttp://Links are usually automatically converted to clickable links, in which case you do not need to use anything extraurlizeorurlizetruncFilters. These filters are mainly used to process those URL strings that exist in pure text format.

By flexible applicationurlizeandurlizetruncThe filter, you can significantly improve the interactivity and user experience of your website content, and also do better in SEO.With the help of AnQiCMS, make your website content more attractive and easier to operate.


Frequently Asked Questions (FAQ)

1. Why did I useurlizeFilters, but the link is still plain text and cannot be clicked?This is likely because you forgot tourlizeAdd after the filter|safeFilter. The AnQiCMS template engine defaults to escaping all HTML tags to ensure safety.urlizeandurlizetruncThe generated is HTML.<a>Tags, if not using|safeTell the template engine that this HTML is safe and can be rendered directly, it will be escaped into plain text.

2.urlizeWhat types of URLs and email addresses does the filter automatically recognize? urlizeFilters can usually identify URLs that start withhttp:///https:///ftp:///ftps://complete URLs that start with as well as URLs thatwww.start with but do not have a protocol part. For email addresses, it recognizes standard email address formats such as[email protected]) and convert them tomailto:links.

3. My article content is published using Markdown editor, and it also needs to useurlizefilter?Generally not required. If your AnQiCMS backend has enabled the Markdown editor and the front end is also configured with the corresponding Markdown rendering (for example inarchiveDetailused in therender=trueParameters), then the Markdown syntax itself supports converting links and email addresses into clickable forms (such as[AnQiCMS官网](https://en.anqicms.com)or directly pastedhttps://en.anqicms.com)urlizeandurlizetruncThe filter is mainly used to process those URL strings stored in the database in plain text format and not parsed by Markdown.

Related articles

How to convert newline characters in multi-line text to HTML tags in AnqiCMS?

In website content operation, we often need to input multi-line text in articles, product descriptions, or page content.However, when displaying text with line breaks directly on a web page, browsers often do not retain these line breaks as expected, instead rendering them as a single continuous line of text, which undoubtedly affects the reading experience and layout of the content.AnQi CMS understands this and provides very convenient built-in functions to help us easily convert newline characters in multi-line text to corresponding HTML tags, allowing the content to be perfectly presented on the front-end page.###

2025-11-08

How to truncate strings or HTML content in templates and add ellipses?

When operating a website, we often encounter such situations: the summary of an article is too long, causing the page layout to be disordered;The product description is detailed but seems redundant on the list page;Or it might be a title that is too long, taking up space for other elements.These seemingly minor issues can affect the user experience and the overall beauty of the page.Fortunately, AnQiCMS (AnQiCMS) has provided us with very convenient and powerful template tags, which can easily solve the needs of content truncation and adding ellipses, making our website content display more appropriate and professional

2025-11-08

How to convert string case or capitalize the first letter in SafeCMS?

In website content operation, the way text is presented often affects the user's reading experience and brand image.In order to unify brand style, improve the readability of the title, or highlight text in specific scenarios, it is a common requirement to convert strings to uppercase or capitalize the first letter.AnQiCMS (AnQiCMS) with its flexible Django template engine syntax, provides us with several concise and efficient methods for handling these string formatting tasks.These operations do not require complex backend code modifications, just apply the corresponding filters in the template file, and you can easily achieve it

2025-11-08

How to set custom URL aliases for articles, categories, and single pages?

On the day when content management is increasingly important, a clear and friendly URL address can not only help search engines better understand the content of the page, improve the SEO performance of the website, but also provide users with a better browsing experience, and even help in brand promotion.AnQiCMS understands this and provides a powerful and flexible custom URL alias feature, allowing you to easily set personalized addresses for articles, categories, single pages, and even tags on your website.Next, we will explore how to finely manage these URL aliases in AnQiCMS.### Core Premise

2025-11-08

How to format numbers in Anqi CMS (such as retaining decimal places for floating-point numbers)?

In website operation, the way data is displayed is crucial to user experience.Whether it is product prices, statistics, or financial statements, clear and standardized number formats can not only enhance the professionalism of the website but also help users understand information more intuitively.AnQi CMS knows this need, and provides powerful number formatting functions in the template system, allowing us to easily control the decimal places of floating-point numbers, the display style of integers, and more customized presentations of numbers.### Why is Number Formatting So Important? Imagine a e-commerce website where the price of a product is displayed as "199"

2025-11-08

How to remove leading and trailing spaces or specified characters from a string?

In website content management, string formatting is often a problem we need to solve when displaying content and handling data.Especially in user input, data import, or system-generated content, strings may have extraneous spaces at the beginning and end, or specific characters may need to be removed to meet expected display effects or data standards.AnQiCMS as an efficient and flexible content management system, through its powerful template engine and rich filters, provides us with concise and powerful string processing capabilities, allowing such tasks to be easily completed at the template level.

2025-11-08

How to perform simple number or string concatenation in AnqiCMS template?

In Anqi CMS template design, flexibly handling and displaying data is the key to building a dynamic website.This is a common need in daily development to concatenate numbers or strings.AnQi CMS provides various ways to achieve this goal, from simple direct combinations to powerful filters, which can meet your different content assembly scenarios.Understanding the Basics of Concatenation: Direct Output Equivalent to "Addition" In Anqi CMS template syntax, the most intuitive way to concatenate strings is to place multiple variables or text directly side by side. For example

2025-11-08

How to convert an array to a string with a delimiter, or split a string into an array?

In AnQi CMS template development, we often encounter situations where we need to handle data format conversion, especially when combining multiple values into a string or splitting a string containing multiple values into independent data.This is especially common when dealing with tags, multi-select properties, or extracting information from custom fields. 幸运的是,AnQi CMS built-in very practical template filters, can help us easily achieve the conversion between arrays and string with delimiters.

2025-11-08