Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:23:18+00:00 2026-05-30T20:23:18+00:00

Looking to get some insight and tips on using ajax with django. Say I

  • 0

Looking to get some insight and tips on using ajax with django.

Say I have a function:

def add_comment(request, pk):
  if request.method == 'POST' and request.is_ajax():
    comment_form = CommentForm(request.POST)
    if comment_form.is_valid():
      comment = comment_form.save(commit=True)
      comment.save()
    json = simplejson.dumps(comment, ensure_ascii=False)
   return HttpResponse(json, mimetype='application/json')
  return render_to_response({{ post.id }}', {'comment': comment,}), context_instance=RequestContext(request), mimetype='application/json')

and I’m trying to post the comments to a page without a redirect with ajax function:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script></javascript>
<script type="text/javascript">
  $(document).click(function()
  {
   $('#comment_form').submit(function()
   {
    var dataString = $('#comment_form').serialize();
    $.ajax({
      type: 'POST',
      url: '',
      data: dataString,
      success: function(data){
        $('').html(data);
      },
    });
    return false;
    });
  });

</script>

I believe I’m mixing up a few things here. I am trying to get the page to load comments without a redirect. I don’t need an exact answer, maybe just steered in the right direction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:23:20+00:00Added an answer on May 30, 2026 at 8:23 pm

    Thanks for the posts I finally got things worked out. The jquery was the main issue.

    $(document).ready(function() {
      $('#comment_form').submit(function(e) {
      e.preventDefault();
      $.ajax({
        type: 'POST',
        url: '{% url art.views.post %}',
        data: $('#comment_form').serialize(),
        dataType: 'json';
        success: function(){
          location.reload();
    $('#comment_form').get(0).reset();
      },
      });
      return false;
      });
    });
    

    I was sending the DOM object not the actual form data to the view.

    In the view I combined two functions to get the two sharing the same URL.

    def post(request, pk):
      post = Post.objects.get.(pk=int(pk))
      comments = Comment.objects.filter(post=post)
      _dict = dict(post=post, comments=comments, form=Comment_form(), user=request.user)
      _dict.update(csrf(request))
      cf_obj = Comment(post = Post.objects.get(pk=pk))
      if request.method == 'POST' and request.is_ajax():
        if comment_form.is_valid():
          comment = comment_form.save(commit=True)
        else:
          raise Http404
        response = serializers.serialize('json', [comment])
        return HttpResponse(response, mimetype='application/json')
      return render_to_response('post.html', d)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to get some info/tips on making a Generic (iOS/Android/WebOS/etc...) Mobile Site. Looking
We have a SaaS web app and are looking to get some feedback from
still a bit of a n00b on SharpSVN, I'm looking to get some simple
Where can I get some decent looking free ASP.Net or CSS themes?
I was looking at this article from 2005 and wanted to get some thoughts
I'm looking to make a website that will probably get some heavy, repetitive traffic.
I'm looking to parse some CXML in PHP...basically all I'm looking to get the
I'm looking at some open source Java projects to get into Java and notice
Perhaps someone could provide some insight into a problem I have. I have a
I'm looking to get some advice; I'm about to build an OOP signup form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.