添加新页面功能无法正常工作。如何添加新页面?姜戈

Add new page function does not work as it should. How can I add new page? Django

我正在尝试创建具有以下功能的 Addpage 函数;

VIEWS.PY

class AddPageForm(forms.Form):
    title = forms.CharField()
    content = forms.CharField(widget=forms.Textarea(
        attrs={
            "class": "form-control",
        })
    )

def add_page(request):
    if request.method == "POST":
        form = AddPageForm(request.POST)
        
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            entries = util.list_entries()
            for entry in entries:
                if title.upper() == entry.upper():
                    return render(request, "encyclopedia/errorpage.html")
                else:
                    return redirect('encyclopedia:entrypage', title=title)
            util.save_entry(title, content)
    else:
        return render(request, "encyclopedia/addpage.html", {
            "form": AddPageForm()
        })

URL.PY

app_name = "encyclopedia"

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:title>", views.entry_page, name="entrypage"),
    path("add_page", views.add_page, name="addpage"),

ADDPAGE.HTML

<h1>Add Page</h1>
        <form action="{% url 'encyclopedia:addpage' %}" method="post">
            {% csrf_token %}
            {{ form }}
            <input type="submit" value="Submit" class="btn btn-secondary">
        </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))

def list_entries():
    """
    Returns a list of all names of encyclopedia entries.
    """
    _, filenames = default_storage.listdir("entries")
    return list(sorted(re.sub(r"\.md$", "", filename)
                for filename in filenames if filename.endswith(".md")))

LAYOUT.HTML

<div>
  <a href="{% url 'encyclopedia:addpage' %}">Create New Page</a>
</div>

我目前遇到的问题是,当我点击“添加”按钮并输入一个已经存在的标题时,例如 CSS,它应该将我带到错误页面,但它却提交了什么我输入的内容没有保存到 entry/homepage.

第二个问题是,当我单击“添加”按钮并输入一个新标题(应该提交并保存在我的主页上)时,它反而将我带到了错误页面。

你能指出我做错了什么吗?

在您的 for 循环中,它只会检查第一项,如果这不是现有条目,它将立即重定向(永远不会到达第二项)。而您可能希望它检查 all 条目,如果不存在则添加并重定向?

例如。这样效果会更好吗?

def add_page(request):
    if request.method == "POST":
        form = AddPageForm(request.POST)
        
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            entries = util.list_entries()
            for entry in entries:
                if title.upper() == entry.upper():
                    return render(request, "encyclopedia/errorpage.html")
            util.save_entry(title, content)
            return redirect('encyclopedia:entrypage', title=title)
    else:
        return render(request, "encyclopedia/addpage.html", {
            "form": AddPageForm()
        })