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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:30:14+00:00 2026-05-16T18:30:14+00:00

I am working on quick project , listing search results from bing and my

  • 0

I am working on quick project , listing search results from bing and my goal is user can save searches they perform . however i manage to list the search results in the template , and right now want to save the result items to the database using AJAX ! well this is my first attempt with ajax ( jquery ) and django !

template

{% for result in results %}<div class="resimgb">
    <a href="{{ result.MediaUrl }}"><img src="{{ result.Thumbnail.Url }}" /></a>
    <br />
    <input type="text" value="{{ result.MediaUrl }}" id="#urlink">
    <input type="submit" id="#savethis" name="add"></span><a herf="#" id="">save</a></div>{% endfor %}

JS :

<script type="text/javascript">
 $( document ).ready( function() {
  $( '#savethis' ).click( function() { data = 
            $( '#urlink' ).val(); $.get("/save/", function(data) {
              alert(data); 
          }); 
    }); 
}); 
</script>

–

I don’t write the view , cause am stuck at there actually ! well its going complex for me from here , as am little confused in

  • How to save the data to my model ?
  • How to send a response into the template to confirm saving the result ?
  • as am a newbie to jquery , i know this is very wrong to use id inside a loop , can anyone suggests how to fix this issue ?

There are many tutorials about Django / jQuery , but most of them i came through are mainly superficial enough to leave me with open questions as well !!

  • 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-16T18:30:14+00:00Added an answer on May 16, 2026 at 6:30 pm

    You obviously have to have a model for storing searches and a form for taking user input (search string).

    This will create a database table which will hold the saved search terms and relate the search terms to specific user.

    models.py

    class Search(models.Model):
        user = models.ForeignKey(User)
        search = models.CharField(max_length=255)
    

    It is easiest to just create a model form which will have input field in which user can write his search term, and also validates posted data as all user input should be validated:

    forms.py

    class SearchForm(forms.ModelForm):
        class Meta:
              model = Search
    

    When you post the form data with AJAX call from the form to the save view, post data will be validated, a instance with the search term will be save in the database after you do the search for the data.

    All left to do then is send all the data in the context dict to you template and render it.
    Once rendered you construct a dictionary with data, dump it to JSON and return it with simple HttpResponse with specific mimetype mimetype="application/json".

    views.py

    def save(request):
        if request.method == 'POST':
            form = SearchForm(request.POST):
            if form.is_valid():
                 # Do some search with forms.cleaned_data['search'] and save it to result
                 result = #SomeSearchCode...
                 instance = form.save()
    
        html = render_to_string('ajax_template.html', {'search':instance, 'result':result}, RequestContext(request))
        if request.is_ajax():
             response = {'result':"Success", 'html':html}
             return HttpResponse(json.dumps(response), mimetype="application/json")
        retrun render_to_response('full_template.html', {'html':html }, RequestContext(request)
    

    templates

    As for templates, full_template.html is the one that shows the search form and includes ajax_template.html to show results so we can easily append this with JavaScript. This way everything works even without JavaScript.

    jQuery

    AJAX call is also strait forward. You collect the data from the form and send it with data to the view which will receive it as request.POST. success is what handles the data you return from your save view. In this example it appends html rendered in the view to some random div.

    var dataString = 'search='+ $("search-field").val();;
    $(function() {
        $("#search-form").live("click", function() {
            $.ajax({
                type: "POST",
                url:  $("#search-form").attr("action"),
                data: dataString,
                dataType: "json",
                success: function(data, textStatus, XMLHttpRequest) {
                    jQuery(data["html"]).appendTo(".some-div");
                }
            });
            return false;
        });
    });
    

    This is not tested but i hope it shed some light on the process.

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

Sidebar

Related Questions

How can I make a quick start on Windows Phone 7 in working with
I am working on a pet project and am at the research stage. Quick
I'm working on a software project for Emacs that has some tests that can
I'm currently working on a project that requires the user to annotate (or comment)
I am working on Magento Quick book project. I am want to get Credit
I'm working on Android project where I'm collecting some information from web server. Everytime
I'm working on a quick sort algorithm for a Huffman coding project I'm working
Quick question regarding Google Maps. I have one fully working Google Maps project and
I'm working with Quick Test Professional 11. The situation is, i would like to
Quick question. For example, working with a some-what larger case of ~1000 options: which

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.