django form is adding extra spaces everytime content is 'edited'

Function ‘title’ fetch the initial value of ‘textarea’ in a form, edit it and should ‘save’ it.
Information is saved but everytime a new line is added and form gets bigger and bigger.

Here is the HTML code.

<form action="{% url 'encyclopedia:title' title=title  %}" method="POST">
    {% csrf_token %}
<input type="submit" value="Save">

Function definitions in view:

from django.shortcuts import render
import random
from . import util
from django.urls import reverse
from django.http import HttpResponseRedirect
from markdown2 import Markdown
from django import forms
from django.http import HttpResponse
from django.http import request

markdowner = Markdown()

class CreateForm(forms.Form):
    Title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Title'}))
    Content = forms.CharField(widget=forms.Textarea(attrs={"rows":3, "cols":10}))

class Edit(forms.Form):
    textarea = forms.CharField(widget=forms.Textarea(), label='')

def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()

#Present the page as per the title in the url
def title(request,title):
    if request.method == 'POST':
        form = Edit(request.POST) 
        if form.is_valid():
            textarea = form.cleaned_data["textarea"]
        return HttpResponseRedirect(reverse("encyclopedia:title", args=(title,)))
        return render(request,"encyclopedia/title.html",{"Content": markdowner.convert(util.get_entry(title)),"title":title})

def create(request):
    if request.method == 'POST':
        form = CreateForm(request.POST)
        if form.is_valid():
            Title = form.cleaned_data["Title"]
            Content = form.cleaned_data["Content"]
            if Title.lower() in util.list_entries() or Title.upper() in util.list_entries() or Title in util.list_entries():
                return render(request,"encyclopedia/title.html",{"Content": "Title already exists","error":True})

                title= Title
                #return render(request,"encyclopedia/title.html",{"Title": Title, "Content": page_html, "error":False})
                return HttpResponseRedirect(reverse("encyclopedia:title", args=(title,)))
        form = CreateForm()
        return render(request,"encyclopedia/create.html",{"form": form})

def search(request):
    sval = request.POST.get("q")
    return render(request,"encyclopedia/Search.html",{"entries":[s for s in util.list_entries() if sval.lower() in s.lower()]})
def edit(request,title):
    if request.method == 'POST':
        #x = markdowner.convert(util.get_entry(title))     
        x = util.get_entry(title) 
        form = Edit(initial={'textarea': x})
        return render(request,"encyclopedia/edit.html",{"form":form,"title": title})  

def randomPage(request):
    if request.method == 'GET':
        entries = util.list_entries()
        num = random.randint(0, len(entries) - 1)
        title = entries[num]

        return render(request,"encyclopedia/title.html",{"Content": markdowner.convert(util.get_entry(title)),"title":title})

1 Like

This is a bit difficult to follow what you’ve tried to post here. There are a couple things you can do that would help.

  • When you’re posting fragments of code or templates, enclose them between lines consisting only of three backtick - ` characters. This means you would have one line that is only ```, then your lines from the file, then another line that is just ```. (I can see where you’re using the backticks, but they need to be on lines by themselves.
  • Put the name of the file above the fragment.
  • Please include the form
  • Please confirm that you have posted the complete view.


Thank you for instant reply. I have updated the code.

What is this function?

That function save the information written in the form (textarea) in the given ‘title’ of the form. The function does save the information now. However, it is adding additional lines/paragraphs in the html view. It means, form in the html gets bigger and bigger evrytime i modify the content of the ‘textarea’

There’s still too much that you’re not showing here. Please post all the code / templates / functions / models etc that are involved in this process.
Do not try to trim or create excerpts - we need to be able to see what is happening to try and diagnose this.

Either that, or create a smaller, complete working sample that demonstrates the effects you’re seeing.

Thank you for the feedback. I have posted whole ‘’ file.
The web application search for a given content based on its title. when content is displayed to the user, it has an option of ‘Edit’ and ‘Save’.

I was having the same issue but I believe I found a solution.

Thank you very much!