我要通過這本書的Django 1.0網站開發,你建立一個小型社交書籤應用。我在第5章創建一個表單來添加書籤,雖然我遵循了指示,並且一直在這個錯誤上掙扎了好幾天。我得到的錯誤:Django的 - AttributeError的=>「設置」對象有沒有屬性「得到」
AttributeError at /save/
'set' object has no attribute 'get'
錯誤被扔在模板的第6行{{form.as_p}}
的views.py代碼:
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)
而模板的代碼是:
{% 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 %}
任何幫助將不勝感激,因爲我被困在這一點,在書中,似乎無法找到 一個答案。謝謝!
全部追溯的非快捷方式。 –
@ IgnacioVazquez-Abrams:「追溯或它沒有發生」,大聲笑 – jdi