0

I got stuck at completing Wiki assignment because of strange behaviour of textarea in form, needed for creating and editing entries. Everytime user saves the entry, for each line of entered text there are new line breaks that added automatically. I can see in browser's developer console that POST request is sending correct text without new line breaks, but they ("\r\n") are created in saved .md files right on the next step. Please, tell me what could be reason of that. I can't see in specification any requirements mentioning this, but I think it's not OK. New line breaks are addin both in new and edit methods. Could markdown2 conversion be the reason of this? Input Output

Entry.html

{% block body %}

    {{ entry|safe }}
    <form action="/edit" method="GET">
        <input type="hidden" name="title" value="{{ title }}">
        <input type="submit" value="Edit">
    </form>

{% endblock %}

New.html

{% block body %}

<h1>Create New Page</h1>
<form action="{% url 'new' %}" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Save">
</form>

{% endblock %}

Edit.html

{% block body %}

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

{% endblock %}

Views.py

class NewPageForm(forms.Form):
    title = forms.CharField()
    content = forms.CharField(widget=forms.Textarea)
class EditPageForm(forms.Form):
    title = forms.CharField(widget=forms.HiddenInput)
    content = forms.CharField(widget=forms.Textarea)


def new(request):
    if request.method == "POST":
        form = NewPageForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = "# " + title + "\n" + form.cleaned_data['content']
            if title not in util.list_entries():
                util.save_entry(title, content)
                return HttpResponseRedirect("/wiki/" + title)
            else:
                return render(request, "encyclopedia/entry.html", {
                "entry": "Entry for " + title + " already exists!",
                "title": "Error"
                })
    else:
        return render(request, "encyclopedia/new.html", {
            "form": NewPageForm(),
            "title": "Create New Page"
        })

def edit(request):
    if request.method == "GET":
        title = request.GET['title']
        content = util.get_entry(title)
        form = EditPageForm(initial={'content': content, 'title': title})
        return render(request, "encyclopedia/edit.html", {
        "form": form,
        "title": "Edit Page: " + title
        })
    else:
        form = EditPageForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            util.save_entry(title, content)
            return HttpResponseRedirect("/wiki/" + title)
1
  • Hello, @Hellbartonio Did you manage to solve this? I faced with the same problem and have no idea how to solve it. I would appreciate any feedback.
    – Kirill N
    Feb 17 at 7:11
0

I hit the same issue and found the solution. I thought I'd post it here in case others hit the same problem.

The solution is here: https://stackoverflow.com/questions/62903909/django-contentfile-unexpected-empty-line-django-core-files-base

Short summary: you need to encode the content to ascii by doing .encode('ascii') - you can either do this in the save_entry function in util.py (which is what I did), or do it as you pass content to util.save_entry.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .