Add rich text format functionality to django TextField

Shiva Krishna Bavandla picture Shiva Krishna Bavandla · Nov 20, 2013 · Viewed 31k times · Source

I have a django model which contains a description field:

class Product(models.Model):
    name = models.CharField(max_length="125")
    description = models.TextField()

class ProductForm(ModelForm):
    class Meta:
        model = Product
    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)
        self.fields['description'].widget.attrs = { 'placeholder':'Description', 'rows':'10'}

I am rendering it as below

<html>
  <body>
     {{form.name}}
     {{form.description | linebreaks}}  
  </body>
</html>

So with the above filter linebreaks I am able to get the data as line formatting, but now I want to give a url(www.google.co.in) in the description field, and it should be a clickable on the front end in the product detail page

How to add the rich text formatting to django description field on front end?

Answer

David picture David · Oct 31, 2017

Another way could be adding some html in the description field

<a href="yourlink.com">your link</a> 

and make it accessible on the front-end with the safe tag:

{{form.description | safe | linebreaks}}

Be sure of placing the safe tag first, otherwise won't work