Hi,
I'm following the book "Django 1.0 WEB Site Development" to learn
django and python.

I modified the function BookmarkSave so that it correctly initialize
the checkbox showing if a bookmark is shared or not. I think it's
correct, please confirm I used the right way to do it adding
    share = SharedBookmark.objects.get(bookmark = bookmark)

When I try to edit a record and modify the value of the checkbox the
functions don't react to it at all.
I checked the form values and there is a difference in the parameters
of the checkbox between the "new" form and the "edited" one.

Could please someone explain me what is going on?

the code is:

models.py:

class Bookmark(models.Model):
    title=models.CharField(max_length=200)
    user=models.ForeignKey(User)
    link=models.ForeignKey(Link)
    def __unicode__(self):
        return u'%s, %s' % (self.user.username, self.link.url)

class SharedBookmark(models.Model):
    bookmark = models.ForeignKey(Bookmark, unique=True)
    date = models.DateTimeField(auto_now_add=True)
    votes = models.IntegerField(default=1)
    users_voted = models.ManyToManyField(User)
    def __unicode__(self):
        return u'%s, %s' % (self.bookmark, self.votes)

forms.py:

class BookmarkSaveForm(forms.Form):
    url = forms.URLField(
      label=u'URL',
      widget=forms.TextInput(attrs={'size': 64})
    )
    title = forms.CharField(
      label=u'Title',
      widget=forms.TextInput(attrs={'size': 64})
    )
    tags = forms.CharField(
      label=u'Tags',
      required=False,
      widget=forms.TextInput(attrs={'size': 64})
    )
    share = forms.BooleanField(
        label=u'Share on the main page',
        required=False
    )

views.py:

def _bookmark_save(request, form):
    #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)
    # share on the main page if requested
    if form.cleaned_data['share']:
        shared, created = SharedBookmark.objects.get_or_create(
            bookmark=bookmark
        )
        if created:
            shared.users_voted.add(request.user)
            shared.save()
    # save bookmark to database
    bookmark.save()
    return bookmark

@login_required
def bookmark_save_page(request):
    ajax = 'ajax' in request.GET
    if request.method == 'POST':
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            bookmark = _bookmark_save(request, form)
            if ajax:
                variables = RequestContext(request, {
                    'bookmarks': [bookmark],
                    'show_edit': True,
                    'show_tags': True
                })
                return render_to_response('bookmark_list.html',
variables)
            else:
                return HttpResponseRedirect('/user/%s/' %
request.user.username)
        else:
            if ajax:
                return HttpResponse(u'failure')
    elif 'url' in request.GET:
        url = request.GET['url']
        title = ''
        tags = ''
        share = False
        try:
            link = Link.objects.get(url=url)
            bookmark = Bookmark.objects.get(
                link = link,
                user = request.user
            )
            title = bookmark.title
            tags = ' '.join(tag.name for tag in bookmark.tag_set.all
())
            share = SharedBookmark.objects.get(
                bookmark = bookmark
            )
        except (Link.DoesNotExist, Bookmark.DoesNotExist,
SharedBookmark.DoesNotExist):
            pass
        form = BookmarkSaveForm({
            'url': url,
            'title': title,
            'tags': tags,
            'share': share
        })
    else:
        form = BookmarkSaveForm()
    variables = RequestContext(request, {'form': form})
    print 'form: ', form
    if ajax:
        return render_to_response('bookmark_save_form.html',
variables)
    else:
        return render_to_response('bookmark_save.html', variables)


Thanks for your help

Enrico

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to