Django的 - AttributeError的= GT; “设置'对象有没有属性'得到' [英] Django - AttributeError => 'set' object has no attribute 'get'

查看:202
本文介绍了Django的 - AttributeError的= GT; “设置'对象有没有属性'得到'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过你在那里建立一个小型社交书签应用书中Django的1.0网站开发。我在那里章创建表单添加书签5,虽然我已经按照指示,并一直在努力对这个错误天。我得到的错误:

I'm going through the book Django 1.0 Website Development where you build a small social bookmarking application. I'm at chapter 5 where you create a form to add bookmarks and although I've followed the instructions and have been struggling on this error for days. I get the error:

AttributeError at /save/
'set' object has no attribute 'get'

该错误被抛出在模板上的第6行{{form.as_p}}

The error is being thrown on line 6 of the template {{ form.as_p }}

在views.py code是:

The views.py code is:

def bookmark_save_page(request):
    if request.method == 'POST':
        form = BookmarkSaveForm(request)
        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
            )
            # if bookmark is being updated, clear the 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()
            # 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)

和模板code是:

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

任何帮助将是非常美联社preciated因为我被困在书中这一点,似乎无法找到答案。谢谢!

Any help would be much appreciated as I'm stuck at this point in the book and can't seem to find an answer. Thanks!

推荐答案

这是一个错误的吗?

for tag_name in tag_names:
    tag, dummy = Tag.objects.get_or_create(name=tag_name)
    bookmark.tag_set.add()  # not adding the tag?

难道不应该是: bookmark.tag_set.add(标签)?在。新增()实际上并没有造成错误,但我知道您不添加您的标记。

Shouldn't it be: bookmark.tag_set.add(tag) ? The .add() doesn't actually cause an error, but I know you aren't adding your tag.

在没有看到追踪,我猜。

Without seeing the traceback, I'm guessing.

我的另一个猜测是,你可能会使用的RequestContext错了?

My other guess is that you might be using the RequestContext wrong?

return render_to_response('bookmark_save.html',
                              {'form': form},
                              context_instance=RequestContext(request))

我相信你现在正在使用它的方式是为使用的Htt presponse()的非快捷方式

I believe the way you are using it now is meant for the non-shortcut approach of using an HttpResponse()

这篇关于Django的 - AttributeError的= GT; “设置'对象有没有属性'得到'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆