Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to render only part of html with data using django

I am using ajax to sort the data which came from search results.

Now I am wondering whether it is possible to render just some part of html so that i can load this way:

$('#result').html(' ').load('/sort/?sortid=' + sortid);

I am doing this but I am getting the whole html page as response and it is appending the whole html page to the existing page which is terrible.

this is my views.py

def sort(request):
  sortid = request.GET.get('sortid')
  ratings = Bewertung.objects.order_by(sortid)
  locations = Location.objects.filter(locations_bewertung__in=ratings)
  return render_to_response('result-page.html',{'locs':locations},context_instance=RequestContext(request))

how can I render only that <div id="result"> </div> from my view function? or what am I doing here wrong?

like image 993
doniyor Avatar asked Apr 19 '13 17:04

doniyor


People also ask

How do I render HTML without a template in Django?

Yes, it is possible to do this. In order to render HTML without a template in Django, we’ll need to manually build up a string of HTML and send that to the browser as an HttpResponse. It’s not really ideal to render HTML this way, but this is a good exercise to really understand how Django processes requests, responses, and rendering of HTML.

How to render an HTML page using Django shortcuts?

Next, create a view in home/views.py for the template. The index () method renders the HTML page. This is made possible using the render helper function from the django.shortcuts package. Here, the render () function takes the request object and the template_name as the arguments and returns an HttpResponse object with the rendered text.

How to modify Dataframe for Table View in Django?

In Django, It is easy to render the HTML templates by setting URLs of respective HTML pages. Here we will let to know about how we can work with DataFrame to modify them for table view in the HTML template or web-pages, and for that, we have to use ‘render’ and ‘HttpResponse’ functions to manipulate the data inside the DataFrame.

How do I create a dynamic HTML page in Django?

Django uses templates to generate HTML dynamically. A template contains the static part as well some static syntax for displaying the dynamic content of the desired HTML page. Django has the Django template language for its own template system. Create a directory named templates inside the newly created home apps directory.


1 Answers

From what I understand you want to treat the same view in a different way if you receive an ajax request. I would suggest splitting your result-page.html into two templates, one that contains only the div that you want, and one that contains everything else and includes the other template (see django's include tag).

In your view then you can do something like the following:

def sort(request):
    sortid = request.GET.get('sortid')
    ratings = Bewertung.objects.order_by(sortid)
    locations = Location.objects.filter(locations_bewertung__in=ratings)
    if request.is_ajax():
        template = 'partial-results.html'
    else:
        template = 'result-page.html'
    return render_to_response(template,   {'locs':locations},context_instance=RequestContext(request))

results-page.html:

<html>
   <div> blah blah</div>
   <div id="results">
       {% include "partial-results.html" %}
   </div>
   <div> some more stuff </div>
</html>

partial-results.html:

{% for location in locs %}
    {{ location }}
{% endfor %}
like image 90
Geekfish Avatar answered Oct 26 '22 22:10

Geekfish