Tuesday, February 4, 2014

Django 1.6 Python 3.3 How to recognize and handle three different form's POST data in the same page without reloading the entire page?

OK, my situation seems similar to SO but much simpler. As we know, in a SO question detail page we can post a comment to this quesion, or to one of its answers, we can also post an answer to this question. All will be done in the same page, and if you just post comments, it is not required to reload the entire page to update the comments -- this is just what I don't know how to do.

Currently, I only know how to handle one form's POST data in a page -- handle answer form's POST data.

I know solutions have something to do with jquery. But it is hard for me to find a solution for my situation, and I am really tired after searching for a long time. Maybe for you it is just a very simple thing. Thanks.

Below I will show you my complete code. A directly code solution is the best. An tutorial (example is better) that could solve my problem is also appreciated.


#ROOT urls.py  urlpatterns = patterns('',      url(r'^questions/', include('questions.urls',namespace='questions')),  )    #urls.py  urlpatterns = patterns('questions.views',        url(r'^(?P<question_pk>\d+)/detail/$',          'detail',          {'template_name': 'questions/detail.html'},          name='detail'),                         )    #models.py  class User(AbstractBaseUser):      username = models.CharField(unique=True)    class Question(models.Model):      user = models.ForeignKey(User)        title = models.CharField(blank=True)        content = models.TextField(blank=True)    class Answer(models.Model):      user = models.ForeignKey(User)        question = models.ForeignKey(Question)        content = models.TextField(blank=True)       class QComment(models.Model):      question = models.ForeignKey(Question)        user = models.ForeignKey(User)        content = models.TextField(blank=True)    class AComment(models.Model):      answer = models.ForeignKey(Answer)        user = models.ForeignKey(User)        content = models.TextField(blank=True)    #forms.py  class AnswerForm(ModelForm):      class Meta:          model = Answer          fields = ['content']    class QCommentForm(ModelForm):      class Meta:          model = QComment          fields = ['content']    class ACommentForm(ModelForm):      class Meta:          model = AComment          fields = ['content']    #views.py   def detail(request,      question_pk,      template_name,      answer_create_form=AnswerForm,      qcomment_create_form=QCommentForm,      acomment_create_form=ACommentForm,):        '''detail to show a question,contains:      1.a question containing title and content.          all comments to this quesiton if possible.          a form to add a comment to this question.      2.all answers(represented by field 'content')to this question.          all comments to each answer if possible.          a form to add a comment to each answer.      3.a form for create an answer to this question. '''        question = Question.objects.get(pk=question_pk)      user = request.user      qcomments = question.qcomment_set.all()      #all answers to this question      answers = question.answer_set.all()        #form to create answers      a_form = answer_create_form()      #form to create comments      qc_form=qcomment_create_form()      ac_form=acomment_create_form()        if user.is_authenticated():          #here only catches the answer_create_form's post data.          #how do I handle the situation when a user          #post a comment to a question or an answer in the same page          #without reloading the entire page.          if request.method == 'POST':              answer= Answer(user=user,question=question)              form = answer_create_form(data=request.POST,instance=answer)              if form.is_valid():                  form.save()                  return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))                  return TemplateResponse(request, template_name, locals())    #templates/detail.html      {{ question.title }}  <br />  {{ question.content }}  <br />  {{ question.user }}  <br />  {% for cm in qcomments %}      {{ cm.content }} {{ cm.user }}<br />  {% endfor %}  <form action="" method="post">{% csrf_token %}      {{ qc_form.as_p }}      <input type="submit" value="add comment" />  </form>    {% for answer in answers %}      {{ answer.content }}      <br />      {{ answer.user }}      <br />      {% for cm in answer.acomment_set.all %}          {{ cm.content }} {{ cm.user }}<br />      {% endfor %}      <form action="" method="post">{% csrf_token %}          {{ ac_form.as_p }}          <input type="submit" value="add comment" />      </form>  {% endfor %}    <form action="" method="post">{% csrf_token %}      {{ a_form.as_p }}      <input type="submit" value="add answer" />  </form>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d801eb2b-7ccf-44f9-82dd-548c8a5a92b4%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment