ValueError:int以10为底的无效文字:''

时间:2019-08-18 14:39:14

标签: python django

尝试启动Django服务器时出现以下错误。

> python manage.py runserver 0.0.0.0:8000
> 
> Traceback (most recent call last):   File "manage.py", line 10, in
> <module>
>     execute_from_command_line(sys.argv)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 363, in execute_from_command_line
>     utility.execute()   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 307, in execute
>     settings.INSTALLED_APPS   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/conf/__init__.py",
> line 56, in __getattr__
>     self._setup(name)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/conf/__init__.py",
> line 41, in _setup
>     self._wrapped = Settings(settings_module)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/django/conf/__init__.py",
> line 110, in __init__
>     mod = importlib.import_module(self.SETTINGS_MODULE)   File "/opt/python/python-2.7/lib64/python2.7/importlib/__init__.py", line
> 37, in import_module
>     __import__(name)   File "/u/agrawalo/beatthestreet/beatthestreet/config/settings.py", line 96,
> in <module>
>     'PORT': config('DB_PORT', cast=int),   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/decouple.py",
> line 197, in __call__
>     return self.config(*args, **kwargs)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/decouple.py",
> line 85, in __call__
>     return self.get(*args, **kwargs)   File "/u/agrawalo/beatthestreet/lib/python2.7/site-packages/decouple.py",
> line 79, in get
>     return cast(value) ValueError: invalid literal for int() with base 10: ''

.env中的内容

SECRET_KEY=BeatTheStreet

DEBUG=False

ALLOWED_HOSTS=['*']

EVENT_STARTED=True
EVENT_ENDED=

# Production database details
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=

2 个答案:

答案 0 :(得分:1)

您可以像这样重现错误:

# python
>> int('')  # forcing an empty string to integer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

基本上在Django中,它的获取端口值是空字符串。因此,您可以检查是否正确读取了端口的值,或者在没有PORT的情况下提供了默认值(或者可以从.env文件中删除该值):

import environ

import environ
env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
environ.Env.read_env()
PORT = env.int('DB_PORT', default=5432)

答案 1 :(得分:1)

您需要为数据库配置设置值。当前DB_PORT设置为空/空,因此无法将其转换为int