1

我正在尝试遵循书中的教程 - Learning Website Development with Django (2008), Chapter 5, Page 72

我收到以下错误http://dpaste.com/1034485/并且我无法确定它是否在我的 views.py 或 urls.py 文件中。(PS Dpaste Link 有效期至 2013 年 3 月 19 日)

与我发现的类似问题如下:

  1. http://markmail.org/message/w3s6sacserx2mwdi(非常相似但找不到答案)

  2. Django - AttributeError => 'set' object has no attribute 'get'(类似情况但错误不一样。在这个问题中 - 错误实际上是在表单代码和设置文本字段的属性中。我删除它们,现在代码工作正常。但是,我不确定他做了什么以及他编辑了什么代码。

我的代码如下:

视图.py

def bookmark_save_page(request):
    if request.method == "POST":
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            # Create or get link            
            link, dummy = Link.objects.get_or_create(
                url=form.cleaned_data['url']
            )

            # Create or get bookmark.
            bookmark, created = Bookmark.objects.get_or_create(
                user = request.user,
                link=link,
            )

            # Update bookmark title
            bookmark.title = form.cleaned_data['title']
            # If the bookmark is being updated, clear old tag list.
            If not created:
                bookmark.tag_set.clear()

            # Create new tag list.
            tag_names = form.cleaned_data['tags'].split()
            for tag_name in tag_names:
                tag, dummy = Tag.objects.get_or_create(name=tag_name)
                bookmark.tag_set.add(tag)
            # Save bookmark to database
            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)

错误信息:

> Environment:

> Request Method: GET

> Django Version: 1.4
> Python Version: 2.7.3


> 5. from bookmarks.views import *

> Exception Type: SyntaxError at /
> Exception Value: invalid syntax (views.py, line 56)

如果帖子太冗长,请道歉。不确定需要什么信息。

注意:views.py 的第 56 行是 If not created:

4

1 回答 1

1
        # If the bookmark is being updated, clear old tag list.
   #    ↓ should be lowercase
        if not created:
            bookmark.tag_set.clear()
于 2013-03-25T16:07:21.833 回答