0
votes

I'd like to append a quoted pots to user's post before saving it.

Here is the view:

@login_required
def quote_reply(request, quote_id):
    tform = PostForm()
    print 'quote_id is:' + quote_id
    quote = Post.objects.get(pk = quote_id)
    topic_id = quote.topic_id
    topic = Topic.objects.get(id= topic_id)
    print 'quote is' + quote.body

    args = {}
    if request.method == 'POST':
        post = PostForm(request.POST)


        if post.is_valid():
            p = post.save(commit = False)
            p.topic = topic
            p.title = post.cleaned_data['title']
            p.body = post.cleaned_data['body']
            p['body'].append(str(quote))  #problematic line
            p.creator = request.user
            p.user_ip = request.META['REMOTE_ADDR']

            if len(p.title)< 1:
                            p.title=p.body[:60]            
            p.save()

            tid = int(topic_id)
            return HttpResponseRedirect('/forum/topic/%s'  % topic_id)

    else:
        args.update(csrf(request))
        args['form'] = tform
        args['post'] = quote
        args['topic_id'] = topic_id
        return render_to_response('myforum/qoute_reply.html', args, 
                                  context_instance=RequestContext(request))

I tried also tried

p['body'].append(unicode(quote)) 

but gives the same error.

Appreciate your help to resolve this.

Update: Here is the Post model

class Post(models.Model):
    title = models.CharField(max_length=75, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    creator = models.ForeignKey(User, blank=True, null=True)
    updated = models.DateTimeField(auto_now=True)
    topic = models.ForeignKey(Topic)
    body = models.TextField(max_length=10000)
    user_ip = models.GenericIPAddressField(blank=True, null=True)

    def __unicode__(self):
        return u"%s - %s - %s" % (self.creator, self.topic, self.title)

    def short(self):
        return u"%s - %s\n%s" % (self.creator, self.title, self.created.strftime("%b %d, %I:%M %p"))

    short.allow_tags = True

Not sure what to do.

1
can you show your Post model ? - karthikr
Why are you using p['body'] in that line? That's your error, but I'm not clear what you're intending to do that would make you use that rather than p.body. - Peter DeGlopper
@karthikr I just added the model. - supermario
@PeterDeGlopper I also tried p.body which yeilds to: 'unicode' object has no attribute 'append' - supermario
do you have unicode specified on all the foreign key attributes used in __unicode__ ? My guess is, something is going wrong while serialization: Try self.creator.username, self.topic.title, self.title, etc.. - karthikr

1 Answers

1
votes

The main problem here is that p is a model instance, which does not support dict-style attribute access syntax. To access the post attribute, use the standard dot syntax, p.post.

The second problem is that you can't use append to change a Unicode or string object - they're immutable. Instead, you should create a new Unicode object containing the content you want and assign that. For instance:

p.post = post.cleaned_data['body'] + unicode(quote)