user1524855
user1524855

Reputation:

The view didn't return an HttpResponse object

This is the error I am getting: The view myapp.views.view_page didn't return an HttpResponse object

Can anyone see what I'm doing wrong here? I can't seem to figure out why I would be getting that exception since I am returning an HttpResponseRedirect.

views.py

from myapp.models import Page
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import Template, RequestContext
from django.core.context_processors import csrf

def view_page(request, page_name):
        try:
                    page = Page.objects.get(pk=page_name)
        except Page.DoesNotExist:
                    return render_to_response("create.html", {"page_name" :page_name},context_instance=RequestContext(request))

                    content=page.content
                    return render_to_response("view.html", {"page_name" :page_name , "content" :content},context_instance=RequestContext(request))

def edit_page(request, page_name):
        try:
                    page = Page.objects.get(pk=page_name)
                    content=page.content
        except Page.DoesNotExist:
                    content = ""
                    return render_to_response("edit.html", {"page_name" :page_name, "content" :content},context_instance=RequestContext(request))         

def save_page(request, page_name):
                content = request.POST["content"]
        try:
                    page = Page.objects.get(pk=page_name)
                    page.content=content

        except Page.DoesNotExist:
                    page = Page(name=page_name, content=content)
                    page.save()
                    return HttpResponseRedirect("/myproject/" + page_name + "/")

Upvotes: 0

Views: 2318

Answers (3)

Jeremy D
Jeremy D

Reputation: 4855

Your indentation is simply wrong. I don't know if you have resolved your problem, but as I said in the first comment one minute after you asked, you should do the following :

  • Comment everything except one method
  • For example keep the method view_page
  • Look closely to the try
  • Think about the following : Am I returning something in all cases?
    • Am I returning something on the try clause?
    • Am I returning something on my except clause?
    • My indentation is maybe wrong?

If you look closely at your code you will find it ? :)

Upvotes: 1

Muhammad Rizwan
Muhammad Rizwan

Reputation: 11

Your view_page function for view is

def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
        content=page.content
    except Page.DoesNotExist:
        content=None
    return render_to_response("view.html",{"page_name": page_name,
                                           "content": content},
                              context_instance=RequestContext(request))

Similarly the edit and save method is

def edit_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
        content=page.content
    except Page.DoesNotExist:
        content = ""
    return render_to_response("edit.html", {"page_name": page_name,
                                            "content": content},
                              context_instance=RequestContext(request))         

def save_page(request, page_name):
    content = request.POST["content"]
    try:
        page = Page.objects.get(pk=page_name)
        page.content=content
    except Page.DoesNotExist:
        page = Page(name=page_name, content=content)
    page.save()
    return HttpResponseRedirect("/myproject/" + page_name + "/")

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174718

You had an execution path that didn't return anything - when the exception was not raised. The indentation that you had was putting both return statements in the except block; effectively never executing the second return:

from django.shortcuts import render

def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
    except Page.DoesNotExist:
        return render(request,"create.html",{"page_name" :page_name})

    content=page.content
    return render(request,"view.html",{"page_name":page_name,"content":content})

You need to do the same with your other methods as well:

def edit_page(request, page_name):

    try:
        page = Page.objects.get(pk=page_name)
        content=page.content
    except Page.DoesNotExist:
        content = ""
    return render(request,"edit.html", {"page_name":page_name,"content":content})

def save_page(request, page_name):

    content = request.POST["content"]
    page,_ = Page.objects.get_or_create(pk=page_name)
    page.content = content
    page.save()
    return HttpResponseRedirect("/myproject/" + page_name + "/")

I am using the render shortcut which automatically includes RequestContext.

Upvotes: 1

Related Questions