Search code examples
djangoblogsforeign-keysreply

Django blog reply system


i'm trying to build a mini reply system, based on the user's posts on a mini blog. Every post has a link named reply. if one presses reply, the reply form appears, and one edits the reply, and submits the form.The problem is that i don't know how to take the id of the post i want to reply to. In the view, if i use as a parameter one number (as an id of the blog post),it inserts the reply to the database. But how can i do it by not hardcoding?

The view is:

def save_reply(request):

  if request.method == 'POST':
    form = ReplyForm(request.POST)
    if form.is_valid():
       new_obj = form.save(commit=False)
       new_obj.creator = request.user
       new_post = New(1) #it works only hardcoded
       new_obj.reply_to = new_post
       new_obj.save()
       return HttpResponseRedirect('.')    
  else:
       form = ReplyForm()     
  return render_to_response('replies/replies.html', {
       'form': form,
       }, 
      context_instance=RequestContext(request))  

i have in forms.py:

  class ReplyForm(ModelForm):
    class Meta:
      model = Reply
      fields = ['reply']

and in models:

class Reply(models.Model):
reply_to = models.ForeignKey(New)
creator = models.ForeignKey(User)
reply = models.CharField(max_length=140,blank=False)
    objects = NewManager()   

mentioning that New is the micro blog class

    thanks

Solution

  • heyy there. i solved the problem,using your advices, but I've created another. I was thinking that as the reply form is in another page, simply clicking on that reply link ain't gonna help me retain the post id anyhow, because the blog page is gone, after i push thet reply button. So, in my view, i 've created a function that holds the id of the blog post as a parameter. It saves just as it should, no problem, but now my problem is: HOW CAN I PASS A LINK LIKE

        url(r'^save_reply/(?P<id>\d+)/$', 
                           save_reply,
                           name='save_reply'), 
    

    (this is what i hold in my urls.py) to the reply under each post? I mean, until now, my reply link was simply calling the function replies/save_reply(i had Reply) but now, when i have the id as a parameter, how can i put it in my a href = 'what here'?

    here is my views.py that works right:

     def save_reply(request, id):
    
     if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
           new_obj = form.save(commit=False)
           new_obj.creator = request.user
    
           u = New.objects.get(pk=id)
           new_obj.reply_to = u   
    
           new_obj.save()
           return HttpResponseRedirect('.')    
     else:
           form = ReplyForm()     
     return render_to_response('replies/replies.html', {
           'form': form,
           }, 
          context_instance=RequestContext(request))  
    

    and i'm callin it by typing in my browser: http://127.0.0.1:8000/replies/save_reply/1/ (for instance) of course, i've removed my foreign key field, as now it is unnecessarry

    Thank you!