I'm trying to set value to Django session within the template and then posible used in the view.
I'm doing something like this
{% block body %}
<html>
{% request.session.fav_color="red" %}
<div> Is your favorite color {{ request.session.fav_color}} ?</div>
</html>
{% endblock %}
There are several reasons why this may not be a good idea:
Templates should deal with presentation only. If you are placing logic in the template it is supposed to be presentation logic. Placing business logic in the template is a violation of the SoC (separation of concerns).
Rendering the template often is one of the last things you do in a view so it is hard to get the cat back in the bag if you have already sent the data to the browser (and if you rendered the template but haven't sent data down the pipe you can just update the value in the view instead of trying to do it in the template).
If you are interacting with the user the traditional way to do it is using a form (even if it is a form with only the submit button) - or posting data to the server in the frontend using AJAX.
Related
I have a chat board where people can post messages that can include attachments and some other additional things. When I retrieve all messages I loop over them and display each message in this way (simplified):
<p>Message posted by: <strong>{{ details.name }}</strong></p>
<p>{{ details.message }}</p>
<ul>
{% foreach attachment in attachments %}
<li>{{ attachment }}</li>
</ul>
{% endforeach %}
So far, so good. Now, my difficulty is that I use AJAX to allow people to post new message. Upon adding a new message, I struggle to find the best way to show this message to the user. The problem is that the new message needs to be formatted in a particular way (the above is a simplification; there are various conditions and additional formats). I can think of three ways:
I use javascript to parse the message. That means I basically have the same code twice, in two different languages (once in Django's template language for all existing message, and then again in js for newly added messages)
I craft the exact same HTML code in the view, so when I return a response through AJAX it already includes properly formatted code. However, this will also be a duplication of code - once in the template itself and again in the view
I create a new function within the model that creates all the right HTML code (say formatMessage()). When I loop over existing messages, I can call this function, and when I add a new message I can call the same function and return this in my response.
The last option is the only one that does not have duplicated code. However, it would introduce a whole lot of code including HTML code to my modal, which seems like an awful thing to do.
Any advice?
There's no reason you can't use a template in an Ajax response. You can extract this template fragment into its own template file and either render it directly, in the case of the Ajax response, or include it in another template for a normal page view.
I have a Django template file working with a passed value like the below.
{% include 'boutique/rating.html' with score=[I want to put value here] %}
When I usually put value into the template, I could easily do it by doing like the below.
{% for store in stores %}
{% include 'boutique/rating.html' with score=store.review_score %}
{% endfor %}
However, as I get into more complex templates, I need to assign the value to the score parameter in the include section through jQuery. Is there a way that I can acheive this through jQuery?
What you need to understand is this: The part of the template between {% and %} is interpreted by Django. It is processed entirely on the server. In other words, it never shows up in the browser, but gets replaced by some sort of standard HTML.
On the other hand, jQuery is a Javascript library and operates entirely in the browser -- it doesn't know anything about the server or Django.
So, to modify the included template with jQuery, you have to find out what HTML it renders to. You can probably do that by looking at the included template file. Then, treat that HTML the way you would any other part of the page for manipulation with jQuery.
I am building an application with Django as the backend and React js for making the interface of the application.
I have a set of Posts which I want to display.
Currently, the approach which I am following is -
Get the template having the compiled js code linked to it.
Then again make get call to get the posts
My question is - In this current approach I am making 2 GET calls to the backend, one for rendering the template and then again for getting the Post.
What is the best way to achieve this? Is this the usual flow how applications are built using Django and React JS?
First off: I don't see anything wrong with doing this in two requests, because one loads the application itself and the second loads the content. To me this seperation makes sense and might turn out to be useful in the future if you want to reuse say the Endpoint, that yields the the posts(i.e. the content).
Answering your question: If, for whatever reason, you absolutely want to load everything with a single GET, a good way of doing so, would be to pass a list of posts to the context as a JSON-serialized object and then load these into the JS-context within the Django-template.
i.e. in the view.py:
from json import dumps
def view(request):
context = {
'posts':get_posts(),
}
render_to_response('django_template.html', context,
context_instance=RequestContext(request))
def get_posts():
qs = Posts.objects.all()
return dumps({'posts': qs })
in the django_template:
{% block content %}
<div id="root"></div>
{% endblock %}
{% block javascript %}
<script>
var INITIAL_POSTS = {{ posts|safe }};
</script>
<script type="text/javascript"
src="PATH_TO_STATIC_REACT_ASSET.JS"></script>
{% endblock %}
you should now have your posts in your JS context and can load them in your React component. Once again: I would agree with Daniel Rosemans comment
I have an attribute of objects that can be rendered in html template like this :
{{ mymodels.something }}
In my case, i have forms containing an input field that has name similar with "something", so i want to run something like this in my template:
{% for form in my_form %}
{{ mymodels.form.name }} <!-- is same as mymodels.something -->
{% endfor %}
but it can't be rendered..
How can i do something like that?
Forms are something to gather data on client side - send it over to server side which validates them and does something with them (e.g. writing into models, or maybe passing it back into the context of your next rendering).
When your template is rendered, it also renders your form (building the markup, providing initial values) - when the template engine finished it's job, the HTML code is sent to the browser.
If you want to access form data on rendering time, there will always be the same value: the initial value. Which does not make much sense. As you might just pass the same data to your rendering context and use it from there.
Read the docs for further information on forms, especially the part rendering fields manually might interest you - if your question aims into this direction.
Is it considered correct/ are there any pitfalls in returning partial templates to ajax POST requests?
For example:
if request.is_ajax:
# response is just the form
return render(request, 'contact/fields.html', {'form':form})
The most typical approach is returning JSON and then contructing whatever HTML you need client-side from the JSON data. However, it could be argued that this is mixing presentation with behavior and it would be better to clearly separate out the HTML.
On the flip-side, returning a block of HTML is about as polar-south of "RESTful" as you can get. In pure REST philosophy, the views should return data in a standard and reusable container (such as JSON or XML). Later if you needed to pull the form into an iOS/Android/WP7/etc. app environment rather than a webpage, the JSON/XML will serve you just as well, whereas the HTML is virtually useless.
I can easily see both arguments, and I don't think one is necessarily more right than the other. Ultimately, I think you just have to do what works best for your app and what "feels right" to you. Think in terms of what is more maintainable and extensible for your particular circumstances.
I have no yet tried this with form POST request but very similarly we return partial HTML to AJAX GET request to do page changes without loading the whole page. I think it would work well for form request as well (we are in fact debating at the moment whether to use this approach on a page with multiple different forms).
I think if done right it is not a bad design pattern. We accomplish this by changing which base template is extended based on whether it was an AJAX call.
A simplified example:
### view
base_template = "base.html"
if request.is_ajax():
base_template = "base-ajax.html"
return render_to_response(request, 'page.html', {'base_template': base_template})
### page.html
{% extends base_template %}
{% block main %}new page content{% endblock %}
### base.html
<html>
<!-- complete html page -->
...
{% block main %}this is what changes per page{% endblock %}
...
</html>
### base-ajax.html
{% block main %}this is what changes per page{% endblock %}
I think most of my ajax return responses return DOM elements and not the entire form.
(an example)
...
person = ''' {1} {2}'''.format(p.id, p.first_name, p.last_name)
q = simplejson.dumps({"person":person})
return HttpResponse(q, mimetype="application/json")
The above sends back a simple DOM element to the template and inserts it into a table at the bottom. Sliding in right to left.
If you send back the entire template, the entire screen will flash and won't get any slick animations. That is my understanding anyway.
I have done this for a couple of jQuery Dialogs. The dialog contents are requested by a AJAX request, the contents are rendered on server side and then returned and displayed. So far I have experienced no problems with that.
From personal experience i can say that there is no pitfalls. I just not very good design pattern (not good practice). I used to do that on pretty high loaded project.
I have had the same problem in the year 2023 (question was asked in 2012..). Django still doesn't support rendering a template without invoking the whole chain of template inheritance.
So here's a basic way of rendering partial template:
from django.template import Context, loader
def render_template_to_string(template_name, context=None):
template = loader.get_template(template_name).template
context_instance = Context(context or {})
with context_instance.bind_template(template):
return template.nodelist.render(context_instance)
And your (partial) template can be as simple as:
<ul>my partial list
{% for opt in options %}
<li>{{ opt }}</li>
{% endfor %}
</ul>
You then invoke it by e.g.
from django.http import HttpResponse
resp = render_template_to_string(
"path/to/partial/html", {"options": range(10)}
)
return HttpResponse(resp)
FYI you might want to add support for some node exclusions, error handling, or better flexibility of what gets passed to render_template_to_string.