Tag: django

  • Django – OAuth Test Sample

    An OAuth sample code base on Django.

    Django Commands

    • pip3 install django djangorestframework django-oauth-toolkit
    • django-admin startproject oauth_test
    • cd oauth_test
    • python3 manage.py startapp test_app
    • python3 manage.py makemigrations
    • python3 manage.py migrate
    • python3 manage.py createsuperuser
    • python3 manage.py runserver
    • We can test by using Postman application.

    Setup OAuth in project’s admin management site

    • Default site url: http://127.0.0.1:8000/admin
    • Attention! The Client secret need save copy on create step! After creating, the Client secret field only shows the hashed client secret not original. (We need use the original client secret for auth)

    Project Structure

    Project Structure

    .\oauth_test\settings.py

    .\oauth_test\urls.py

    .\test_app\apps.py

    .\test_app\models.py

    .\test_app\serializers.py

    .\test_app\views.py

    .\test_app\urls.py

    Postman Test for get auth – Post

    Use the original Client secret.

    Postman Test for use auth – Get

  • CentOS 7-Install Python 3, Django 2, uwsgi, nginx

    以root身份依序執行以下指令:

    yum -y install epel-release
    yum -y install python36 python36-tools python36-libs python36-devel wget gcc gcc-c++ zlib* openssl-devel unixODBC-devel
    yum -y install uwsgi
    
    wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
    python36 get-pip.py
    pip install django
    pip install django-pyodbc-azure
    
    firewall-cmd --zone=public --add-service=http --permanent
    systemctl enable nginx
  • Python-Django connect to MS SQL Server

    Use django-pyodbc-azure:

    pip install django-pyodbc
    pip install django-pyodbc-azure

    Microsoft recommend solution for Python connect to SQL Server: pyodbc

    Settings.py:

    DATABASES = 
        'default': {
            'NAME': 'guest',
            'ENGINE': 'sql_server.pyodbc',
            'HOST': '127.0.0.1',
            'USER': 'sa',
            'PASSWORD': 'brassaikao.idv.tw',
            'PORT': '1433',
            'OPTIONS': {
                'driver': 'ODBC Driver 17 for SQL Server',
            },
        }
    }