Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get current user in a template tag?

Tags:

How can i get the current user in a django template tags? (request object is not accessible) Or how can i access to request object?

like image 642
tapioco123 Avatar asked Feb 11 '13 15:02

tapioco123


People also ask

How do template tags work Django?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.

How do I create a custom template tag?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

Which tag is used to load another template in current template file?

include tag loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.


1 Answers

If you want to access the current user in a template tag, you must pass it as a parameter in the templates, like so:

{% my_template_tag user %} 

Then make sure your template tag accepts this extra parameter. Check out the documentation on this topic. You should also check out simple tags.

like image 130
msc Avatar answered Oct 12 '22 22:10

msc