Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango render_to_string not working
    primarykey
    data
    text
    <p>I am trying to implement an ajax view to create an object and then return it and insert it into the template. It is almost working except I cannot seem to get render_to_string() to work to render the html to insert. The object is being created and html is being returned and inserted into the template however the variables are not included in the html. My view is below.</p> <pre><code>def tr_create_xhr(request, person, slug): if request.method == "POST": form = TopicResourceForm(request.POST) if form.is_valid(): try: r = Resource.objects.get(url=form.cleaned_data['resource']) except Resource.DoesNotExist: r = Resource.objects.create(url=form.cleaned_data['resource'], rtype=form.cleaned_data['rtype']) r.save() obj = form.save(commit=False) obj.resource = r try: topic = Topic.objects.get(person__user=request.user, slug__iexact=slug) except Topic.DoesNotExist: return Http404 obj.topic = topic objs = obj.save() html = render_to_string('includes/tr_inc.html',{"r":objs, "topic":topic}) res = {'html':html} if request.is_ajax(): return HttpResponse(simplejson.dumps(res), mimetype="application/json") else: return HttpResponseRedirect("../..") return Http404 </code></pre> <p>This is the template "includes/tr_inc.html":</p> <pre><code>{% load markup %} {% load people_tags %} &lt;li&gt; &lt;h5&gt;{{ r.title }}&lt;/h5&gt; &lt;p&gt;&lt;a class="tru" href={{ r.resource.url }}&gt;{{ r.resource.url|sliceit:70 }}&lt;/a&gt;&lt;/p&gt; &lt;span class="oc"&gt;&lt;p&gt;Added {{ r.added }}{% if r.rtype %} |&lt;a href={% url resource_type_detail r.rtype.slug %}&gt;{{ r.rtype }}&lt;/a&gt;{% endif %} | &lt;a href="/topics/user/{{ r.topic.person.user }}/{{ r.topic.slug }}/topic-resource/delete/{{ r.id }}/"&gt;Delete&lt;/a&gt; &lt;a href="/topics/user/{{ r.topic.person.user }}/{{ r.topic.slug }}/topic-resource/edit/{{ r.id }}/"&gt;Edit&lt;/a&gt; {{ r.note|markdown }}&lt;/span&gt; &lt;/li&gt; </code></pre> <p>The html string that is returned is the template without any variables inserted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload