Connect Mysql With Django


How To Connect MYSQL With Django
When you make a Django project it comes with a build in database named SQLite which is for testing your project so now you are in a stage when you want to deploy your project then you have two choices one is to use a cloud or a big database like Oracle , Mysql , etc . So i will show you how to link your Mysql database with Django. Now here are some few steps you have to do.

1 Download Mysql
Now first you need to install mysql to do that go to this link
 https://dev.mysql.com/downloads/installer/
Once you have opened the link you need to download this file
Download the file that i highlighted and install mysql.Once you have done then open up mysql i will use Navicat for this purpose then create a database and fill all of these things and remember them because we will use them while building a connection with django.

once you have filled all of them click test Connection if successful then click OK if not successful then check if the same user doesn't exist .
2 Download Mysql Client 
Now you need to download mysqlclient so that it can create a bridge between mysql and django .
To download mysqlclient go to this link  https://pypi.python.org/pypi/mysqlclient  now once you go to this link you will see these different files

Now you have to download the source file and once you have downloaded it now go to your CMD or Power Shell and then we will install mysql form there .First you have to navigate where the mysqlclient is so my file is on the desktop so i will do is this
then you just have to type "python setup.py install" and press Enter then it will start installing .

3 Setting Up Database
Now once you have install mysql and mysqlclient now you have to open up your project file and go to settings.py and then type the following things

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysql',
        'USER': 'your username',
        'PASSWORD': 'your password',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
Now once you have done this go to CMD or Power Shell navigate to your project folder and type "python manage.py migrate" and it will apply all the settings .Now in order to check weather the settings have been applied open up mysql i will use Navicat and go to your database and search for the table i have created a table named which i made it in my models.py named is posts_post

Now if you are wondering what i have written in my models.py then this is what i wrote
from django.db import models
from django.core.urlresolvers import reverse

class Post(models.Model):
    title = models.CharField(max_length=120)
    Image = models.FileField(upload_to='images')
    content = models.TextField()
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"id": self.id})

    class Meta:
        ordering = ["-timestamp", "-updated"]

4 Conclusion
Now today we just learned how to connect your mysql with django which is so simple to do .So if you have any problems please let me know and thanks for coming.

Comments