홍의숙
홍의숙

Reputation: 317

The view didn't return an HttpResponse object when submitting a form

I've got the error below and i don't know what's the problem is. ValueError : The view bookmarks.views.bookmark_save_page didn't return an HttpResponse object. It returned None instead.

Here is the code of bookmarks.views.bookmark_save_page :

def bookmark_save_page(request):
if request.method == 'POST':
    form = BookmarkSaveForm(request.POST)
    if form.is_valid():
        link, dummy = Link.objects.get_or_create(
            url = form.cleaned_data['url']
        )
        bookmark, created = Bookmark.objects.get_or_create(
            user=request.user,
            link = link
        )
        bookmark.title = form.cleaned_data['title']

        if not created:
            bookmark.tag_set.clear()

        tag_names = form.cleaned_data['tags'].split()
        for tag_names in tag_names :
            tag, dummy = Tag.objects.get_or_create(name=tag_names)
            bookmark.tag_set.add(tag)

        bookmark.save()
        return HttpResponseRedirect(
            '/user/%s/' %request.user.username
        )
    else:
        form = BookmarkSaveForm()
    variables = RequestContext(request,{
        'form' : form
    })
    return render_to_response('bookmark_save.html', variables)

The code of 'bookmark_save.html':

{%  extends "base.html" %}
{% block title %}save your bookmark{% endblock %}
{% block head %}save your bookmark{% endblock %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="save" />
</form>
{% endblock %}

What is the problem? Did i miss something?

Upvotes: 0

Views: 1215

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

Your entire code is indented under the if condition, so when the page is initially requested, no response is sent. You need to move the last return to the same level as the initial if:

def bookmark_save_page(request):
  if request.method == 'POST':
    form = BookmarkSaveForm(request.POST)
    if form.is_valid():
        link, dummy = Link.objects.get_or_create(
            url = form.cleaned_data['url']
        )
        bookmark, created = Bookmark.objects.get_or_create(
            user=request.user,
            link = link
        )
        bookmark.title = form.cleaned_data['title']

        if not created:
            bookmark.tag_set.clear()

        tag_names = form.cleaned_data['tags'].split()
        for tag_names in tag_names :
            tag, dummy = Tag.objects.get_or_create(name=tag_names)
            bookmark.tag_set.add(tag)

        bookmark.save()
        return HttpResponseRedirect(
            '/user/%s/' %request.user.username
        )
    else:
        form = BookmarkSaveForm()
    variables = RequestContext(request,{
        'form' : form
    })
  return render_to_response('bookmark_save.html', variables)

You can also further simplify your code:

from django.shortcuts import render, redirect

def bookmark_save_request(request):
    form = BookmarkSaveForm(request.POST or None)
    if form.is_valid():
        # .. your logic here
        return redirect('/user/%s' % (request.user.username,))
    return render(request, 'bookmark_save.html', {'form': form})

Upvotes: 2

Related Questions