Я получаю valueError, где post_edit возвращает None вместо объекта HttpResponse.
Это мой views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect, request
from django.urls import reverse
from markdown import Markdown
from django import forms
from . import util
md = Markdown()
class createPage(forms.Form):
title = forms.CharField(label="Title", widget=forms.TextInput(attrs={'cols': 60}))
description = forms.CharField(label="Description", widget=forms.Textarea(attrs={'cols': 120}))
def pages(request, name):
page_name = util.get_entry(name)
if page_name is None:
return render(request, "encyclopedia/invalidPage.html", {
"title": name.capitalize()
})
return render(request, "encyclopedia/wikiPages.html", {
"page": md.convert(page_name),
"title": name
})
def post_edit(request, name):
if request.method == "POST":
form = createPage(request.POST)
if form.is_valid():
description = request.POST.get('description')
title = form.cleaned_data["title"]
description = form.cleaned_data["description"]
util.save_entry(title, description)
return HttpResponseRedirect(reverse('pages', kwargs={'name': title }))
else:
description = {
'title': name,
'description':util.get_entry(name)
}
return render(request, "encyclopedia/edit.html", description)
My edit. html
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }} | Edit
{% endblock %}
{% block body %}
{{message}}
<form action="{% url 'post_edit' title %}" method="POST">
{% csrf_token %}
<table>
<tr>
<th><label for="title">Title:</label></th>
<td><input type="text" name="title" cols="60" required disabled value="{{title}}" id="title"></td>
</tr>
<tr>
<th><label for="description">Description:</label></th>
<td>
<textarea name="description" cols="120" rows="10" required id="id_content">{{ Description | linebreaksbr}}</textarea>
</td>
</tr>
</table>
<input type="submit" value="Edit">
</form>
{% endblock %}
My urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("search", views.search, name="search"),
path("wikiPLUS/new/", views.post_new, name="post_new"),
path('wikiPLUS/edit/<str:name>', views.post_edit, name='post_edit'),
path("<str:name>", views.pages, name="pages"),
]
Я получаю сообщение об ошибке: Энциклопедия просмотра. views.post_edit не вернул объект HttpResponse. Вместо этого он вернул None.
Я новичок в Django. Я надеюсь, что смогу предоставить соответствующую информацию для вопросов и ошибок.
Edit Я применил следующие изменения к функции post_edit, и она сработала:
def post_edit(request, name):
if request.method == "POST":
form = createPage(request.POST)
description = request.POST.get('description')
util.save_entry(name, description)
return HttpResponseRedirect(reverse('pages', kwargs={'name': name }))
else:
form = createPage() # this will create empty form if it is a GET request
description = {
'title': name,
'description': util.get_entry(name)
}
return render(request, "encyclopedia/edit.html", description)