On Wed, Feb 18, 2015 at 1:27 PM, João Marques <joao6697marq...@gmail.com> wrote:
> Hey guys, so basicly I want to send a GET request with ajax to one of my
> views and the specific view returns an html response that will be loaded on
> a div.
> The problem is that is doesn't seems to work at all. Nothing happers on the
> div.
>
> Please help me community! Thank you!

How have you debugged it so far?

>
>
> HTML to be filled with the response:
>
> <div id="content"></div>
>
>
> GET REQUEST:
>
>  $.ajax({
>                     type: 'GET',
>                     url: 'saveToDbAndReturn',
>                     data: {sols: holder}
>                 }).done(function (response) {
>                    document.getElementById("content").innerHTML = response;
>
>                 });

This is a GET request. It should be trivial to open the URL in a
browser, no AJAX. Does it render the correct content? If it doesn't,
fix that first.

Next, what jumps out is the URL. You've specified 'saveToDbAndReturn',
which is a relative URL. Javascript will evaluate that relative to the
current page, so if the current page is "/foo/bar/", it will attempt
to open "/foo/bar/saveToDbAndReturn'.

Is that the right URL?
What response status code do you get from the webserver for this AJAX
request? Use Web Inspector/firebug to determine.

PS: this is jquery?

$('#content').load('saveToDbAndReturn', {sols: holder})

PPS:

Views which modify data should not be GET requests. RFC2616 (HTTP) and
django's own docs:

In particular, the convention has been established that the GET and
HEAD methods SHOULD NOT have the significance of taking an action
other than retrieval. These methods ought to be considered "safe".
This allows user agents to represent other methods, such as POST, PUT
and DELETE, in a special way, so that the user is made aware of the
fact that a possibly unsafe action is being requested.

Ideally, views that modify data should not present data either. If
that behaviour is desired, on a successful data modification the view
can redirect to another url to render the newly stored information.

>
>
>
> VIEW:
>
> def saveToDbAndReturn(request):
>
>     if(request.GET.get('sols', ''))=='':
>         return HttpResponseRedirect("/")
>     else:
>         sols = json.loads(request.GET.get('sols', ''))

Arbitrarily loading user input as JSON might not be wise...

>
>
>         for i in range(0, len(sols)):
>
>             new_entry = Solution(fullArray=sols[i])
>             new_entry.save()

Ugly and inefficient. No need to count things, no need for multiple queries:

  Solution.objects.bulk_create([
      Solution(fullArray=datum)
      for datum in sols
  ])

>
>     return render_to_response('saveToDbAndReturn.html', {'sols': sols})
>
>
> saveToDbAndReturn.html
>
> <div class="panel panel-default">
>
> {% for i in range(sols) %}

PPPS:

You can't do that in templates. However, within any {% for %} loop,
django is already counting. Do this instead:

{% for elem in sols %}
<div class="panel-heading" role="tab" id="heading{{ forloop.counter0 }}">

>
>     <div class="panel-heading" role="tab" id="heading{{i}}">
>       <h4 class="panel-title">
>         <a data-toggle="collapse" data-parent="#accordion"
> href="#collapse{{i}}" aria-expanded="true" aria-controls="collapse{{i}}">
>             {{i}}
>         </a>
>       </h4>
>     </div>
>     <div id="collapse{{i}}" class="panel-collapse collapse in"
> role="tabpanel" aria-labelledby="heading{{i}}">
>       <div class="panel-body">
>         {{sols[i]}}

PPPPS:

This is not how to access the i-th member of an array in django
templates. In fact, there is not a way to do that without using a
custom tag. However, if you rewrite your loop logic as suggested in
the PPPS, you don't need to do that, just "{{ elem }}"

Cheers

Tom

-- 
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+unsubscr...@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/CAFHbX1Jvp%2BepEhhgyZHkcVuWffkwJ5vur3KDMxrm-ZdYfWCCFg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to