Django: rest_framework

ModelSerializer

data

retrieve lookup

Override APIView

permission

HyperlinkedIdentityField

MethodField

    發表留言

    Django Staticfiles

    urls.py

    settings.py

    collectstatics: add internal statics to external(dev) statics

    Direcotry

     

    發表留言

    Django User Model

    Extent User Model

    User login

    Third Part Packages

    • Django Registration Redux
    • Django All Auth
    • Python Social Auth

     

    發表留言

    Django Forms

    Cross Site Request Forgery

    Html form vs Django form

    Validation

    Initial data

    ModelForm

    Setting fields in a model form

    Render field manually

    Include Snippet

    Formset

    Model Formset

    Management

    Query

     

    發表留言

    Linux Root Directory

    bin: binary, where programs live

    etc: where configuration files for different system programs and utilities is

    boot: where boot files is (the operating system itself)

    cdrom: CD mount point

    dev: pseudo entry that operating system puts there to interact with devices

    lib: librarys for programs

    media: media mount point

    mnt: removeable media mount point or others

    opt: options software

    proc: pseudo entry that store process related data

    root: roots home directory

    run: where temp data stored

    sbin: system binary for system programs

    sys: pseudo entry that store system data

    temp: where temp data stored

    usr: where user related data stored, user accessible binary

    var: any variable data (e.g. log)

    發表留言

    Django Class Base View

    Template View

    doc: base.py

    • View: Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking.
    • TemplateView: Render a template. Pass keyword arguments from the URL conf to the context
    • TemplateResponseMixin: A mixin that can be used to render a template
    • ContextMixin: A default context mixin that passes the keyword arguments received by get_context_data() as the template context

    Custom Mixin

    Generic display views: Detail View

    Reverse

    Generic display views: Create View

    Handle MultipleObjectReturned

    SuccessMessageMixin

    FormMixin

     

    發表留言

    Linux Commands

    Open directory

    • nautilus /path/to/folder

    Delete

    • sudo rm /path/to/file
    • sudo rm -r /path/to/folder

    Download

    • sudo wget http://url

    Edit file

    • gedit file
    • vim file
    • nano file
    • vi file
    • echo some content > file

    Create .desktop

    • sudo gnome-desktop-item-edit –create-new /path/to/file.desktop

    Activate venv

    • source venv/bin/activate

    Root

    • sudo su –
    • ls /

    PTT

    • luit -encoding big5 telnet ptt.cc

    Shortcut

    • alias rm=’rm -i’

    Permission

    • chmod u+r file #user add read permission
    • chmod 764 file #user: rwx; group:rw; world: r

    Print work directory

    • pwd

    PATH

    • echo $PATH
    • export PATH=$PATH:path/to/add

    FInd Program

    • which python
    • sudo find / -name “python” -print

    Redirect

    • ls > ls.txt (rewrite)
    • ls >> ls.txt (append)
    • find / -name passwd -print 2>error.txt (redirect standard error to text file)

    Grep

    • grep -r <–filename–> (The specific meaning of “recursive” in this context is “operating on a directory and its contents, including the contents of any subdirectories“.)
    • e.g. ls | grep “query” (pipe: take the previous command output and send to input of next command)
    • e.g. apt-cache search apache | grep python
    • e.g. ps aux | grep bash

    Create Link

    • ln -s /usr/share/backgrounds mywallpapers (symbolic)

    Compress

    • zip file.zip file1 file2 file3
    • unzip file.zip
    • tar cfz file.tar.gz file1 file2 file3 (create, filename, zip)
    • tar xfz file.tar.gz (extract, filename, zip)

    Packages Management source: /etc/apt/source.list

    • sudo apt-get update
    • sudo apt-get upgrade
    • sudo apt-get install software
    • sudo apt-get remove software
    • sudo apt-get autoremove (remove all unused/relevent software)
    • apt-cache search python

    Log Management system log configs:

    • /etc/rsyslog.conf
    • /etc/rsyslog.d

    log paths:

    • /var/log

    Service Management service scripts path: /etc/init.d

    • sudo /etc/init.d/networking status
    • sudo service –status-all (+:running, -:stop, ?:disable/no status)
    • sudo service <–service name–> start

    Remove program

    • sudo apt-get purge <-app->
    • sudo apt-get purge autoremove <-app->
    • sudo apt-get purge –auto-remove <-app->

    Manual

    • man

    Background Services / Processes

    • top (running process)
    • ps aux
    • ps aux | grep firefox

    Kill Process

    • kill <-process id->
    • killall firefox

    Build program

    • run configure to create Makefile => ./configure
    • run Makefile to compile => make
    • make install

    Edit string

    • echo this is true | sed ‘s/true/false/’ => this is false
    發表留言

    Python Custom Exception

     

    發表留言

    “with” in Python

    發表留言