2014-09-28 72 views
0

我有下面的錯誤,我不知道問題是什麼。 ValueError:視圖bookmarks.views.bookmark_save_page未返回HttpResponse對象。它返回None而不是。提交表單時視圖沒有返回HttpResponse對象

這裏是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) 

'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 %} 

問題是什麼?我錯過了什麼?

回答

2

您的整個代碼在if條件下縮進,所以當最初請求頁面時,不會發送任何響應。你需要的最後一個return移動到相同的水平爲初始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) 

您還可以進一步簡化代碼:

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}) 
+0

我有一個另一個錯誤這一次。 UnboundLocalError at/save /局部變量'變量'在賦值之前被引用,例外類型:\t UnboundLocalError,異常值:\t 賦值之前引用的局部變量'變量', – 2014-09-28 07:01:50

+0

我修改的唯一東西是最後一次返回到與最初如果你說的話。 – 2014-09-28 07:03:54

+0

這是因爲當請求方法不是'POST'時,變量'variables'將永遠不會被定義,但會在return語句中使用。你可以在'if'語句之前通過初始化'variables'到'{}'來解決這個問題。儘管你可能想要考慮像Khalid所建議的那樣重構你的代碼。 – RevolutionTech 2014-09-28 07:15:46

相關問題