Файл Django url вызывает неправильную функцию
Я пытаюсь вызвать функцию для записи файла, но мой Django url.py, похоже, вызывает функцию markdown. Когда вы нажимаете кнопку submit на форме в HTML, вы получаете ошибку, которая бросается на строке "if query["q"] in entries:"
HTML
<h1>New Page</h1>
<form action="{% url 'newPage'}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="t" placeholder="Title">
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea class="form-control" id="content" rows="10" name="c" placeholder="Markdown content"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
views.py
def markdown(request, entry):
pathOfFile = "entries/" + entry + ".md"
entries = util.list_entries()
query = request.POST
if request.method == "POST":
if query["q"] in entries:
return render(request, "encyclopedia/entry.html", {
"html_markdown": markdown2.markdown_path("entries/"+query["q"]+".md")
})
else:
searchResults = []
if query["q"] in entries:
for i in entries:
if query["q"] in i:
searchResults.append(i)
return render(request, "encyclopedia/search.html", {
"searchResults": searchResults
})
else:
return render(request, "encyclopedia/error.html")
else:
if (entry in entries):
return render(request, "encyclopedia/entry.html", {
"html_markdown": markdown2.markdown_path(pathOfFile)
})
else:
return render(request, "encyclopedia/error.html")
def newPage(request):
if request.method == "POST":
query = request.POST
title = query["t"]
content = query["c"]
util.save_entry(title, content)
else:
return render(request, "encyclopedia/newPage.html")
urls.py
urlpatterns = [
path("", views.index, name="index"),
path("error", views.error, name="error"),
path("newPage", views.newPage, name="newPage"),
path("<str:entry>", views.markdown, name="entry")
Выброшена ошибка:
MultiValueDictKeyError at /{% url 'newPage'}
'q'
Request Method: POST
Request URL: http://127.0.0.1:8000/%7B%25%20url%20'newPage'%7D
Django Version: 3.2.7
Exception Type: MultiValueDictKeyError
Exception Value:
'q'
Exception Location: /home/ansley/.local/lib/python3.8/site-packages/django/utils/datastructures.py, line 78, in __getitem__
Python Executable: /usr/bin/python3
Python Version: 3.8.10
Python Path:
['/home/ansley/Desktop/wiki',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/ansley/.local/lib/python3.8/site-packages',
'/usr/local/lib/python3.8/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Tue, 21 Sep 2021 00:35:25 +0000
Traceback Switch to copy-and-paste view
/home/ansley/.local/lib/python3.8/site-packages/django/utils/datastructures.py, line 76, in __getitem__
list_ = super().__getitem__(key) …
▶ Локальные переменные
During handling of the above exception ('q'), another exception occurred:
/home/ansley/.local/lib/python3.8/site-packages/django/core/handlers/exception.py, line 47, in inner
response = get_response(request) …
▶ Локальные переменные
/home/ansley/.local/lib/python3.8/site-packages/django/core/handlers/base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Локальные переменные
/home/ansley/Desktop/wiki/encyclopedia/views.py, line 19, in markdown
if query["q"] in entries: …
▶ Локальные переменные
/home/ansley/.local/lib/python3.8/site-packages/django/utils/datastructures.py, line 78, in __getitem__
raise MultiValueDictKeyError(key) …
▶ Локальные переменные