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?

116 views
Skip to first unread message

项楠

unread,
Feb 4, 2014, 2:36:12 PM2/4/14
to django...@googlegroups.com

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>

nkuttler

unread,
Feb 5, 2014, 4:58:56 PM2/5/14
to django...@googlegroups.com


On Tuesday, February 4, 2014 3:36:12 PM UTC+1, 项楠 wrote:

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.


Well, you don't need jQuery at all, what you want to do is called Ajax, but jQuery makes it a little easier.

So you have a page with multiple forms on it. You simply give those forms different action urls that point to different views and intercept the form submission with JavaScript. When you intercept the submission you send an Ajax request to the form's view instead, and do something with the returned data.


You might want to look into one of the REST packages for Django.
Reply all
Reply to author
Forward
0 new messages