CS50W Project 1 WIKI - django路径中某些条目的大写问题



我对Wiki的实现有一个问题,我不能简单地理解发生了什么。

目前为止我的url .py代码:

from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<str:title>", views.entries, name="entries"),
path("search/", views.search, name="search")
]

和views.py:

from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from django.http import HttpResponseRedirect
from markdown2 import Markdown
from django import forms
from . import util
# render wiki's index
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
# take path, remove capitalization and query for a matching entry
def entries(request, title):
nocase_title = title.lower()
entry = util.get_entry(nocase_title)
if entry:
# convert markdown to html and render the entry route
translator = Markdown()
html = translator.convert(entry)
return render(request, "encyclopedia/entry.html", {"entry":html, "title":nocase_title.upper()})
else:
return render(request, "encyclopedia/not_found.html")
def search(request):
return render(request, "encyclopedia/search.html")

我的问题是:在url,我不能键入一个传递到wiki/python或wiki/css全部小写。每次我尝试,我得到404问题返回给我。我没有其他条目的问题,我可以输入wiki/django, wiki/git或wiki/html....但最奇怪的是,我可以输入包含所有大写或一半大写的url。例如,如果我输入wiki/CSS wiki/CSS, wiki/CSS或wiki/CSS,所有的工作。python也是一样。我可以使用wiki/pYthon、wiki/pYthon、wiki/pYthon等等……只有当我尝试键入全小写时,我才无法访问这些条目。

我真的对这个问题感到惊讶,因为我想象不出是什么引起的。正如我之前所说的,它只发生在这两个条目模板(css和python)中,其余的工作良好(我也可以键入全小写的类似单词(例如,cs50或pythagoras),并且我没有得到404错误,在这些情况下,我收到我的自定义"未找到条目";HTML模板返回条目(请求,标题)函数。

url区分大小写,域名除外。所以我预计你会得到404错误使用"css"one_answers";cSS"(以及除大写字母外的其他字母)两者都有。我的假设是,这是关于你在浏览器中的历史记录。清除你的历史记录,然后再试一次。除此之外,为了能够防止大小写问题,请尝试以下操作:

if name.lower() in [i.lower() for i in util.list_entries()]:
content = util.get_entry(name)

最新更新