Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Friday, November 28, 2014

Dockerfile for the Django application, MySQL as database


Below steps to prepare docker image with the Django with helloworld application installed/setup, MySQL as a database


1. Save the Below dockerfile contents to the directory.

2. Build the image as sudo docker build -t django_hw_img .
(there is a . at the end).

On successful image creation, docker image will created. it will be listed in the list from the command sudo docker images

3. Run the image as 
sudo docker run -i -t -p 80:80 django_hw_img:latest /bin/bash

4. On container shell start the apache2 service service apache2 start

5.Access the application from the http://<your_host_ip_address>/helloworld/

+===================Dockerfile======================+
#################################################
# Dockerfile to build Python-Django WSGI Application Containers
# Based on Ubuntu:latest
#################################################

# Set the base image to Ubuntu
FROM ubuntu

# File Author / Maintainer
MAINTAINER surya.janardhan@gmail.com

# Update the sources list
RUN apt-get update

# Install basic applications
RUN apt-get install -y git build-essential libapache2-mod-wsgi apache2

# Install Python and Basic Python Tools
RUN apt-get install -y python python-pip

# Get pip to download and install requirements:
RUN pip install django

#install MySQL in noninteractive way
RUN export DEBIAN_FRONTEND=noninteractive
RUN apt-get -q -y install mysql-server python-mysqldb

#create the working dir and set the working directory
CMD mkdir /usr/local/share/apps
WORKDIR /usr/local/share/apps

#clone the tutorials from the github.com
RUN git clone https://github.com/Jadatravu/Tutorials /usr/local/share/apps/tutorials

# set the permissions to the app directory
CMD cd /usr/local/share/apps && chown -R www-data:www-data tutorials
WORKDIR  /usr/local/share/apps/tutorials/django_tutorials/helloworld/

# set the database for the helloworld database django_tut
RUN echo "CREATE DATABASE django_tut;" > /usr/local/share/apps/create_database.txt && service mysql start  && mysql -u root < /usr/local/share/apps/create_database.txt &&  python manage.py migrate

# Configure apache configuration for the helloworld application
RUN  echo "WSGIScriptAlias / /usr/local/share/apps/tutorials/django_tutorials/helloworld/helloworld/wsgi.py" >> /etc/apache2/apache2.conf &&  echo "WSGIPythonPath /usr/local/share/apps/tutorials/django_tutorials/helloworld/" >> /etc/apache2/apache2.conf &&  echo "<Directory /usr/local/share/apps/tutorials/django_tutorials/helloworld/>" >> /etc/apache2/apache2.conf &&  echo "<Files wsgi.py>" >> /etc/apache2/apache2.conf &&  echo "Order deny,allow" >> /etc/apache2/apache2.conf &&  echo "Require all granted" >> /etc/apache2/apache2.conf &&  echo "Satisfy Any" >> /etc/apache2/apache2.conf &&  echo "</Files>" >> /etc/apache2/apache2.conf &&  echo "</Directory>" >> /etc/apache2/apache2.conf

#expose the port
EXPOSE 80
+===================Dockerfile======================+

Wednesday, January 29, 2014

Django Static files tutorial
1. Create a Django project and app, Edit settings.py for database settings
2. Place the static files "files1.txt", "files2.txt" in the directory for example "/home/notroot/django_tut/staticfiles/sfiles"
3. create the directory files in the path for example "/home/notroot/django_tut/staticfiles/"
4. edit section  STATICFILES_DIRS in settings.py file with the value
"/home/notroot/django_tut/staticfiles/sfiles"
5. edit section in STATIC_ROOT in settings.py file with the value "/home/notroot/django_tut/staticfiles/files"
6. set the value for the variable in settings.py STATIC_URL = '/static/'
7. write a template in the template dir for example index.html
{% load staticfiles %}
  <a href="{% static 'files1.txt' %}">link1</a>
  <a href="{% static 'files2.txt' %}">link2</a>
8. update the TEMPLATE_DIRS option in the settings.py.
9. Add a view in the views.py and update urls.py file
10. In the project root folder run the command "python manage.py collectstatic"
out put would be

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

0 static files copied, 73 unmodified.
11. run the server, access the url from the browser

http://127.0.0.1:8000/staticfiles/
  
12 Download the tutorial from the github
Note: This is compatible with django 1.5.4

Sunday, December 15, 2013

Django Logging Tutorial
1.Update LOGGING section in settings.py with logger,handler,formatter fields
  Logger which specifies the handler, in this case it is file
 'loggers': {
        'logapp':{
            'handlers': ['file'],
            'level': 'DEBUG',
        },
    }
  File handler specifies the filename "logapp.log", also specifies theformatter in this case it is simple.
  'handlers': {
        'file':{
            'level':'DEBUG',
            'class':'logging.FileHandler',
            'filename':'logapp.log',
            'formatter':'simple'
        },
    }
  Formatter specifies the formatter in this case it is 'simple'.
 'formatters':{
        'simple':{
           'format':'%(levelname)s %(message)s'
        }
    },
2. import logging modules and add the logger message lines whereever it is required to have log message.

    logger.debug("This is debug message")
    logger.error("This is error message")

3. log messages will be printed in the log file in this case "logapp.log"
   http://127.0.0.1:8000/logproject/

4.Source Code can be downloaded from the github
https://github.com/Jadatravu/Tutorials/tree/master/django_tutorials/logproject

Note: This tutorial is compatible with django 1.5.4, python 2.7.4

Tuesday, December 3, 2013

Django Helloworld tutorial
1. Install django from pip or apt-get/yum 2. Install mysql, python-mysqldb pacakge from apt-get/yum 3. Create database in mysql using steps mentioned in the url creating-database 4. Creating mysql user and granting database permissions to the created user using the steps mentioned in the url adding-users 5. Edit helloworld/settings.py for database settings [ Database(mysql), Database name, Database user, Database user password] 6. Run server in the helloworld directory python manage.py runserver
7. Open the url http:/127.0.0.1:8000/helloworld


8.Source Code can be downloaded from the github
https://github.com/Jadatravu/Tutorials/tree/master/django_tutorials/helloworld
Note: This tutorial is compatible with django 1.5.4, python 2.7.4

Monday, November 4, 2013

Deploying Django application. http://tinyurl.com/odz9hfx

1. Install libapache2-mod-wsgi using apt-get utility.

2. Add below lines to /etc/conf/apache2.conf

WSGIScriptAlias / /home/notroot/django_tut/helloworld/helloworld/wsgi.py
WSGIPythonPath /home/notroot/django_tut/helloworld/
<Directory /home/notroot/django_tut/helloworld/>
<Files wsgi.py>
Order deny,allow
Require all granted
Satisfy Any
</Files>
</Directory>

3. reload apache2 "service apache2 reload"

4. access url/view from the browser example : http://127.0.0.1/helloworld