7
from django.conf import settings
def my_view(self, request, *args, **kwargs):
if 'LOCAL' in settings and settings.LOCAL:
# do something
TypeError at ... argument of type 'LazySettings' is not iterableL'argomento django di tipo "LazySettings" non è iterable; come controllare quindi se esiste un'impostazione?
Come verificare se esiste un'impostazione? Altrimenti ricevo un errore di attributo.
Um ho ottenuto eccitato circa la vostra risposta. ma fallisce ancora. >>> se getattr (impostazioni, 'junk'): ... print 'Ciao' ... Traceback (chiamata più recente scorso): file "", linea 1, in File "/ Utenti /ben/Envs/menus_proto/lib/python2.7/site-packages/django/utils/functional.py", la linea 277, in __getattr__ getattr ritorno (self._wrapped, nome) AttributeError: oggetto 'Impostazioni' non ha alcuna attributo 'JUNK' –
È necessario fornire un valore predefinito come terzo argomento, ovvero 'getattr (impostazioni, 'nome', Nessuno)'. Oppure, dato che non hai bisogno del valore attuale, usa 'hasattr (settings, 'name')' che controlla solo il nome e non ne ottiene il valore. –
Ho aggiornato la risposta. –