Considered you have this dictionary
MESSAGE = {
'0': 'Success',
'1': 'Unknown error',
'2': 'Failed',
}
Expected display as Success if code='0'
- Create a package name
templatetagsin your app and a file for customer filters (for example,custom_filters.py). Then the directory tree should be:
my_project/
|-- my_app/
|-- templates/ # templates are stored here
|-- templatetags/
| |-- __init__.py
| |-- custom_filters.py
|-- urls.py
|-- views.py
- In
custom_filters.pyfile:
# coding=utf-8
from django import template
# For XSS prevent
from django.template.defaultfilters import linebreaksbr, urlize
MESSAGE = {
'0': 'Success',
'1': 'Unknown error',
'2': 'Failed',
}
register = template.Library()
@register.filter(name='get_message')
def get_message(response_code):
if response_code not in MESSAGE:
return u'Error: not defined'
return linebreaksbr(urlize(MESSAGE[response_code], autoescape=True), autoescape=True)
- In template file, for e.g.:
templates/list.html, add this line{% load get_message %}below extends tag. Then use like this
{{ object.response_code|get_message }}
- Add these line at the end of file
if __name__ == '__main__':
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bank_giro.settings")
# YOUR CODE RUN HERE
- In terminal, run this command:
export PYTHONPATH=$PYTHONPATH:path/to/project/source/code # this will be lost after you logout
- Run python file
python path/to/python/file
# Or using virtual environment
path/to/virtual-environment/bin/python path/to/python/file
Add these configurations into settings.py file:
SESSION_COOKIE_DOMAIN = None # Just remove the SESSION_COOKIE_DOMAIN setting or set it to None. Django will automatically use the current domain.
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_HTTPONLY = True
SESSION_COOKIE_AGE = 5 * 60Please take a look at Create connection from Ubuntu to MS SQL Server
# requirements.txt
Django==1.11.6
django-pyodbc-azure==1.11.0.0
pyodbc==4.0.21
pytz==2017.3DATABASES = {
'default': {
'NAME': 'database_name',
'ENGINE': 'sql_server.pyodbc', # Must be
'HOST': 'MSSQLDev', # Which is configured in /etc/freetds/freetds.conf file
'USER': 'python',
'PASSWORD': 'python',
# 'PORT': '1433',
},
}