Django Workflow

administration:

Create the administrative user to enable logins to the /admin section of the site:

(env)$  python manage.py createsuperuser

View & URL

  1. django-admin.py startapp <appname>
  2. add the app to the projects settings.py file section:
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        '<appname>',
    )
    
  3. Create a view to render: edit the <appname> apps views.py file:
    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("It works!")
  4. Map the URL for the view: create the <appname> urls.py
    from django.conf.urls import patterns, url
    from <appname> import views
    
    urlpatterns = patterns('',
            url(r'^$', views.index, name='index'))
  5. Update the project’s urls.py  file to look for the <appname>’s url in the request
    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'tango_with_django_project_17.views.home', name='home'),
        # url(r'^blog/', include('blog.urls')),
    
        url(r'^admin/', include(admin.site.urls)),
        url(r'^<appname>/', include('<appname>.urls')), 
    )
  6. Now you can visit <IP>:8080/<appname> to see your view working (do restart the server first via:
    python manage.py runserver <IP>:8080 )

Templates

Create the directory structure for the app:

  1. create a templates directory in the projects root
  2. inside of the new templates directory create a directory matching the name of your app <appname>
  3. inside of the /projectdir/templates/<appname>/ directory create an index.html file

Update the project settings.py:

  1. edit the projects settings.py file and add the following where <projectname> is the name of the folder that houses the entire project and contains the manage.py file:
    TEMPLATE_PATH = os.path.join(BASE_DIR, '<projectname>/templates')
  2. then to the same file add:
    EMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        TEMPLATE_PATH,
    )
  3. create a template view in the /projectdir/templates/<appname>/index.html file  eg:
    <!DOCTYPE html>
    <html>
    
    <head>
         <title>Print Consumable Tracking</title>
    </head>
    
    <body>
         <h1>Tracking Printer Consumable Items</h1>
         <strong>{{ boldmessage }}</strong><br />
         <a href="/rango/about/">About</a><br />
    </body>
    
    </html>
  4. in the application’s views.py file add:
    from django.shortcuts import render

    and update the index() to:

    def index(request):
    
        # Construct a dictionary to pass to the template engine as its context.
        # Note the key boldmessage is the same as {{ boldmessage }} in the template!
        context_dict = {'boldmessage': "I am bold font from the context"}
    
        # Return a rendered response to send to the client.
        # We make use of the shortcut function to make our lives easier.
        # Note that the first parameter is the template we wish to use.
    
        return render(request, '<appname>/index.html', context_dict)

Initial Django Setup for itstock project

The following steps assume you’ve completed part 1 :http://www.davegernon.co.uk/techblog/django-setup-with-virtualenv-pycharm/

 

  1. Re-activate your virtual environment (where /code/env is the location you created the virtualenv)
    $ source ./code/env/bin/activate
  2. cd into the parent directory of the virtualenv (/code/) in this example:
    (env)$ cd ./code
  3. Create the django project named itstock:
    (env)~/code/itstock$ django-admin startproject itstock
  4. Now setup Git on the ./code/itstock directory by:
    (env)~/code/itstock$ git init ./
    (env)~/code/itstock$ git add ./
    (env)~/code/itstock$ git commit
  5. Next setup connection to the Bitbucket remote repository via:
     cd to the core directory of the project:

    (env)~/code/itstock$ cd ./itstock
  6. (env)~/code/itstock/itstock$ git remote add origin git@bitbucket.org:<username>/<name of repo>
    (env)~/code/itstock/itstock$ git push -u origin --all
    (env)~/code/itstock/itstock$ git push -u origin
  7. Integrate Bitbucket Git repo to Pycharm: 

    Launch pycharm and select the core directory [~/code/itstock/itstock in my case] as the location for the projectDownload the Bitbucket plugin from  https://plugins.jetbrains.com/plugin/?pycharm&id=6207Within Pycharm click : File > Settings > Plugins > Install plugin from disk…

    Then select the downloaded zip file and install.

    I got a bit lost then, you need to add the Bitbucket repo in when you want to push, maybe start with a PULL request to define that and then work from there.

  8. Ensure the db is migrated:
    Back in the terminal:

    (env)~/code/itstock$ python manage.py runserver <IP>:8080

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

django setup with virtualenv & PyCharm

Setting up my django development (basic).

The following steps assuming that Python is installed.

  1. Setup a repository on Bitbucket:
    Ensure my development machine has access via SSH keys
  2. Install pip on development machine:
    https://pip.pypa.io/en/stable/installing.html:
    To install pip, securely download get-pip.py
    Then run the following (which may require administrator access):

    $[sudo] python get-pip.py
  3. Install virtualenv on the development machine:
    https://virtualenv.pypa.io/en/latest/installation.html

    $ [sudo] pip install virtualenv
  4. Create a working directory for the project:
    $mkdir /path/to/project/root
  5. Create the virtual environment to work from:
    $virtualenv /path/to/project/root/env
  6. Activate the virtual environment:
    $source /path/to/project/root/env/bin/activate
  7. Install mysql-python:
    (env)$[sudo]apt-get install python-dev libmysqlclient-dev
    
    (env)$pip install MySQL-python
  8. Install django 1.7:
    (env)$pip install django==1.7

    exit the virtualenv for now by:

    (env)$ deactivate
  9. Install pycharm to manage the project from a GUI:
    First install java:

    $ sudo add-apt-repository ppa:webupd8team/java
    $ sudo apt-get update
    $ sudo apt-get install oracle-java8-installer
    $ sudo apt-get install oracle-java8-set-default

    Next install pycharm:

    Download from https://www.jetbrains.com/pycharm/download/
    or from a directory outside of your project directory:

    $ wget http://download.jetbrains.com/python/pycharm-community-4.5.3.tar.gz

    change to that directory and :

    $ tar -xvf pycharm-community-4.5.3.tar.gz

    now to launch pycharm:

    $ ./pycharm-community-4.5.3/bin/pycharm.sh

 

Django 1.7, mysql-python and the pycharm editor should now be ready to use!

 

Next: Setting up the itstock django project http://www.davegernon.co.uk/techblog/initial-django-setup-for-itstock-project/

 

 

 

Setup SSH access on bitbucket for a machine

https://confluence.atlassian.com/bitbucket/set-up-git-and-mercurial-ubuntu-linux-269982882.html

 

Step 1. Install Git

Ubuntu uses the apt package management system, which provides the command line utility apt-get and optional graphical interfaces, such as Synaptic and Aptitude. We’ll use apt-get to install packages, but if you’re more comfortable with GUIs, those options are available. Open a terminal window on your local system and do the following:

  1. Enter the following command to install Git:
    $ sudo apt-get install git
  2. Verify the installation was successful by typing which git at the command line.
    $ which git
    /opt/local/bin/git
  3. Configure your username using the following command.
    $ git config --global user.name "Emma Paris"
  4. Configure your email address using the following command.
    $ git config --global user.email "eparis@atlassian.com"

Step 2. Ensure you have an SSH client installed

SSH is most likely included with your version of Mac OS or Linux. To make sure, do the following to verify your installation:

  1. From your terminal window, enter the following command, which identifies the version of SSH you have installed.
    If SSH is installed, you see something similar to the following:

    $ ssh -v
    OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011
    usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
    [-D [bind_address:]port] [-e escape_char] [-F configfile]
    [-I pkcs11] [-i identity_file]
    [-L [bind_address:]port:host:hostport]
    [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
    [-R [bind_address:]port:host:hostport] [-S ctl_path]
    [-W host:port] [-w local_tun[:remote_tun]]
    [user@]hostname [command]>

    If you have  ssh  installed, the terminal returns version information.
    If you don’t have ssh installed, install it now.

  2. List the contents of your ~/.ssh directory.
    If you don’t have an .ssh directory, don’t worry, you’ll create it the next section. If you have a .ssh directory or you may see something like this:

    $ ls -a ~/.ssh
    known_hosts

    If you have defined a default identity, you’ll see the two id_* files:

    $ ls -a ~/.ssh
    .        ..        id_rsa        id_rsa.pub    known_hosts

    In this case, the default identity used RSA encryption (id_rsa.pub). If you want to use an existing default identity for your Bitbucket account, skip the next section and go to start the ssh-agent and load your keys.

Step 3. Set up your default identity

By default, the system adds keys for all identities to the /Users/<yourname>/.ssh directory on Mac OSX, or /home/<yourname>/.ssh on Linux. This procedure creates a default identity. If you have a default identity and you want to use it for Bitbucket, skip this step and go to start the ssh-agent and load your keys. If you have an existing default identity but you forgot the passphrase, you can also use this procedure to overwrite your default identity and create a fresh one.

Want to Use Multiple Identities?

You can create multiple SSH identities. Doing this is an advanced topic and beyond the scope of this tutorial. For information on how to create multiple identities, see Configure multiple SSH identities for GitBash, Mac OSX, & Linux.

Use the following procedure to create a new default identity.

  1. Open a terminal in your local system.
  2. Enter ssh-keygen at the command line.
    The command prompts you for a file where you want to save the key. If the .ssh directory doesn’t exist, the system creates one for you.

    $ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/emmap1/.ssh/id_rsa):
  3. Press the Enter or Return key to accept the default location.
  4. Enter and re-enter a passphrase when prompted.
    Unless you need a key for a process such as script, you should always provide a passphrase. The command creates your default identity with its public and private keys. The whole interaction will look similar to the following:

    $ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/emmap1/.ssh/id_rsa):
    Created directory '/Users/emmap1/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/emmap1/.ssh/id_rsa.
    Your public key has been saved in /Users/emmap1/.ssh/id_rsa.pub.
    The key fingerprint is:
    4c:80:61:2c:00:3f:9d:dc:08:41:2e:c0:cf:b9:17:69 emmap1@myhost.local
    The key's randomart image is:
    +--[ RSA 2048]----+
    |*o+ooo.          |
    |.+.=o+ .         |
    |. *.* o .        |
    | . = E o         |
    |    o . S        |
    |   . .           |
    |     .           |
    |                 |
    |                 |
    +-----------------+
  5. List the contents of ~/.ssh to view the key files.
    $ ls -a ~/.ssh

Step 4. Start the ssh-agent and load your keys

If you are running OSX 10.6.8 or later you can skip this step.  The OSX 10.6.8 system asks for your connection parameters the first time you try to establish a SSH connection.  Then, it automatically starts the ssh-agent for you. If you don’t have OSX 10.6.8 or are running another Linux operating system, do the following:

  1. Open a terminal window and enter the ps -e | grep [s]sh-agent command to see if the agent is running.
    $ ps -e | grep [s]sh-agent
    9060 ?? 0:00.28 /usr/bin/ssh-agent -l
  2. If the agent isn’t running, start it manually with the following command:
    $ ssh-agent /bin/bash
  3. Load your new identity into the ssh-agent management program using the ssh-add command.
    $ ssh-add ~/.ssh/id_rsa
    Enter passphrase for /Users/emmap1/.ssh/id_rsa:
    Identity added: /Users/emmap1/.ssh/id_rsa (/Users/emmpa1/.ssh/id_rsa)
  4. Use the ssh-add command to list the keys that the agent is managing.
    $ ssh-add -l
    2048 7a:9c:b2:9c:8e:4e:f4:af:de:70:77:b9:52:fd:44:97 /Users/manthony/.ssh/id_rsa (RSA)

Step 5. Install the public key on your Bitbucket account

  1. From Bitbucket, choose avatar > Manage account from the application menu.
    The system displays the Account settings page.
  2. Click SSH keys.
    The SSH Keys page displays. If you have any existing keys, those appear on this page.
  3. Back in your terminal window, copy the contents of your public key file.
    For example, in Linux you can cat the contents.

    $ cat ~/.ssh/id_rsa.pub

    In Mac OSX, the following command copies the output to the clipboard:

    $ pbcopy < ~/.ssh/id_rsa.pub
  4. Back in your browser, enter a Label for your new key, for example, Default public key.
  5. Paste the copied public key into the SSH Key field: