0
votes

I have been getting the valueError where post_edit returns None instead of an HttpResponse object.

This is my 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"),
    
]

I have been getting the error: The view encyclopedia.views.post_edit didn't return an HttpResponse object. It returned None instead.

I am very new to Django. I hope I could provide relevant information for question and error.

Edit I applied the following changes on post_edit function then it worked:


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)


1
You may not need that else.... clause thereJPG
I tried deleting the else clause, save and ran. but the error still persistsLucifer

1 Answers

0
votes

In post_edit function add:

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)

Indentation of description(your context dictionary) and render should be at same as else.

For more info. like when this errors usually occurs, you may check these answers The view didn't return an HttpResponse object. It returned None instead