Click here to Skip to main content
15,668,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to return a bound form that has been modified and has some arbitrary text and HTML inserted into it. I have done some research and have been able to successfully insert some arbitrary text into a bound form but I haven't found any way to render the injected HTML as HTML. It renders as plain text. How can I achieve my goal?

Here is my code:


def multi_text(request):
    if request.method == 'POST':
        data = request.POST.copy()
        form = MultilineForm(data=data)
        if form.is_valid():
            cd = form.cleaned_data
  ['text'] = 'Hello hello'
            return render(request, 'multi_text.html', {'form': form})
        form = MultilineForm()
    return render(request, 'multi_text.html', {'form': form})


class MultilineForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['text'].widget.attrs.update({'class': 'form-control'}, verbose_name='Text', placeholder='Type your text here...')['text'] = '...'
    class Meta:
        model = Multiline
        fields = ['text']
        widgets = {
            'text': Textarea(attrs={}),

# template.html

<form method="post" action="" class="form">
        {% csrf_token %}
        {{ form.text.as_widget }}
        <span class="input-group-btn">
        <input type="submit" value="Check" class="form-control btn btn-primary">

What I have tried:

I have trired safe tag but it works on the fields, not the whole form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900