John Garza
John Garza

Reputation: 94

Completely stripping certain HTML Tags in Django forms

I have a ModelForm that posts news items to a database, and it uses a javascript textarea to allow the authorized poster to insert certain pieces of HTML to style text, like bold and italics. However, since I have the template output using the "safe" filter, it outputs all the HTML the form widget tries to pass on. This includes a bothersome <br> tag that never goes away, making it so you can submit without form validation reading the field as empty and stopping you. How can I make that I can not only filter the <br> tag, but completely remove it from the data? Here is relevant code:

Models.py:

from django.db import models
from django.forms import ModelForm, forms
from django.contrib.auth.models import User
# Create your models here.

class NewsItem(models.Model):
    user = models.ForeignKey(User)
    date = models.DateField(auto_now=True)
    news = models.TextField(max_length=100000, blank=False, help_text='HELP TEXT')

    def __unicode__(self):
        return u'%s %s %s' % (self.user, self.date, self.news)

class NewsForm(ModelForm):
    class Meta:
        model = NewsItem
        exclude=('user','date',)

Views.py:

from news.models import NewsForm, NewsItem
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse

def news(request):
    if request.method == 'POST':
        item = NewsItem(user=request.user)
        form = NewsForm(request.POST, instance=item)
        if form.is_valid():
            form.save()
        return HttpResponseRedirect('/news/')
    else:
        form = NewsForm()
        news_list = NewsItem.objects.all()
        return render(request, 'news_list.html', {'news_list': news_list, 'form': form})

news_list.html:

{% extends "base.html" %}
{% block title %}News in the Corps{% endblock %}

{% block content %}
    <h2 id="page_h">News in the Corps</h2>
    {% if user.is_authenticated %}
    <h3>Post News</h3>
        <script src="{{ STATIC_URL }}nicEdit.js" type="text/javascript"></script>
        <script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>

        <div id="news_poster">
            <form id="news_poster" action="/news/" method="POST">{% csrf_token %}
            {{ form }} 
            <input type="submit" value="Submit" />
            </form>
        </div>
    {% endif %}
<ul id="events_list">
{% if news_list %}
<div id="news_list">
{% for news in news_list %}
    {% if news.id == 1 %}
    <hr />
    {% endif %}
    <div id="{{ news.id }}" class="news_item">
        <p class="poster">Posted By: {{ news.user }} | Posted On: {{  news.date }} | <a href="{% url 'news' %}#{{ news.id }}">Link</a></p>
        <div id="news_item">
        {{ news.news|safe }}
        </div> 
    </div>
    <hr />
{% endfor %}
</div>
{% endif %}
</ul>
{% endblock %}

Upvotes: 0

Views: 843

Answers (2)

Tim Selaty Jr.
Tim Selaty Jr.

Reputation: 598

I can't help but thinking that the "removetags" as Timmy O'Mahony suggested might work if it was structured like this:

{{ news.news|safe|removetags:"br"}}

Give it a shot and see if it works. I would reply, but my karma's not height enough to directly reply to an answer with a suggestion.

Upvotes: 0

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53981

You can try the removetags template filter:

{{ news.news|removetags:"br"|safe }}

Upvotes: 1

Related Questions