Due to the fact that I couldn't find anywhere that actually spelled it out explicitly, here's the minimum you need in your settings.py file if you're using a MySQL DB for your Django project and you're using an external DB server (ie, not localhost):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name',
'HOST': '192.168.0.1',
'PORT': '3306',
'USER': 'user',
'PASSWORD': 'password'
}
}
It's probably obvious to everyone, but who knows - it might save someone some hassle some day.
Tuesday, 9 February 2016
Baby steps with Python and Django
I've neglected this blog lately, but I've decided to pick it up again. I've started a new project for my self in Python and I want to keep a bit of a track of what I've learned.
I've basically been following the (rather excellent) Django tutorials so far, but today I wanted to start deviating. Here's how I've gotten to where I am with the project so far:
Used Homebrew to install Python 3 (my Mac comes with Python 2.7 installed by default, but I wanted to go with the latest version):
I've basically been following the (rather excellent) Django tutorials so far, but today I wanted to start deviating. Here's how I've gotten to where I am with the project so far:
Used Homebrew to install Python 3 (my Mac comes with Python 2.7 installed by default, but I wanted to go with the latest version):
$ brew install python3
Then it was time to set up a Virtual Environment for my project. According to this Ask Ubuntu post, virtualenv isn't set up to run with Python3 yet, so I used pip3 rather than the normal pip.
$ sudo pip3 install virtualenv
...
$ virtualenv-3.3 project
I'm using PyCharm to manage my project, which comes with a built in Terminal window and Python Console. Within the Terminal window, we need to use the virtualenv for the project:
$ source bin/activate
Then it's a case of installing Django
$ pip install django
Installing the MySQL connector
$ pip3 install mysqlclient
Oops - but that didn't work: OSError: mysql_config not found. Turns out the mysqlclient connector for Python needs MySQL (or at least the developer library) installed. Despite it being about running on Linux, I found this Stack Overflow post quite useful. Back to Homebrew to fetch MySQL:
$ brew install mysql
After that mysqlclient installed without a project. Finally it's time to create a project:
$ django-admin startproject project
And there you go - all of the building blocks are in place to start things off!
Subscribe to:
Posts (Atom)