This is sourced from https://scotch.io/tutorials/build-your-first-python-and-django-application

Running the virtualenv env command puts an env folder in our firstPythonJango folder.

➜ firstPythonJango virtualenv env
Using base prefix ‘/Library/Frameworks/Python.framework/Versions/3.4’
New python executable in /Users/alinafe/PycharmProjects/firstPythonJango/env/bin/python3.4
Also creating executable in /Users/alinafe/PycharmProjects/firstPythonJango/env/bin/python
Installing setuptools, pip, wheel…done.
➜ firstPythonJango ls
dictionary.py env

activate the environment

➜ firstPythonJango source env/bin/activate
(env) ➜ firstPythonJango pwd

The prompt ‘(env)’ shows our environment is active
/Users/alinafe/PycharmProjects/firstPythonJango
(env) ➜ firstPythonJango

Install Django

(env) ➜ firstPythonJango pip install django
Collecting django
Downloading Django-1.10.5-py2.py3-none-any.whl (6.8MB)
100% |████████████████████████████████| 6.8MB 224kB/s
Installing collected packages: django
Successfully installed django-1.10.5

Create an app

django-admin startproject helloapp

this will create the following directory structure:

env) ➜ firstPythonJango tree helloapp
helloapp
├── helloapp
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

There is a file called manage.py and another folder called helloapp. The helloapp
subdirectory is your main project folder and contains the project’s settings in a
file called settings.py and the routes in your project in the file called urls.py.

Change app settings

vi helloapp/helloapp/settings.py

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
]
Apps we create or third-party apps are added to Installed Apps list after
the default apps installed.

add your own timezone:
TIME_ZONE = ‘UTC’
TIME_ZONE = ‘America/Los_Angeles’

Create your own app

The app gets data from a model, the view does something to the data and then renders a template containing the processed information.
Django templates correspond to views in traditional MVC and Django views
can be likened to the controllers found in traditional MVC.

to create an app in the main the folder:

python manage.py startapp howdy

env) ➜ helloapp tree howdy
howdy
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py

edit the settings.py with the new app.

env) ➜ helloapp cd –
~/PycharmProjects/firstPythonJango/helloapp/helloapp
(env) ➜ helloapp vi settings.py

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘howdy’ <<—-
]

Run the app

env) ➜ helloapp python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run ‘python manage.py migrate’ to apply them.

February 17, 2017 – 00:15:26
Django version 1.10.5, using settings ‘helloapp.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

screen-shot-2017-02-17-at-12-16-15-am

run migrations to remove warnings
(env) ➜ helloapp python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying sessions.0001_initial… OK
(env) ➜ helloapp python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).
February 17, 2017 – 00:25:10
Django version 1.10.5, using settings ‘helloapp.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

URLS and templates

Add the URLS to urls.py folder

urlpatterns = [
url(r’^admin/’, admin.site.urls),
url(r’^’, include(‘howdy.urls’)), <<—
]

add the include to your import statement in the file:
from django.conf.urls import url, include

In the howdy app folder create a file called urls.py.

(env) ➜ helloapp cd howdy
(env) ➜ howdy ls
__init__.py __pycache__ admin.py apps.py migrations models.py tests.py views.py
(env) ➜ howdy touch urls.py

The code above calls for the defined HomePageView. Modify the views.py file and add the following lines to the blank template:

env) ➜ howdy vi views.py

from django.shortcuts import render
from django.views.generic import TemplateView <<–

# Create your views here.
class HomePageView(TemplateView): <<–
def get(self, request, **kwwargs): <<–
return render(request, ‘index.html’, context=None) <<–

 

we need to create a template folder to render our index.html file.in the templates folder touch index.html file
env) ➜ templates pwd
firstPythonJango/helloapp/howdy/templates
(env) ➜ templates touch index.html

add the following to your html file:

<!– howdy/templates/index.html –>
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>Hooddy Hoo!!</title>
<head>
<body>
<h1>Hoody! First Django App!</h1>
</body>
</html>

 

run your app
env) ➜ helloapp python manage.py runserver
Performing system checks…

System check identified no issues (0 silenced).
February 17, 2017 – 00:51:52
Django version 1.10.5, using settings ‘helloapp.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[17/Feb/2017 00:52:03] “GET / HTTP/1.1” 200 197

screen-shot-2017-02-17-at-12-53-31-am

 

 

 

Advertisements