New to Django and Ajax in general.
I was wondering about a general question of good-practice web design: Should a view function that returns a call called using ajax, return data structures and leave rendering of HTML pages to the client side (meaning javascript) or is it also an "okay" practice to render the entire HTML and return it to javascript so it can paste it somewhere in the code?
I don't want the logic of my templates to be repeated by javascript. I want to use the templates to render HTML and return it to the client. But... that misses the point of ajax, doesn't it?
I did come across this fantastic post: Rendering JSON objects using a Django template after an Ajax call
Since it's old, I didn't post a reply there but rather revived the matter here. Is it technically possible to render on server side? yes. Best practice? I don't know.
Perhaps the better practice is to have my HTML pages sort of "empty spaces" already well-formed and just pass data back and forward.
I would love to hear your opinions on the matter.
After being set on hold since this discussion tends to be opinion based (it does) and that format isn't fit for this forum, I though it would be good to update my post to include a finer resolution of discussion with very much technical implications.
What, in your "djangonian" experience, would be a best-practice for building a web page, such as a news feed, that could be dynamically updated with ajax calls, which return the client raw data (using JSON for example) and not pre-rendered HTML?
I'll get more specific: My website has a feed. On that feed, the user sees many posts and comments per post. Every post is its own HTML form, and already structured as HTML. An example for a page source:
<form id="post_looloo_by_testuser" method="post" action="/bz/login/" class="single_post_form">
<input type='hidden' name='csrfmiddlewaretoken' value='GfOHEMxx433XBUpg3yakTYPRQCVq132f' />
<p class="box_single_post">
<span name="post">
<label class="single_post_form">
says: looloo
</label>
</span>
<span name="comments_for_post">
<span name="single_comment">
<label class="single_comment">
testuser2 commented: moomoomoo
</label>
</span>
</span>
<span name="comments_for_post">
<span name="single_comment">
<label class="single_comment">
testuser2 commented: qweqwe
</label>
</span>
</span>
<span name="add_comment">
<input id="id_add_comment" maxlength="140" name="new_comment" type="text" placeholder="Add a comment..."/>
</span>
</p>
</form>
On the top, much like on facebook, the user can add a new post instantly.
<form id="add_post_form" method="post" action="" class="new_post_form" username="testuser">
<input type='hidden' name='csrfmiddlewaretoken' value='GfOHEMxx433XBUpg3yakTYPRQCVq132f' />
<span name="add_post">
<input id="id_add_post" maxlength="140" name="new_post" type="text" placeholder="Say something"/>
<input id="add-post-btn" type="submit" name="submit" value="Add" username="testuser"/>
</span>
</form>
The "Add" button (really a submit for the form) would then invoke an ajax call to a certain view within my app on the server side. I want the view to return information such as the user who owns the post, the post text, the comments list etc. The information would be return as JSON or XML. Not as pre-rendered HTML. I want to have my javascript process the information and update the user's feed with the new post on top, while avoiding refreshing the page (hence the use of ajax).
Problem is the user can of course add an infinite number of posts (or a limited number, but bigger than one). Why does it matter? see below question:
How should I create the feed page in a way which allows for me to just update variables on the page with the dynamically retrieved information, and still allow for more posts to be added?
If I were to know in advance that only one such new post is available, then I would create a pre-made post form, invisible, and the javascript would set values to that form and set it to visible. But what about the second post the user adds? and the third? and so on...
I would love to learn from your experience. Thank you.