332👍
Remember that the dot notation in a Django template is used for four different notations in Python. In a template, foo.bar
can mean any of:
foo[bar] # dictionary lookup
foo.bar # attribute lookup
foo.bar() # method call
foo[bar] # list-index lookup
It tries them in this order until it finds a match. So foo.3
will get you your list index because your object isn’t a dict with 3 as a key, doesn’t have an attribute named 3, and doesn’t have a method named 3.
- [Django]-Django – how to unit test a post request using request.FILES
- [Django]-How to convert a Django QuerySet to a list?
- [Django]-Django's Double Underscore
35👍
You can access sequence elements with arr.0
, arr.1
and so on. See The Django template system chapter of the django book for more information.
👤ema
- [Django]-Cannot access django app through ip address while accessing it through localhost
- [Django]-Passing variable urlname to url tag in django template
- [Django]-Separation of business logic and data access in django
13👍
When you render
a request to context
some information, for example:
return render(request, 'path to template', {'username' :username, 'email' :email})
You can access to it on template, for variables
{% if username %}{{ username }}{% endif %}
for arrays
{% if username %}{{ username.1 }}{% endif %}
{% if username %}{{ username.2 }}{% endif %}
you can also name array objects in views.py
and then use it as shown below:
{% if username %}{{ username.first }}{% endif %}
- [Django]-Django edit user profile
- [Django]-How to get Request.User in Django-Rest-Framework serializer?
- [Django]-Django 2 – How to register a user using email confirmation and CBVs?
Source:stackexchange.com