The goal of efficiency is more slack.

Thursday, June 20, 2019

Regular expression to match URLs

Here is a regular expression to match URLs, from http to the end.

(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?

Source: https://stackoverflow.com/a/9284473/199959

Note: Does not match some IP addresses.

Sunday, June 16, 2019

FeinCMS installation

Following directions at https://feincms-django-cms.readthedocs.io/en/stable/installation.html, I ran into some problems, probably due to a Django version mismatch. I'm using Django 1.11.x. This second tutorial helped fill in some blanks: https://www.digitalocean.com/community/tutorials/how-to-install-and-get-started-with-feincms . Below are my additions to the first documentation link.

Don't add context processors to the default set Django startproject created.

Create a folder for templates. Add it to settings.py:
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'my_project/templates'),
        ],

Create a template base.html with at least the following:
{% block content %} {% for content in feincms_page.content.main %} {{ content.render }} {% endfor %} {% endblock %}

Create a new Django project in VSCode

  • Go to user settings (cmd-,):
"python.venvPath": "~/.virtualenvs",
  • Quit VSCode.
  • Open a Python file in VSCode.
  • Select virtualenv in bottom bar.
  • Debug > Open Configurations to create launch.json. Otherwise, "Add Configurations" may not work.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Server",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver",
                "--noreload",
            ],
            "debugOptions": [
                "RedirectOutput",
                "Django"
            ]
        },
        {
            "name": "Script",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runscript",
                "$(basename -s .py ${file})"
            ],
            "debugOptions": [
                "RedirectOutput",
                "Django"
            ]
        }
    ]
}
  • Click button on bottom right to Install Linting.

Install Django on a development Mac

Notes on installing a Django development environment on a Mac.

Install Python

  • Install Python 3 from www.python.org.
  • Run Install Cerltificates.command.
  • Install pip and virtualenv
pip3 install --upgrade pip
pip3 install virtualenvwrapper
Put virtualenvwrapper in .bash_profile:
# Virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper_lazy.sh

Create a Python virtual environment

mkvirtualenv -p /usr/bin/python2 my_proj
cd ~/Documents/my_proj
setvirtualenvproject
-p is optional

Install Visual Studio Code

  • Install shell command: Cmd-Shift-p > shell command
  • Install Python extension.

Install Postgres


Install Django

pip install Django

Create directory

cd to the parent directory of the directory you wish to place the source files.
Create directory for source files with the Django startapp command.
django-admin startproject my_proj
cd my_proj
setvirtualenvproject

Create app

mkdir my_proj/my_form
./manage.py startapp my_form my_proj/my_form

Configure for VSCode

code . 
Create a new Django project in VSCode

Initialize git

git init
git add .
If .pyc files were uninentionally added:
git rm --cached '**.pyc'

Create .gitignore

touch .gitignore
code .gitignore
Input:
*.pyc

Static files

STATIC_URL: URL (relative to domain root) to trigger searching for static files.
STATICFILES_DIRS: List of directories in which to look for static files when collectstatic is run.
STATIC_ROOT: Django serves files from this directory when run under development mode. Also, collectstatic collects static files here. Upload this to server.

In development: project/static, sibling to project/manage.py
On production: /home/user/domain.com/public/static

settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'public/static/')   

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Powered by Blogger.
Scroll To Top