Django博客回复系统 [英] Django blog reply system

查看:187
本文介绍了Django博客回复系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户在迷你博客上的帖子建立一个迷你回复系统。
每篇文章都有一个名为reply的链接。如果按下回复,则会显示回复表单,并且一个编辑回复,并提交表单。问题是我不知道如何取得我要回复的帖子的ID。在视图中,如果我使用一个数字(作为博客文章的id),它会将答复插入数据库。
但是我怎么可以不用硬编码呢?



视图是:

 
如果request.method =='POST':
form = ReplyForm(request.POST)
如果表单。 is_valid():
new_obj = form.save(commit = False)
new_obj.creator = request.user
new_post = New(1)#it只能使用硬编码的
new_obj。回复_回复引用举报返回顶部返回列表发新帖高级模式B Color Image Link Quote Code Smilies您需要登录后才可以回帖登录|注册发表回复回帖后跳转到最后一页。。。。。。。。。。。。。。。。。。。。。。。。。 html',{
'form':form,
},
context_instance = RequestContext(request))

我在forms.py:

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

和模型中:

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

提到New是微博类

 感谢


解决方案那里有他/她我解决了这个问题,使用你的建议,但我创造了另一个。
我在想,因为回复表单在另一个页面,只需点击该回复链接不会帮助我保留帖子id无论如何,因为博客页面消失了,我推后的回复按钮。所以,在我看来,我创建了一个保存博客文章的id作为参数的函数。它保存就像它应该,没有问题,但现在我的问题是:如何通过链接如下

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

$ b

(这是我在我的urls.py中保存的)
给每个帖子下的回复?我的意思是,直到现在,我的回复链接只是调用函数回复/ save_reply(我有回复),但现在,当我有id作为参数,我该怎么把它放在我的一个href ='这里什么?



这里是我的views.py,可以正常工作:

  def save_reply(request,id):

如果request.method =='POST':
form = ReplyForm(request.POST)
如果form.is_valid():
new_obj = form.save(commit = False)
new_obj.creator = request.user

u = New.objects.get(pk = id)
new_obj.reply_to =

new_obj.save()
return HttpResponseRedirect('。')
else:
form = ReplyForm()
return render_to_response('replies / replies.html',{
'form':form,
},
context_instance = RequestContext(request))

,我通过在浏览器中键入来调用它:
http://127.0.0.1:8000/replies/save_reply/1/ (例如)
当然,我删除了我的外键字段,现在它是不必要的y



谢谢!


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

解决方案

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!

这篇关于Django博客回复系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆