【问题标题】:Add new page function does not work as it should. How can I add new page? Django添加新页面功能无法正常工作。如何添加新页面?姜戈
【发布时间】:2022-01-02 06:16:56
【问题描述】:

我正在尝试创建具有以下功能的 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")))

布局.HTML

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

我目前遇到的问题是,当我单击“添加”按钮并输入一个已经存在的标题(例如 CSS)时,它应该将我带到错误页面,但它会提交但是我输入的内容没有得到保存到入口/主页。

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

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

【问题讨论】:

    标签: django django-views django-forms django-templates cs50


    【解决方案1】:

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

    例如。这会更好吗?

    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()
            })
    

    【讨论】:

    • 谢谢Timmering,效果很好
    猜你喜欢
    • 1970-01-01
    • 2014-09-20
    • 2019-08-03
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多