I need to use the mysql backend in Django. Documentation in the settings file says just use 'django.db.backends.mysql', and this is also documented at https://docs.djangoproject.com/en/dev/ref/settings/#engine
When I set a database setting engine to 'django.db.backends.mysql' Django errors out.
I've found references on the web that one needs to install MySQLdb. However, I look in my site-packages where I find django/db/backends/sqlite3 and django/db/backends/mysql.
So, it looks like I have the Django back ends in place. Do I have the MySQL back end installed but have a configuration problem, or do I need to install the MySQLdb package to allow the backend to work?
django.db.backends.mysql is a wrapper around MySQLdb; you will need to have MySQLdb installed for it to work, or it will raise an ImproperlyConfigured exception on startup.
The Django database backends generally work like this -- they rely on a lower-level database interface library, but they provide a uniform interface to the ORM layer.
The only backend that will work "out-of-the-box" is SQLite, but that backend is no different, it's just that the SQLite interface is built into the python distribution, so it's almost guaranteed to be present.
You will have to install MySQLdb in order to use MySQL as db backend.
If you read the documentation, it states,
If you plan to use Django's database API functionality, you'll need to
make sure a database server is running.
So yes, you need to install a MySQL server in order for the backend to work.
Related
I am looking for a solution to work with Postgres and MongoDB simultaneously in my Django project.
I have large data which I would like to store in MongoDB and Postgres for user management, billing, and other things to manage.
I have tried to use a library ```pip install djongo`` djongo, which is outdated, and not able to find any solution. Please guide me with an authentic solution! Thanks
First of all, To install a newer version of Django you can use pip install Django==<version>, But you need to see compatibility with the Python version you're running on your system and virtual environment.
e.g. For Python 3.x, I'm using a bit older Django version 2.2.21.
Second, Django does not support MongoDB natively, But MongoDB team themselves provides a library called pymongo which you can install, but with pymongo you'll have to write more of a native mongodb queries. They do not have any Django ORM.
But there's another library called mongoengine which is actually built on top of pymongo and is very handy and close to the ORM Django provides for relational databases.
Thirdly, About Postgres you can easily set up any relational database with Django as it provides native support for relational databases. Both the database are completely independent of each other, So there should not be any problem with the setup.
I, myself use Postgres and MongoDB together in my projects, Never faced a single problem. Although, sometimes it's a little challenging to build things for NoSQL databases in Django which Django already provides for relational databases but yeah a little extra code as overriding Django base classes to provide support for mongodb always works.
I have written a web app in django using mongoengine as a driver and am trying to host my database on mongodb cloud. In mongoengine's connect() method, according to what I understood from the documentation, specifying the url (specified in the connect section on the mongo cloud site) in the hostname argument should do the trick, however requests from the app to the mongo cloud keep timing out.
Where am I going wrong?
This is the call to the connect method in my setting.py file-
mongoengine.connect(db='scheduler', host='mongodb+srv://<user>:<password>#cluster0.oualt.mongodb.net/<db name>?retryWrites=true&w=majority')
Update: the specific error thrown on using the above url is this-
ServerSelectionTimeoutError
If you're using just mongoengine then Django will not detect that connect to the database, so you will not be able to use the Django ORM system to save instances of models etc.
If you want to use the ORM you would have to use a package like Djongo or djmongoengine
However, if you're not planning to use the ORM system then you can use just mongoengine.
I believe the connect url should be in this format:
mongodb+srv://<username>:<password>#<database-name>.mongodb.net/test?retryWrites=true&w=majority
Right now you seem to have the url to a specific cluster on your database.
So I was going through similar posts which had the same error ServerSelectionTimeoutError with djongo and pymongo as the drivers and one of the posts said that they had forgotten to whitelist their IP address on the mongo cloud site. I had done that during signup, but I repeated it anyways and for some reason the IP address was different and after that had been whitelisted, my app connected and started working. I have no explanation.
mongoengine works fine, but in terms of protocols you need another ext file that serves for your connection protocols, just pip install pymongo[srv]
def register_database(db_name:str):
connect(db=f"mongodb+srv://<username>:<password>#<cluster-name>.nwr5l.mongodb.net/{db_name}?retryWrites=true&w=majority", alias="default")
I developed an app in Django, mainly for educactional purposes, and I want to host it on GAE. The app uses MySql and Haystack/Whoosh. I'm following this django-nonrel guide, but I'm not sure if Haystack/Whoosh will work on the NoSQL from GAE. Any ideas?
It's possible to use Haystack on GAE, but you can't use whoosh as the search backend. Instead, you'll need to use a hosted Solr or ElasticSearch as a backend search server. Try searchbox.io, they tell you how to get it working with Haystack here.
I don't know about haystack on appengine but there is a nonrel-search which I is designed to work on appengine and NoSQL databases.
Also I don't think GAE supports reletional database like Mysql.
Whoosh is a file-based search backend so as long as you have write access to a directory then you should be able to use it.
You need to configure the directory of the search index in your Django application and run the appropriate command (./manage.py rebuild_index or ./manage.py update_index) to build the search index into that directory.
I am completely new to this Django world. I haven't tried it ever before.
Now the problem is as below;
One of my clients was hosting his site somewhere else that I don't know and they built the site using Django. The host company doesn't allow to make any changes on their server, instead they provided the zip file for all the files in the site to me; so that now I can host my client's site.
As I don't know anything about Django, can someone please shed a light where I should start from?
Thanks in advance.
Cheers.
Sach
First of all, install Django on the development machine. Start by trying to get the development server run on your machine.
Gather requirements: check the settings.py for installed apps against the default Django settings.py file. See if there are any popular django apps that site depends on. If there are any, then you probably will have to install them, too.
In which format was the database provided? Will you move to another more appropriate format? Python bindings for databases are required too.
Considering the fact that you have inherited this project and probably will need to make some changes, consider installing django-south, so you can easily make changes to the database schema.
If you get the site running properly on your own machine, consider deplyoment. Is there a lot of static content? (if so, consider nginx). Set up apache2 and install the mod_wsgi module. Deploy.
Work your way through the Django tutorial first. Then look into Django Book as has been mentioned. Django IRC channel (#django) on Freenode is also great for help.
Your best bet would be to learn about Django before trying to jump in head first - https://www.djangoproject.com/ contains documentation as well as tutorials on creating Django apps.
Django is fairly easy to setup if you already have the code written. You'll need to install the chosen database and then simply follow the tutorial on the Django website
Django comes with a built-in server so it's very easy to run the website for development without needing Apache, nginx or much else.
I learned using the Django Book. Django is an easy-to-use framework, you should be fine.
Also, in the short-term there's a file called views.py and separate folder containing templates. If you're familiar with MVC (MVT in Django) this contains the views for the site in function form. There's probably (but not always) a folder for templates which contains a lot of the HTML for the site. Just a good starting pointing for basic modifications.
You can perhaps start here. https://docs.djangoproject.com/en/dev/howto/deployment/
First, find out the django version required by your client. Install that on a server (not a production one), setup apache and mod_wsgi. The zip files may go to a dir which can be included in the mod_wsgi configuration.
Find about the static files and setup apache or any other lightweight webserver to server it.
You may not be a developer, but have a try with the django book. It can give you a good idea how its structured.
There seem to be 2 ways to use django 1.1 with GAE
Google App Engine helper for django
The new use_library() function
We currently use the first. Should we switch? And what's the difference between the two?
use_library loads an unpatched version of django in the production environment, so many things will not work out of the box on app-engine.
The helper applies a series of patches to the django libraries to enable things like Sessions, test, cache framework, etc. If you do not add your own copy of django into your helper application and you are using the latest release (r100 or higher), the helper first tries to load django 1.1 and if it does not succeed then loads 1.0. You can see this in appengine_django/__init__.py::LoadDjango.
On production GAE, django 1.1 always exists, so it is loaded first.
However, on your development environment the dev server SDK does not distribute Django. Therefore, it uses whatever version of Django it can find, first trying 1.1 and then 1.0 and if it cannot find one then throws UnacceptableVersionError.
You probably want to use the helper and not use_library because then you will need to patch the raw django libraries yourself, thus duplicating the work in the helper. Whether you distribute your own version of django, either as a folder or zip file is up to you. One of the advantages of not distributing your own copy of django is that as google applies security patches you automatically get them without having to redeploy your application.
the replacement is called django-nonrel (and djangoappengine)... you can find it at
http://www.allbuttonspressed.com ... with django-nonrel, you should be able to run pure Django apps on top of App Engine without tweaking your models!
FYI, there is at least one more way to get Django 1.1 in GAE.
Take a look at http://code.google.com/p/app-engine-patch/
It allows use to use most of Django features including Admin.
app-engine-patch seems to have died:
http://code.google.com/p/app-engine-patch/issues/detail?id=253
As of GAE 1.5.0, there's a much simpler way of specifying a Django version.
In appengine_congif.py, include the line
webapp_django_version = '1.2'
This will cause the use_libary() to happen under the covers.