Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

CS50W Project 1 WIKI - django paths capitalization problem in some entries

I have an issue with the implementation of Wiki I can't simply understand what's happening.

My code so far for urls.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")
]

and for 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")

My problem is this: In the url, I can't type a pass to wiki/python or wiki/css all in lowercase. Everytime I try it, I get 404 problem returned to me. I don't have issue with the other entries, I can type wiki/django, wiki/git or wiki/html.... But the most strange part is that I can type urls including those words in all caps or half of it. For example, if I type wiki/CSS wiki/cSS, wiki/Css, or wiki/cSs AND ALL OF IT WORKS. The same goes for python. I can use wiki/pYthon, wiki/PYTHON, wiki/PyTHon, etc... I only can't access to those entries when trying to type in all-lowercase.

I am truly amazed about this issue because I can't image what causing it. As I told before, it only occur with those two entry templates (css and python), the rest work fine (I also can type all-lowercase similar words (for example, cs50 or pythagoras) and I don't get a 404 error, in those cases I receive my custome "entry not found" html template return by entries(request, title) function.

question from:https://stackoverflow.com/questions/66051769/cs50w-project-1-wiki-django-paths-capitalization-problem-in-some-entries

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

URLs are case sensitive except domain names. So I would expect that you get 404 error with "css" and "cSS" (and the others except uppercase one) both. My assumption is that this is about your history in your browser. Clear your history and try it again. Apart from that, to be able to prevent lowercase-uppercase issue try this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...