I am creating a app in django and I want to fetch data from mongolab.
But,I don't know how to connect mongolab to django.
I had connect mongodb to django but now I want to connect with mongolab.
How to do this?
I have used this in my setting.py file,
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'djangotoolbox',
'app'
)
And in DATABASE:,
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': '*****',
'USER': '*****',
'PASSWORD': '******',
'HOST': 'ds129469.mongolab.com',
'PORT': '26469'
}
}
And after running command, python manage.py runserver
I get following error,
life#life-All-Series:~/mongo/env1/project7/project$ python manage.py runrver
/home/life/.local/lib/python2.7/site-packages/djangotoolbox/db/utils.py:1: RemovedInDjango19Warning: The django.db.backends.util module has been renamed. Use django.db.backends.utils instead.
from django.db.backends.util import format_number
/home/life/.local/lib/python2.7/site-packages/djangotoolbox/db/utils.py:1: RemovedInDjango19Warning: The django.db.backends.util module has been renamed. Use django.db.backends.utils instead.
from django.db.backends.util import format_number
Performing system checks...
System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x7fbad4b24c08>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check_migrations()
File "/usr/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 168, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/usr/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 19, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 47, in __init__
self.build_graph()
File "/usr/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 191, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/usr/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/home/life/.local/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 179, in table_names
return self.connection.database.collection_names()
File "/home/life/.local/lib/python2.7/site-packages/pymongo/database.py", line 488, in collection_names
ReadPreference.PRIMARY) as (sock_info, slave_okay):
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/home/life/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 699, in _socket_for_reads
with self._get_socket(read_preference) as sock_info:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/home/life/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 663, in _get_socket
server = self._get_topology().select_server(selector)
File "/home/life/.local/lib/python2.7/site-packages/pymongo/topology.py", line 121, in select_server
address))
File "/home/life/.local/lib/python2.7/site-packages/pymongo/topology.py", line 97, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: ds129469.mongolab.com:29469: [Errno -2] Name or service not known
Related
Each time i include the Django Rest Framework in my installed apps in my settings.py file, I get an error while starting my server.
The Error Message below:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File"C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner self.run()
File"C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run self._target(*self._args, **self.kwargs)
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs)
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run autoreload.raise_last_exception()
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception raise exception[1]
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management_init.py", line 375, in execute autoreload.check_errors(django.setup)()
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs)
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django_init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS)
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 122, in populate app_config.ready()
File "C:\Users\gchukwuma\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\apps.py", line 10, in ready from .checks import pagination_system_check # NOQA
ValueError: source code string cannot contain null bytes
The Settings.py file:
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'base.apps.BaseConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework'
]
In settings file, I have given SQL SERVER Database connection values like this.
DATABASES = {
'default': {
'NAME': 'AdventureWorks2014',
'ENGINE': 'sqlserver_ado',
'HOST': '127.0.0.1',
'USER': '',
'PASSWORD': '',
}
}
Versions:
Django v:1.11
Python v:2.7
django-mssql v:1.8
pip v:9.0
Development Platform:
Visual Studio
After the database connection values changes, I have used the command
python manage.py makemigrations
Here I got the error:
Executing manage.py makemigrations
Traceback (most recent call last):
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\manage.py", line 17, in
execute_from_command_line(sys.argv)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\core\management__init__.py", line 364, in execute_from_command_line
utility.execute()
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\core\management__init__.py", line 338, in execute
django.setup()
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models()
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:\Python27\Lib\importlib__init__.py", line 37, in import_module
import(name)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\contrib\auth\models.py", line 4, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\contrib\auth\base_user.py", line 52, in
class AbstractBaseUser(models.Model):
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\db\models\base.py", line 124, in new
new_class.add_to_class('_meta', Options(meta, app_label))
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\db\models\base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\db__init__.py", line 33, in getattr
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\db\utils.py", line 212, in getitem
conn = backend.DatabaseWrapper(db, alias)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\sqlserver_ado\base.py", line 184, in init
super(DatabaseWrapper, self).init(*args, **kwargs)
File "E:\Django Projects\DjangoMSSQLDatabaseConn\DjangoMSSQLDatabaseConn\VirtualEnv\lib\site-packages\django\db\backends\base\base.py", line 96, in init
self.client = self.client_class(self)
TypeError: Error when calling the metaclass bases
'NoneType' object is not callable
The interactive Python process has exited.
Can anyone help me solve this?
I've got an existing Django 1.10 application and I want to create a new db locally from scratch.
Following these steps
$ createdb -h localhost -p 5432 -U me -w my_db # OK
$ ./manage.py migrate
I'm getting an exception
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 23, in ready
self.module.autodiscover()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/me/workspace/myproject-django/customauth/admin.py", line 7, in <module>
from core.admin import ShopInline
File "/home/me/workspace/myproject-django/core/admin.py", line 242, in <module>
admin.site.register(Campaign, CampaignAdmin)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 108, in register
admin_obj = admin_class(model, self)
File "/home/me/workspace/myproject-django/core/admin.py", line 104, in __init__
last_task = TaskState.objects.filter(name='core.tasks.compute_metrics', state='SUCCESS').order_by('tstamp').last()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 565, in last
objects = list((self.reverse() if self.ordered else self.order_by('-pk'))[:1])
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/me/workspace/myproject-django/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "celery_monitor_taskstate" does not exist
LINE 1: ...ker_id", "celery_monitor_taskstate"."hidden" FROM "celery_mo...
Here is the relevant part of my requirements.txt
celery==4.0.2
django-celery-beat==1.0.1
django-celery-monitor==1.1.0
django-celery-results==1.0.1
And here is my in INSTALLED_APPS
INSTALLED_APPS = (
'modeltranslation',
...
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.postgres',
'django_countries',
'django_extensions',
'django.contrib.humanize',
'django_celery_results',
'django_celery_beat',
'django_celery_monitor',
...
)
I tried to do a
$ ./manage.py migrate django_celery_monitor # also tried for _results and _beat
but it throws the same error.
Ok the problem came from a piece of my code in the admin module that was trying to retrieve a TaskState during initialisation. Hence the exception was raised even before any migration could be performed.
Following the MongoDB tutorial (http://django-mongodb-engine.readthedocs.org/en/latest/tutorial.html), I have a model called Post in an app called 'tutorial.' I have the app, along with djangotoolbox, installed:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'djangotoolbox',
'tutorial'
)
I have the following connection settings with a name, user, and password that is set up on MongoLab:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': '*****',
'USER': '*****',
'PASSWORD': '******',
'HOST': 'ds047720.mongolab.com',
'PORT': '47720'
}
}
When I SSH into my host, I can run manage.py validate and I get "0 errors found." When I go into the shell and follow the tutorial to create a new Post, this is what happens:
(InteractiveConsole)
>>> from tutorial.models import Post
>>> post = Post.objects.create(
... title='123',
... text='test',
... tags=['a','b']
... )
Traceback (most recent call last):
File "<console>", line 4, in <module>
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/query.py", line 416, in create
obj.save(force_insert=True, using=self.db)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/base.py", line 548, in save
force_update=force_update, update_fields=update_fields)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/base.py", line 668, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django/db/models/query.py", line 1675, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", line 592, in execute_sql
key = self.insert(to_insert, return_id=return_id)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/compiler.py", line 84, in wrapper
return func(*args, **kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/compiler.py", line 387, in insert
collection = self.get_collection()
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/compiler.py", line 296, in get_collection
return self.connection.get_collection(self.query.get_meta().db_table)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 196, in get_collection
collection = self.collection_class(self.database, name, **kwargs)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 204, in __getattr__
self._connect()
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 261, in _connect
self.connection = connection_class(**conn_options)
File "/home1/kevinlak/tabenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 369, in __init__
raise ConnectionFailure(str(e))
DatabaseError: timed out
>>>
Obviously, Django is having a difficult time connecting to the database, but I'm able to connect just fine using Mongo Management Studio. I've read similar tutorials here: http://compsocsci.blogspot.com/2012/02/getting-started-with-django-heroku-and.html
and here:
http://dennisgurnick.com/2010/07/06/bootstrapping-a-django-with-mongo-project/
but I have no idea what's stopping django from connecting to my database. Has anyone overcome a similar issue with the mongodb engine? Does anyone know what I'm missing?
I am trying to install django-notification app[1] and I get an error when I give manage.py syncdb.
The error states: EnvironmentError: NOTIFICATIONS was not found.
I have installed django-notification using setup tools successfully. This is my stack trace.
File "C:\Tools\Python26\lib\site-packages\django\core\management\__init__.py",
line 438, in execute_manager
utility.execute()
File "C:\Tools\Python26\lib\site-packages\django\core\management\__init__.py",
line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Tools\Python26\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Tools\Python26\lib\site-packages\django\core\management\base.py", line 219, in execute
self.validate()
File "C:\Tools\Python26\lib\site-packages\django\core\management\base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "C:\Tools\Python26\lib\site-packages\django\core\management\validation.py", line 36, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Tools\Python26\lib\site-packages\django\db\models\loading.py", line 146, in get_app_errors
self._populate()
File "C:\Tools\Python26\lib\site-packages\django\db\models\loading.py", line 61, in _populate
self.load_app(app_name, True)
File "C:\Tools\Python26\lib\site-packages\django\db\models\loading.py", line 78, in load_app
models = import_module('.models', app_name)
File "C:\Tools\Python26\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "build\bdist.win32\egg\django_notifications\models.py", line 20, in <module>
File "build\bdist.win32\egg\django_notifications\backends\__init__.py", line 23, in get_available_backends
File "build\bdist.win32\egg\django_notifications\backends\__init__.py", line 90, in get_class_instance_by_key
File "build\bdist.win32\egg\django_notifications\backends\xmpp.py", line 33, in __init__
File "build\bdist.win32\egg\django_notifications\backends\__init__.py", line 5
5, in get_settings
EnvironmentError: NOTIFICATIONS was not found.
Any ideas what went wrong?
*Added settings.INSTALLED_APPS part (corrected 'django-notifications' entry to 'notification' as suggested)*
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli',
'filebrowser',
'tinymce',
'south',
'avatar',
'django.contrib.admin',
'notification',
'myproject.main',
)
[1] https://github.com/jtauber/django-notification
For whatever reason, you've decided to add django_notifications to settings.INSTALLED_APPS instead of notification. Fix this.