Django - AttributeError => 'set' object has no attribute 'get'

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'

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

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)

And the template code is:

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

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!

Answers


Is this an error for you?

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

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.

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))

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


Need Your Help

Magento OnePage Checkout Conflict with jQuery

php jquery magento prototypejs conflict

i am using Magento 1.8.1 i have used responsive navigation menu and for this i have to use jQuery if i put jQuery on my checkout stops working.

Repeating rows from right join

sql left-join right-join

My application saves logs that need to be taken at least one time during each of the 3 different time periods during the day. So ideally 3 logs per day, each with a unique time period ID. I need to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.