获取有关单个html模板的所有帖子和评论

时间:2014-04-25 13:56:16

标签: jquery python html ajax django

我对django很新。如果有任何愚蠢的内容在这里提及,请原谅我,请指导我。 我想在crave.html模板中显示所有帖子和相关评论。我尝试过,但我无法做到这一点。我如何在那里使用ajax请求?

models.py

class crave_data(models.Model):
    person = models.ForeignKey(User)
    post=models.TextField(blank = True,null = True)
    date= models.DateTimeField(auto_now_add=True, blank=True)

 class comments(models.Model):

    crave=models.ForeignKey(crave_data, blank = True,null = True)
    reply=models.CharField(max_length=1000, blank = True,null = True)

    date= models.DateTimeField(auto_now_add=True, blank=True)

我想在crave.html页面上显示所有广告和相关评论。提交后,应该在同一页面上显示。并且应该有回复选项。像普通的评论结构一样。 这是我的views.py

@login_required
def crave_edit(request, id=None):
    instance = get_object_or_404(crave_data, pk=id) if id else None
    crave_made = None
    if instance and instance.person != request.user:
        raise PermissionDenied()

    if request.method=="POST":
        form=CraveDataForm(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            crave_made = form.save(commit=False)
            crave_made.person = request.user
            crave_made.save()

       else:                    
        form=CraveDataForm(instance=instance)  

    return render(request, "crave/crave.html", {'form' : form,'crave_made':crave_made})


@login_required
def crave_view(request, id):
    #what id is this? this is crave_data id
    crave = get_object_or_404(crave_data, pk=id)
    print crave
    check = comments.objects.filter(crave=crave.id)
    if request.method=="POST":
        form=CraveReplyForm(request.POST, request.FILES)
        if form.is_valid():
            reply = form.save(commit=False)
            reply.person=request.user
            reply.crave = crave
            reply.save()                    
    else:                    
        form=CraveReplyForm()  

    return render(request, "crave/comment.html", {'crave': crave, 'form' : form, 'check':check})

我的urls.py

urlpatterns = patterns('crave.views',
                       url(r'^$', 'crave_edit'),
                       url(r'^(?P<id>.*)/$', 'crave_edit'),
                       url(r'^reply_get/(?P<id>.*)$', 'crave_view'),

     )

crave.html

<form action="" method="post" style="font-size:150%">{% csrf_token %}
Query<br /><br/>
                {{ form.post }}
  <input type="submit" value="Submit" style="background-color:#53760D;color:#D0F18F;width:30% ;align:center;margin:0 auto;" />
</form>

comment.html

<form class="horizontal-form" role="form" action="/crave/reply_get/{{crave.id}}" method="post"  style="padding: 10px;">
           {% csrf_token %}
                <div class="form-group" >
                        <div class="col-sm-10">
                        {{ form.reply.label_tag }} {{ form.reply }} </br> </br>
                    </div>

                    </div>
                    <input type="submit" class="btn btn-success" value="reply" />
    </form>
  {{crave}}
{% for pf in check%}
                  {{pf.reply}}<br>
            {% endfor %}

现在发布和评论在不同的模板上。我想使用ajax请求获取所有帖子并在crave.html页面上发表评论。 请帮帮我。提前谢谢。

0 个答案:

没有答案