Tag: Django

  • Django – some frequently used commands

    python-django

    While working with Django you use a lot of commands. I decided to create a list of commands for myself to make my development a little faster.

    Quick links to tutorials:

    My project folder is this: ~/Documents/common-app-server/app_server

    My project name is: tokens

    Creating virtual environment:

    $ virtualenv env
    

    Activating virtual environment:

    $ source env/bin/activate
    

    Installing Django on virtual environment:

    $ pip install Django
    

    Django version:

    $ python -c "import django; print(django.get_version())"
    

    Starting a project:

    $ django-admin.py startproject tutorial
    

    Creating Django application:

    (env)~/Documents/common-app-server/app_server: $ ./manage.py startapp multiverse
    

    Syncing database for the first time:

    python manage.py syncdb
    

    Creating migrations:

    $ ./manage.py makemigrations tokens
    

    Looking at migrations:

    $ ./manage.py sqlmigrate tokens 0001
    

    Starting migrations:

    $ ./manage.py migrate
    

    Entering sqlite mode:

    (env)~/Documents/common-app-server/app_server: $ ./manage.py dbshell
    

    Recreating database:
    1. Delete migrations folder
    2. Use this command to clear database:

    $ ./manage.py sqlclear tokens| ./manage.py dbshell
    

    3. Create and run migrations

    Running a local server:

    $ ./manage.py runserver
    

    Creating admin user:

    $ ./manage.py createsuperuser
    

    App view:

    http://localhost:8000/tokens/
    

    Admin view:

    http://localhost:8000/admin/
    

    Installing DjangoRestFramework:

    $pip install djangorestframework
    
  • Running Django project on Apache production server with mod_wsgi

    django

    This is official tutorial. But it didn’t work for me for 100 %. But there is some useful information on wsgi and daemon process that should be created.

    There is a good tutorial, that I followed and it actually works. But I decided to write my own post with instructions for my personal project:

    First we enter the production server via SSL and create a virtual environment there:

    virtualenv env
    

    Then we activate it:

    source env/bin/activate
    

    Check the Django version. It must be the same as in your local server:

    python -c "import django; print(django.get_version())"
    

    Save somewhere a path to a Django project:

    /root/www/den.ladby.ru/public/common-app-server/app_server
    

    Your path will be different.

    This is an example of a sites-available/*.conf file setting to run Django with WSGI:
    Снимок экрана 2014-09-19 в 20.25.49

    Here app_server is the name of my Django project or you can name it a site. But in my case it is not site but server for a mobile application.

    Python and Django version on server and virtual environment may be different. You should do everything inside virtual environments with the same versions on local and production servers.

  • Installing Django on Mac

    django

    First you should install Python and Pip:

    $ brew install python
    

    Then you install virtual environments:

    $ sudo pip install virtualenv
    

    And activate one virtual environment:

    $ virtualenv my_virtualenv
    
    $ source my_virtualenv/bin/activate
    

    Then you install Django using pip in that virtual environment. Don’t use sudo, since if you use it, then you will install Django not in virtual environment but in common environment.

    $ pip install Django
    

    Now you can start creating Django projects using this tutorial.

    To look at a version of installed Django use this:

    $ python -c "import django; print(django.get_version())"
    

    To create a project:

    $ django-admin.py startproject mysite
    

    To activate database:

    $ python manage.py migrate
    

    To run a server:

    $ python manage.py runserver
    

    To open your first project on Django:

    http://localhost:8000/