Latest Version of Django Support
@ccc, what is the Django requirement that is a show-stopper for Pythonista?
Latest Version of Django Support
No, Pythonista does not support the latest version of Django or Python. If you are looking for more current versions of Python and packages, perhaps Pyto would be a better choice.
Latest Version of Django Support
Does Pythonista support the latest version of Django? According to the store description it runs Python 3.6 which is quite old. While it’s competitor runs 3.8.
Django install error
I tried following this: https://dev.to/jjsantos/running-django-on-an-ipad-20kb but when I try to use Stash to do pip install Django == 3.1.7, the install fails:
Querying PyPI ...
Error: Failed to fetch package release urls
[~/Documents]$ pip install urls
Querying PyPI ...
Error: Failed to fetch package release urls
Django Logging Filter with KeyError
I have trouble with the Django logging and setting the custom filter.
This is my current settings:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'filters': {
'user_filter': {
'()': 'myapp.user_logging_filter.UserFilter',
}
},
'formatters' : {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s %(current_user)s %(message)s'
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'filters': ['user_filter'],
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/debug.log',
'maxBytes': 1024102410, # 10 MB
'backupCount': 5,
'formatter': 'standard',
},
'django': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/django.log',
'maxBytes': 1024102410, # 10 MB
'backupCount': 5,
'formatter': 'standard',
},
},
'loggers': {
'my': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
},
'django': {
'handlers': ['django'],
'level': 'DEBUG',
'propagate': True
},
}
}
and the filter class is currently:
import logging
class UserFilter(logging.Filter):
def filter(self, record):
print('INSIDE')
record.current_user = 'Marko'
return True
I get the error stack:
--- Logging error ---
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/handlers.py", line 71, in emit
if self.shouldRollover(record):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/handlers.py", line 187, in shouldRollover
msg = "%s\n" % self.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/init.py", line 839, in format
return fmt.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/init.py", line 579, in format
s = self.formatMessage(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/init.py", line 548, in formatMessage
return self._style.format(record)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/init.py", line 391, in format
return self._fmt % record.dict
KeyError: 'current_user'
Call stack:
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check_migrations()
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/core/management/base.py", line 442, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in init
self.loader = MigrationLoader(self.connection)
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in init
self.build_graph()
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 62, in applied_migrations
return {tuple(x) for x in self.migration_qs.values_list('app', 'name')}
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/models/query.py", line 268, in iter
self._fetch_all()
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/models/query.py", line 138, in iter
return compiler.results_iter(tuple_expected=True, chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1017, in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
cursor.execute(sql, params)
File "/Users/zadravecm/Work/Razvoj/Filc/Plansphere/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 111, in execute
extra={'duration': duration, 'sql': sql, 'params': params}
Message: '(%.3f) %s; args=%s'
Replacing place holder with variable content using replace
@Niklas, for future reference:
- You cannot assign a value of a variable as a value in dictionary, then change the value of the variable and expect the value in the dictionary also to change.
- There is stdlib class
string.Template
that does exactly what you were attempting, even with the same$variable
syntax. - Pythonista also includes the jinja2 templating engine used by e.g. Django. It lets you write templates like this:
{% extends "layout.html" %}
{% block body %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}
Django - Have to restart Pythonista everytime I make a code change
Hey there!
I too was having this same problem, other scripts and reset routines weren't doing the job for me.
But i threw this together and it works quite well for me, near the top of the manage.py
# unload django modules to release cached files. Server reload will reflect changes in code without full Pythonista restart
if settings.DEBUG:
names = [name for name in sys.modules.keys() if name.startswith('django.')]
for name in names:
del sys.modules[name]
print(f'Unloaded {len(names)} django modules.')
Hope this helps!
- Benjamin
Creating a django project via Stash
@codecowboy also, make sure you don't have a folder or script called django -- sometimes people create a quick django.py to test functionality, but then the script that importa django imports the wrong file!
You can also check this by editing the django_admin.py to print django.file after importing.
stashconf py_traceback 1
Is useful for getting full traceback, or if you are comfortable with pdb,
stashconf py_pdb 1
Then you can check which Django is being used, etc.