$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# for python2
brew install python
# for python3
brew install python3
#for python2. pip3 comes with python3.
sudo easy_install pip
# for python2
pip install virtualenv
# for python 3
pip3 install virtualenv
First create the projects directory.
mkdir django-project
cd django-project
Create a virtual environment. The name venv can be whatever you like.
# for python2
virtualenv venv
# for python3
virtualenv -p python3 venv
Active virtual environment
source venv/bin/activate
Install Django and other needed packages
pip install django
pip install <packages>
To get out of venv
deactivate
Activate virtual environment
source venv/bin/activate
Create Django project
django-admin startproject testsite
Create Django app
cd testsite
python manage.py startapp myapp
To run code on mac.
python manage.py runserver
Go to http://127.0.0.1:8000/ to see code in action.
Get mysql from Brew
brew install mysql
Install the components for python.
source venv/bin/activate
pip install mysqlclient
# if you run into errors, you may need to relink openssl in brew.
brew reinstall openssl
brew link --force openssl
# copy and past instructions to terminal and run.
Turn on mysql with brew
brew services start mysql
Create mysql user.
mysqladmin -u root password 'yourpassword'
Access mysql through the command line to create your database.
mysql -u root -p
Create database
CREATE DATABASE my_database;
use my_database;
-- show tables
show tables;
-- show databases
show databases;
Back to the virtual environment, we need to create all the needed project tables. Run:
python manage.py makemigrations
Resolve any errors… here would be a good place to use requirements.txt file :)
python manage.py migrate
Login to the server, navigate to where you want to put the dump. Run this.
mysqldump -u root -p --opt databasename > databasename.sql
Next, on your new install (Mac or other server) you can pull in that database.
# for files
# sudo scp 'user'@'server':path/to/file.txt /path/on/host
# for folders
# sudo scp -r 'user'@'server':path/to/folder /path/on/host
sudo scp 'admin'@'server':~/database.sql ~/Documents/Projects/django-project/
Finally the database can be imported on the new install. You must have already created the database in mysql.
mysql -u root -p hostdatabasename < databasename.sql
Replace the line with “STATIC_ROOT” in settings.py with
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Add the following snippet to the end of your urls.py file
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
if settings.DEBUG == True:
urlpatterns += staticfiles_urlpatterns()
Activate the virtual environment and run this to create a listing of required modules.
pip freeze > requirements.txt
Create git. Don’t forget to update .gitignore!
$ git init
$ touch .gitignore
$ git add .
$ git status
$ git commit -m 'initial commit'
$ git remote add origin https://github.com/username/reponame.git
$ git push -u origin master