Django(5) Connect Django With MySQL Database

You can follow this article to make Django get connection with MySQL Database.

Connect Django With MySQL Database

  1. First you should launch XAMPP and open phymyadmin page. And then create a new table called “mydjango” (or any name you want).

  2. Delete the unused file called “db.sqlite3”. Django(5)_Connect_Django_With_MySQL_Database_1.png

  3. Next in settings.py file, change the DATABASE code as following.

DATABASES = {
    
    
    'default': {
    
    
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydjango',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3316',      # you should check your own port, usually it should be '3306'
    }
}
  1. After that, in pycharm terminal, input this command python manage.py migrate. Django(5)_Connect_Django_With_MySQL_Database_2.png

  2. Until now, open mydjango database, you will see below. Django(5)_Connect_Django_With_MySQL_Database_3.png

  3. Now, when you try to get into your admin page using your password set before, you will get an error. Django(5)_Connect_Django_With_MySQL_Database_4.png

  4. To fix this, you should create a new superuser for this. So in pycharm terminal, input python manage.py createsuperuser. You can just create a new superuser with the same information as before to cover the old message. Django(5)_Connect_Django_With_MySQL_Database_5.png

  5. Finally, refresh admin page and login again. You will see this page if you get successful. Django(5)_Connect_Django_With_MySQL_Database_6.png

So far, you have connected Django with MySQL successfully. Hopefully, it helps.

猜你喜欢

转载自blog.csdn.net/Moses_SU/article/details/127681572