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 8290783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:54:43+00:00 2026-06-08T12:54:43+00:00

I am newbie working with django. I need simple example. How to submit form

  • 0

I am newbie working with django. I need simple example.
How to submit form (post) without refreshing page using Django, Ajax, jQuery?

This is my form, views and template:

views.py

from django.shortcuts import *
from django.template import RequestContext
from linki.forms import *

def advert(request):
    if request.method == "POST":
        form = AdvertForm(request.POST)

        if(form.is_valid()):
            print(request.POST['title'])
            message = request.POST['title']

        else:
            message = 'something wrong!'


        return render_to_response('contact/advert.html',
                {'message':message},
            context_instance=RequestContext(request))

    else:
        return render_to_response('contact/advert.html',
                {'form':AdvertForm()},
            context_instance=RequestContext(request))

forms.py (form using “ModelForm”)

from django import forms
from django.forms import ModelForm
from linki.models import Advert


class AdvertForm(ModelForm):
    class Meta:
        model = Advert

TEMPLATES (form html code)

<html>
<head>

</head>
    <body>
    <h1>Leave a Suggestion Here</h1>
        {% if message %}
            {{ message }}
        {% endif %}
        <div>
            <form action="" method="post">{% csrf_token %}
                {{ form.as_p }}
                <input type="submit" value="Submit Feedback" />
            </form>
        </div>
    </body>
</html>
  • 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-06-08T12:54:46+00:00Added an answer on June 8, 2026 at 12:54 pm

    if you are planning to use an ajax submit with jquery you should not return html from your view.. I propose you to do this instead:

    html:

    <html>
    <head>
    </head>
    <body>
        <h1>Leave a Suggestion Here</h1>
            <div class="message"></div>
            <div>
                <form action="" method="post">{% csrf_token %}
                    {{ form.as_p }}
                    <input type="submit" value="Submit Feedback" />
                </form>
            </div>
    </body>
    </html>
    

    the js

    $('#form').submit(function(e){
        $.post('/url/', $(this).serialize(), function(data){ ... 
           $('.message').html(data.message);
           // of course you can do something more fancy with your respone
        });
        e.preventDefault();
    });
    

    the views.py

    import json
    from django.shortcuts import *
    from django.template import RequestContext
    from linki.forms import *
    
    def advert(request):
        if request.method == "POST":
            form = AdvertForm(request.POST)
    
            message = 'something wrong!'
            if(form.is_valid()):
                print(request.POST['title'])
                message = request.POST['title']
    
            return HttpResponse(json.dumps({'message': message}))
    
        return render_to_response('contact/advert.html',
                {'form':AdvertForm()}, RequestContext(request))
    

    so that way you put the response in the message div. instead of returning plain html you should return json.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am a newbie working with jQuery and Ajax. I am trying submit
Django Newbie working on a form to collect information for a carpool app. In
I am a newbie in Rails and am working on a page that enables
I am kind of newbie on C++, and working on a simple program on
I am a newbie with a problem working with Django-nonrel on Google App Engine.
Django newbie at work and I could use some pointers. I'm using django-profile and
I'm a newbie programmer working with jQuery and wonder if anyone can help me
I'm newbie to android and present working on android smartphone apps using phonegap and
I'm a newbie with jQuery Mobile and I'm working with a list that is
Hibernate newbie here. I am working on a simple Hibernate mapping file. When I

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.