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)

You must log in to answer this question.

Browse other questions tagged .