Advertisements
Skip to content

Posts from the ‘DJANGO’ Category

22
Feb

Pip and virtualenv on Windows

Install pip

Pip (Python Package Installer), official documentation for pip.

Usually Python3 comes with pip preinstalled. If you get an error “pip command not found”, use the following command to install pip:

Download get-pip.py, make sure you’re saving file to Desktop

In your Command Prompt navigate to Desktop

cd Desktop

Execute get-pip.py

python get-pip.py

Now pip should work system wide.

virtualenv

In your Command Prompt enter:

pip install virtualenv

Launch virtualenv

In your Command Prompt navigate to your project:

cd your_project

Within your project:

virtualenv env

Activate your virtualenv:

on Windows, virtualenv creates a batch file

\env\Scripts\activate.bat

to activate virtualenv on Windows, activate script is in the Scripts folder :

\path\to\env\Scripts\activate

Example:

C:\Users\'Username'\venv\Scripts\activate.bat

Another way to install pip

Save the “ez_setup.py” file to your desktop form https://bootstrap.pypa.io/ez_setup.py

In your Command Prompt navigate to Desktop:

cd Desktop

Execute ez_setup.py:

python ez_setup.py

install pip:

easy_install pip

Advertisements
20
Feb

DJANGO Cheat Sheet

Set up the virtual environment and create a new project in DJANGO.

linux:
virtualenv --python=python3.6 venv
windows:
virtualenv venv

linux:
source venv/bin/activate

windows:
\path\to\env\Scripts\activate

pip3 install django
pip3 install pillow
django-admin startproject [project name]

Verify project creation and start DJANGO server.

# Navigate to the newly created project folder.
. python3 manage.py runserverall

# Create a superuser
python3 manage.py createsuperuser [username]

# Test Server
python3 manage.py runserver

Creating a Django App

1. Open terminal and navigate to django project folder (where manage.py exists)
2. django-admin startapp [app name] # use pluralised names, e.g. posts
3. Navigate to new app folder.
4. Create new subfolder called “templates”
5. Navigate to new templates folder.
6. Create new subfolder called same name as your app (e.g. posts)
7. Go back to project folder
8. Open project urls.py and add “from [appname] import views”. Then add url path for new view.
e.g. url(r’^$’, views.home, name=‘home’)
9. Open views.py and add:
def home(request):
return render(request, ‘posts/home.html’)
10. Navigate to projectfolder\appfolder\templates\appfolder
11. Create new file ‘home.html’
12. Go to project folder and open settings.py. Scroll down to INSTALLED_APPs and add
‘appname’, to the end of the list of apps.

Connecting PostgreSQL to a Django Project

1. Create a new database in PostgreSQL, and assign a DB user with admin privileges
2. Go to project settings.py and scroll down to DATABASES section. Replace with:

DATABASES = {
 'default': {
 'ENGINE': 'django.db.backends.postgresql',
 'NAME': ‘[databasename]’,
 'USER': ‘[username]’,
 'PASSWORD': ‘[password]’,
 'HOST': '127.0.0.1',
 'PORT': '5432',
 }
}

3. Scroll down to TIMEZONE and replace code. e.g. TIME_ZONE = ‘Australia/Perth’