0

Everything is working properly in terms of editing and redirects, however there is a strange occurence where multiple extra empty lines are added between each line of the markdown content everytime the edit page is called. This makes editing difficult, as there are many blank lines between each line of content in the page.

Edit: Apparently \r is added to the lines, but I still dont understand what is happening exactly: https://www.reddit.com/r/cs50/comments/j4l64f/cs50web_project1_wiki_extra_line_breaks_being/ provides one solution.

The relevant code is below:

views.py

class EditPageForm(forms.Form):
    markdown = forms.CharField(label="Edit Page Content", widget=forms.Textarea(attrs={'class': "form- 
               control"}), required=False)

def edit(request, title):
    if request.method == 'GET':
        initial = {"markdown":util.get_entry(title.capitalize())}
        return render(request, 'encyclopedia/edit.html', {
            "title": title.capitalize(),
            "form": EditPageForm(initial = initial),
            "error": None
        })
    form = EditPageForm(request.POST)
    if form.is_valid():
        markdown = form.cleaned_data["markdown"]
        util.save_entry(title, markdown)
        return redirect('wikipage', title = title)
    else:
        return render(request, 'encyclopedia/edit.html', {
            "form": form,
            "error": "Something went wrong."
            })

edit.html

{% extends "encyclopedia/layout.html" %}

{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}
    <h1>Edit {{title}}</h1>
    <form  method="post" class="form-group col-md-10">
        {% csrf_token %}
        {{form.markdown.label}}
        {{form.markdown}}
        <input type="submit" class="btn btn-primary">
    </form>
    {% if error != None %}
    <div class="alert alert-danger">
        {{error}}
    </div>
    {% endif %}
{% endblock %}
1
  • The link you posted has the perfect answer – Vsjain Jan 26 at 12:04

You must log in to answer this question.

Browse other questions tagged .