1.

Can I Connect MySQL to Django?

Answer»

Below are the supported and tested versions of Django-MySQL’s requirements:

  • Python: 3.5, 3.6, 3.7
  • Django: 1.11, 2.0, 2.1, 2.2
  • MySQL: 5.6, 5.7 / MariaDB: 10.0, 10.1, 10.2, 10.3
  • mysqlclient: 1.3

Before setting connection below prerequisite is taken into consideration-

1. MySQL is INSTALLED
2. MySQL-python package is installed (python interface to MySQL)
3. Django configured

Now let us see the STEPS to connect to MySQL -

1. Open settings.py from the directory of the Django project.

2. Update the DEFAULT key in the DATABASE dictionary. Set ENGINE, NAME, USER and PASSWORD you choose when installing MySQL. 

   # Database

        # HTTPS://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = { 'default': {               'ENGINE': 'django.db.backends.mysql',           'NAME': 'djangoApp',   'USER':'root',          'PASSWORD':'mysql',           'HOST':'localhost',         'PORT':'3306'                 },             }

3. Open a command window and activate a virtual environment and change it to Django project directory.
4. Type python maanage.py syncdb and create the underlying tables required for the Django project.

If there is no error your connection between Python, Django, and MySQL is successful.



Discussion

No Comment Found