Jul-02-2020, 09:31 PM
(This post was last modified: Jul-02-2020, 09:31 PM by card51shor.)
Hey guys I'm onto a new project and I'm using Django now instead of Flask. Please forgive me but sometimes I need to be prodded a bit to get started - once you guys get me going in the right direction I usually figure it out.
I'm creating a Wikipedia page and I have to make it so that the user can go to /wiki/<title> with title being a wikipedia entry like HTML or CSS and it will take them to a page of the information.
I'm getting an error when I go to the wiki/HTML (and the others). Can anyone see what I'm doing wrong?
view.py:
I'm creating a Wikipedia page and I have to make it so that the user can go to /wiki/<title> with title being a wikipedia entry like HTML or CSS and it will take them to a page of the information.
I'm getting an error when I go to the wiki/HTML (and the others). Can anyone see what I'm doing wrong?
view.py:
from django.shortcuts import render from . import util def index(request): return render(request, "encyclopedia/index.html", { "entries": util.list_entries() }) def wiki(request, title): return render(request, "encyclopedia/wiki/{title}.html", { "entries": util.get_entry(title) })url.py :
from django.urls import path from . import views urlpatterns = [ path("", views.index, name="index"), path("wiki/<str:title>", views.wiki, name="wiki"), ]util.py :
import re from django.core.files.base import ContentFile from django.core.files.storage import default_storage 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"))) 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 get_entry(title): """ Retrieves an encyclopedia entry by its title. If no such entry exists, the function returns None. """ try: f = default_storage.open(f"entries/{title}.md") return f.read().decode("utf-8") except FileNotFoundError: return None
Error:TemplateDoesNotExist at /wiki/HTML
encyclopedia/wiki/{title}.html
Request Method: GET
Request URL: http://127.0.0.1:8000/wiki/HTML
Django Version: 3.0.8
Exception Type: TemplateDoesNotExist
Exception Value:
encyclopedia/wiki/{title}.html
Exception Location: C:\Python38\lib\site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Python38\python.exe
Python Version: 3.8.2
Python Path:
['C:\\school\\project1',
'C:\\Python38\\python38.zip',
'C:\\Python38\\DLLs',
'C:\\Python38\\lib',
'C:\\Python38',
'C:\\Python38\\lib\\site-packages']
Server time: Thu, 2 Jul 2020 21:28:38 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\school\project1\encyclopedia\templates\encyclopedia\wiki\{title}.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python38\lib\site-packages\django\contrib\admin\templates\encyclopedia\wiki\{title}.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python38\lib\site-packages\django\contrib\auth\templates\encyclopedia\wiki\{title}.html (Source does not exist)
Traceback Switch to copy-and-paste view
C:\Python38\lib\site-packages\django\core\handlers\exception.py in inner
response = get_response(request)
…
▶ Local vars
C:\Python38\lib\site-packages\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request)
…
▶ Local vars
C:\Python38\lib\site-packages\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
…
▶ Local vars
C:\school\project1\encyclopedia\views.py in wiki
return render(request, "encyclopedia/wiki/{title}.html", {
…
▶ Local vars
C:\Python38\lib\site-packages\django\shortcuts.py in render
content = loader.render_to_string(template_name, context, request, using=using)
…
▶ Local vars
C:\Python38\lib\site-packages\django\template\loader.py in render_to_string
template = get_template(template_name, using=using)
…
▶ Local vars
C:\Python38\lib\site-packages\django\template\loader.py in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
…
▶ Local vars