简体   繁体   中英

How to modify certain part of Django content by adding <span>, while keeping the original format of paragraph text?

I am trying to display a paragraph of text ( content ) from django, but I want to add <span class="modify"> to particular words (whichever matches pre-defined words in a referenceList ) in the paragraph.

here's my view.py:

def post_content(request, id=None):
    instance = get_object_or_404(post, id=id)
    content = instance.content.split()
    referenceList = ["customer","sales","service","management"]
    context = {
        "title": instance.title,
        "instance": instance,
        "content": content,
        "referenceList": referenceList
    }
    return render(request,"get_post_content.html",context)

And here is my get_post_content.html:

<!--DOCTYPE html-->
<html>
<style type="text/css">
    .modify{
        color: blue;
    }
</style>
<body>
    <h1>{{ instance.title }}</h1>
    {{ instance.author }}<br/>
    <pre class="postContent">
        {% for obj in content %} 
                {% if obj in referenceList %}
                    <span class="modify">{{ obj }}</span>
                {% else %}
                    {{ obj }}
                {% endif %}
        {% endfor %}
    </pre>  
</body> 
</html>

This is able to differentiate the words and change to the css I want it to, but it is just going through and displaying a list of strings and all the spaces and '\\n' are lost. Is there a way to keep the original format of text (with spaces and '\\n' kept)? Thanks in advance!!

>>> def add_span(arg)
>>>     return  '<span class="modify">  %s </span>'%arg.string[arg.regs[0][0]:arg.regs[0][1]] 
>>> import re
>>> r = re.compile("foo|baz")
>>> content = "Once upon a time foo met baz"
>>> print r.sub(add_span,content)
Once upon a time <span class="modify">  foo </span> met <span class="modify">  baz </span>

Then edit your view code. Use a form, to validate, since when you add span you will have to mark the tag as safe, otherwise django will escape it:

    import re
    from django import forms

    def add_span(arg)
        return  '<span class="modify">  %s </span>'%arg.string[arg.regs[0][0]:arg.regs[0][1]] 
    r = re.compile("customer|sales|service|management")

    class ValidateRefList(Form):

       content = forms.CharField()

       def save(self):
          return r.sub(add_span,self.cleaned_data['content'])

    def post_content(request, id=None):
        instance = get_object_or_404(post, id=id)
        f = ValidateRefList({'content':instance.content})
        if f.is_valid():
            fixed_content = f.save()
        else:
            # do something else
        context = {
            "title": instance.title,
            "instance": instance,
            "content": fixed_content   
        }
        return render(request,"get_post_content.html",context)

Finally, in your template, simply:

{{ content|safe }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM