有没有一种方法可以删除从Django textarea中添加到markdown文件中的行? [英] Is there a way to remove lines being added to markdown file from django textarea?

查看:45
本文介绍了有没有一种方法可以删除从Django textarea中添加到markdown文件中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建条目时,例如

# Title
This is an entry

以下文件已创建

# Title

This is an entry

当我更新此文件时,将添加更多的空格.我希望删除空格,仅在用户输入空格时显示.代码段不是完整的文件,但应提供将所有内容连接在一起的相关代码.

When I update this file, more spaces are added. I want the spaces to be removed and only show when the user inputs spaces. The code snippets are not the complete files but should provide the relevant code that connects everything together.

views.py

class NewEntryForm(forms.Form):
  title = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
  entry = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control'}))

def create(request):
    if request.method == "POST":
        print(request.POST.get('entry'))
        form = NewEntryForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data["title"].strip()
            if util.get_entry(title):
                return render(request, "encyclopedia/create.html", {
                    "form": form,
                    "exists": True,
                    "title": title
                })
            else:
                entry = form.cleaned_data["entry"]
                print(entry)
                util.save_entry(title, entry)
                return redirect("entry", title=title)
        else:
            return render(request, "encyclopedia/create.html", {
                "form": form,
                "exists": False
            })
    return render(request, "encyclopedia/create.html", {
        "form": NewEntryForm(),
        "exists": False
    })

create.html

{% block body %}
  <h2>New Entry</h2>
  <form class="entry-form" action="{% url 'create' %}" method="post">
    {% csrf_token %}
    {{ form }}
    {% if exists %}
      <p class="alert alert-danger">Entry '{{ title }}' already exists.</p>
    {% endif %}
    <input type="submit" class="btn btn-primary mt-3">
  </form>
{% endblock %}

utils.py

def save_entry(title, content):
    """
    Saves an encyclopedia entry, given its title and Markdown
    content. If an existing entry with the same title already exists,
    it is replaced.
    """
    filename = f"entries/{title}.md"
    if default_storage.exists(filename):
        default_storage.delete(filename)
    default_storage.save(filename, ContentFile(content))

推荐答案

我找到了一个更好的答案.

I found a better answer.

保存时textarea中的换行符数量翻倍

这篇关于有没有一种方法可以删除从Django textarea中添加到markdown文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆