I am somewhat new to coding and wanted to start my first Django app with a MySQL backend. I've used this setup for almost a year now on my windows machine, but it was an inherited codebase - and I unfortunately never tried to build a new app from scratch. Furthermore, I recently purchased a used MacBook to familiarize myself with the OS and get more accustomed to using Unix (since all of our code runs from Ubuntu/RedHat servers) - so I'm still in the 'finding out how to get around town' phase.
Below are some of the difficulties/errors I ran into, and the answers that worked for me (and weren't necessarily that easy to find). This is not 'This is how you should solve this problem', but merely a repository for how I resolved some issues - and hopefully learn how I should have done it. Feel free to add more!
Change the default Python Version
- I needed to use Python 2.4 for a short time. When I no longer needed to use Python 2.4 consistently, I wanted to change it back to Python 2.7.
One way I was able to do this was to change my export viaexport PATH=/usr/bin:$PATH
. This only worked for my current terminal shell - not that helpful.
I finally found that changing the symlink in /Library/Frameworks/Python.framework/Versions/Current from -> 2.4 to 2.7 solved my problem. I triedln -f -s 2.7/ Current
but this didn't work. I ultimately moved my Current dir to Current_back then created the new symlinkln -s 2.7/ Current
- I needed to use Python 2.4 for a short time. When I no longer needed to use Python 2.4 consistently, I wanted to change it back to Python 2.7.
MySQL
- Problem 1: The installer couldn't find the correct path for MySQL_config
"EnvironmentError: mysql_config not found"
I found several people pointing out that you need to edit the 'setup_posix.py' file and set
mysql_config.path = "/usr/local/mysql/bin/mysql_config"
but I ultimately had to change this in the site.cfg file. - Problem 2: I consistently received a 'Library Not Loaded' error anytime I tried to import MySQLdb. To resolve this, I updated my bash profile and set
DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
- Problem 1: The installer couldn't find the correct path for MySQL_config
"EnvironmentError: mysql_config not found"
I found several people pointing out that you need to edit the 'setup_posix.py' file and set
Django
- Segregating and Separating models.py:
I like putting things into containers, and I wanted to use multiple 'models' files in a 'models' directory rather than one large models.py file. The trick is, you need to inform everyone where the models are and how to access them. - Add an init.py file to your new 'models' directory. In this file, make sure you import all of the classes from your models. If you have data1.py and data2.py, you will need something like this in init.py:
from data1 import *
from data2 import *
- Add the app name to the metadata for each model:
o
class Data(models.Model): foo = models.CharField(max_length=200) class Meta: app_label = 'app_name'
- Segregating and Separating models.py: