====== Starting django Working Tutorials Serie ====== * http://www.jeffknupp.com/blog/2013/12/18/starting-a-django-16-project-the-right-way/ * Similar tutorial website from A-Z: http://www.marinamele.com/taskbuster-django-tutorial/model-creation-onetoone-relationship-signals-django-admin ====== Preparing workspace ====== Create an isolated python env virtualenv --no-site-packages pjs_env source ./pjs_env/bin/activate pip install django pip install django-cms which django-admin.py ===== Image Support ===== Install JPEG support on host (outside virtualenv!): sudo yum install python-devel libjpeg-devel freetype-devel libpng-devel sudo yum install mysql-devel mysql-common mysql-libs Reinstall Pillow pip install -I Pillow Mysql-python pip install mysql-python ===== Posgre Database ===== * https://fedoraproject.org/wiki/PostgreSQL * http://www.if-not-true-then-false.com/2012/install-postgresql-on-fedora-centos-red-hat-rhel/ * http://www.postgresonline.com/journal/archives/362-An-almost-idiots-guide-to-install-PostgreSQL-9.5,-PostGIS-2.2-and-pgRouting-2.1.0-with-Yum.html ==== Install postgresql and postgresql-server packages: ==== dnf install postgresql postgresql-server python-psycopg2 dnf install postgresql-server postgresql-devel postgresql-contrib vir_env> pip install psycopg2 ==== Installation issues ==== When installing specific version (9.4.4) from repo, e.g., configured with ansible. psql links with older libpq. * http://stackoverflow.com/questions/28123212/postgres-9-4-0-undefined-symbol-pqhostaddr * http://stackoverflow.com/questions/34052046/how-do-i-correct-the-mismatch-of-psql-version-and-postgresql-version The libpq being found on the library path (/etc/ld.so.conf and LD_LIBRARY_PATH) is older than the version in 9.4. Use ldd /usr/lib/postgressql/9.4/bin/psql to find out which version is being linked to. – Craig Ringer Jan 24 '15 at 9:19 Check installation: dnf list installed | grep postgres postgresql.x86_64 9.5.4-1.fc24 @updates postgresql-contrib.x86_64 9.5.4-1.fc24 @updates postgresql-devel.x86_64 9.5.4-1.fc24 @updates postgresql-libs.x86_64 9.5.4-1.fc24 @updates postgresql-server.x86_64 9.5.4-1.fc24 @updates postgresql94.x86_64 9.4.9-1PGDG.f24 @pgdg94 postgresql94-contrib.x86_64 9.4.9-1PGDG.f24 @pgdg94 postgresql94-libs.x86_64 9.4.9-1PGDG.f24 @pgdg94 postgresql94-server.x86_64 9.4.9-1PGDG.f24 @pgdg94 ==== Configure PostgresSQL: ==== The database initialization could be done using following command. It creates the configuration files postgresql.conf and pg_hba.conf sudo postgresql-setup initdb When install from package: Initialize Cluster with initdb Command su - postgres -c /usr/pgsql-9.4/bin/initdb Set PostgreSQL Server to Listen Addresses and Set Port. /var/lib/pgsql/data/postgresql.conf Uncomment this two line: listen_addressses = ‘ ‘ port = 5432 or listen_addresses=’localhost’ Set PostgreSQL Permissions (pg_hba.conf ) # Local networks host all all xx.xx.xx.xx/xx md5 # Example host all all 10.20.4.0/24 md5 # Example 2 host test testuser 127.0.0.1/32 md5 Start PostgreSQL Server and Autostart PostgreSQL on Boot Start PostgreSQL Server: ## Start PostgreSQL 9.4 ## systemctl start postgresql-9.4.service ## Start PostgreSQL 9.4 on every boot ## systemctl enable postgresql-9.4.service Configuring Postgresql ==== Setting up Postgres Users ==== Changing the postgres users database password: su postgres and then run postgre's interactive shell: psql psql (9.3.2) Type "help" for help. postgres=# It might be good idea to add password for postgres user: postgres=# \password postgres Lets get back to user creation: postgres=# CREATE USER lenny WITH PASSWORD 'leonard'; postgres=# CREATE DATABASE carl OWNER lenny; Type \q and then press ENTER to quit psql. List users: \deu[+] [PATTERN] such as : postgres=# \deu+ And for all users: postgres=# \du User management: postgres=# DROP ROLE username; postgres=# CREATE USER alice WITH PASSWORD 'pAssw0rd'; Other commons: \list or \l: list all databases \dt: list all tables in the current database \connect database_name === This could be done from system shell too: === $ createuser lenny $ createdb --owner=lenny carl createuser - define a new PostgreSQL user account dropuser - remove a PostgreSQL user account Configuration The postgresql server is using two main configuration files /var/lib/pgsql/data/postgresql.conf /var/lib/pgsql/data/pg_hba.conf Changing the postgres user OS password: sudo passwd -d postgres sudo su postgres -c passwd ==== Allow user create database ==== psql psql (9.4.4) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres vfossdb | vfossdbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows) postgres=# ALTER USER vfossdbuser CREATEDB; ===== Working with Postgre ===== * http://www.tutorialspoint.com/postgresql/postgresql_create_table.htm ==== Drop database ==== DROP DATABASE [ IF EXISTS ] name; ==== Select database ==== Connect to the testdb database: postgres=# \c testdb; You can select your database from command prompt itself at the time when you login to your database. Following is the simple example: psql -h localhost -p 5432 -U postgress testdb Password for user postgress: **** ==== Create Table ==== Basic syntax of CREATE TABLE statement is as follows: CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) ); Used to list down all the tables in an attached database. testdb-# \d Use \d tablename to describe each table as shown below: testdb-# \d company ==== Drop Table ==== DROP TABLE table_name; ==== Headline ==== ==== Configure Django Settings ==== DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'dbname', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': '', 'PORT': '', } } ===== Install additional packages ===== ====== Start A Django Project ====== Setup project django-admin.py startproject myproject ====== Database Migrations ====== ===== Setup Database ===== ==== Fedora 21 ==== yum install mariadb-server.x86_64 mariadb-devel.x86_64 systemctl start mysqld.service systemctl enable mysqld.service ==== Create DB, Users ==== sudo su mysql CREATE DATABASE djangodb; GRANT ALL ON djangodb.* TO djangouser@localhost IDENTIFIED BY 'djangouser'; exit mysql -u djangouser -p djangodb See more: https://www.linux.com/learn/tutorials/793758-mariadb-practical-how-to-for-linux-admins === DB init script === cat scripts/mysql_init.sh #!/bin/bash EXPECTED_ARGS=3 E_BADARGS=65 MYSQL=`which mysql` Q1="CREATE DATABASE IF NOT EXISTS $1;" Q2="GRANT ALL ON *.* TO '$2'@'localhost' IDENTIFIED BY '$3';" Q3="FLUSH PRIVILEGES;" SQL="${Q1}${Q2}${Q3}" if [ $# -ne $EXPECTED_ARGS ] then echo "Usage: $0 dbname dbuser dbpass" echo "Default args used: $0 inhanoidb inhanoidbuser inhanoidbuser" Q1="CREATE DATABASE IF NOT EXISTS inhanoidb;" Q2="GRANT ALL ON *.* TO 'inhanoidbuser'@'localhost' IDENTIFIED BY 'inhanoidbuser';" Q3="FLUSH PRIVILEGES;" SQL="${Q1}${Q2}${Q3}" #exit $E_BADARGS fi #$MYSQL -uroot -p -e "$SQL" $MYSQL -uroot -e "$SQL" ==== Database Engine ==== vim settings.py DATABASES = { 'default': { #'ENGINE': 'django.db.backends.sqlite3', #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydatabase', 'USER': 'mydatabaseuser', 'PASSWORD': 'mypassword', 'HOST': '127.0.0.1', #'PORT': '5432', } } ==== Djagno DB ==== South is built-in to Django > 1.7. This is optional if 1.7 is used. pip install south Add south to settings.INSTALLED_APP python manage.py syncdb # admin refmanadmin For Django > 1.7 simply do this AFTER creating models python manage.py makemigrations python manage.py migrate ====== Generate requirements.txt ====== mkdir requirement pip freeze > requirement/requirements.txt ====== Creating App ====== python manage.py startapp myapp Add it to settings.INSTALLED_APP ===== Create Model ===== Refer to next tutorial for working with models. ===== Track Changes to Model ===== Create migration for current model. Changes to the model will be saved in a file, e.g., myapp/migrations/0001_initial.py. python manage.py makemigrations myapp To see what will happen with the database if the migration is applied, use the sqlmigrate command takes migration names and returns their SQL: python manage.py sqlmigrate myapp 0001 run migrate again to create those model tables in your database: python manage.py migrate ===== Depricated ===== Tell south to track myapp: python manage.py schemamigration myapp --initial With Django > 1.7 just migrate is enough. python manage.py migrate This creates tables in the database for all apps in INSTALLED_APPS. Migrate DB changes: $ python manage.py migrate myapp Mark current migration point: python manage.py makemigrations ===== Django-cms ===== http://localhost/wiki/doku.php?id=programming:django:django_cms:django_cms_installation_guide ==== Useful Commands ==== Check setup ./manage.py cms check Remove app ./manage.py migrate app_name zero Check pip pip check