1👍
I think you have some issues with ‘ and ” not properly closed, try this instead:
var injecthtml = '<a href="/register/"></a>';
{% if user.is_authenticated %}
injecthtml = '<a href="'+datainfo.id+'"></a>';
{% endif %}
$("#somediv").html(injecthtml);
0👍
If you’re grabbing HTML from the server via AJAX, just modify the html on the server side and pass the correct information back.
For instance, user.is_authenticated will be true or false on the client side, so if you grab that and pass in the boolean value to your ajax call, the server can return the correct HTML for you to use, you don’t need to pass in template style html.
Otherwise just request DATA from your server and then modify the html with the data you get back.
Django Templates replace all of that on the server side before the client ever gets it anyway, so you just want to replicate that if you’re getting a specific piece of html.
Your question is a little confusing, but I think that’s what you’re looking for?
That piece of javascript won’t work… django templates are rendered on the server side, so “{% %}” doesn’t mean jack squat in html/javascript unless you have a client side templating engine (which is separate from django).