2017年11月30日 星期四

Javascript - 新增element

var tag = document.createElement('div');
var img = document.createElement('img');
img.setAttribute('class','bobo')
tag.appendChild(img);
document.getElementById("mydiv").appendChild(tag);

2017年11月25日 星期六

Django - Display Foreign Key object Fields at Admin Interface

# admin.py

class TaskDetailAdmin(admin.ModelAdmin):
    list_display = ['get_name']

    def get_name(self, obj):
        return obj.user.last_name

admin.site.register(models.Task, TaskAdmin)

2017年11月22日 星期三

Django - 分頁

from django.core.paginator import Paginator




reference:https://docs.djangoproject.com/en/1.11/topics/pagination/

Django - 設定User Model 顯示

#models.py

def get_name(self):
    return '{} {}'.format(self.last_name, self.first_name)

User.add_to_class("__str__", get_name)

2017年11月17日 星期五

Django - Message

此筆記將紀錄如何使用Django Message 功能
參考資料:https://docs.djangoproject.com/en/1.11/ref/contrib/messages/

設定預設Tag
# settigns.py

from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
messages.INFO: '',
50: 'critical',
}


# views.py

from django.contrib import messages
messages.add_message(request, messages.INFO, 'Hello world.')


# template

            {% if messages %}
                {% for message in messages %}
                <p {% if message.tags %} class="bg-{{ message.tags }}"{% endif %}>{{ message }}</p>
                {% endfor %}
            {% endif %}



各種TAG

messages.debug(request, '%s SQL statements were executed.' % count)
messages.info(request, 'Three credits remain in your account.')
messages.success(request, 'Profile details updated.')
messages.warning(request, 'Your account expires in three days.')
messages.error(request, 'Document deleted.')


客制level
剛剛設定預設Tag的 50 = critical

def my_view(request):
    messages.add_message(request, 50, 'A serious error occurred.')

2017年11月13日 星期一

Python - DataTime筆記

from datetime import date

取得日期
t = date.today()

轉換字串
t.strftime('%Y%m%d') # 20171114

轉成TUPLE
tt = t.timetuple()


使用localtime()取得時間跟日期
import time

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 

Python - CSV筆記

參考:https://docs.python.org/3.6/library/csv.html


mode二碼
第一碼:
r -讀取
w - 檔案存在與否,均可寫入
x - 檔案不存在時,寫入
a - 檔案存在時,寫入結尾

第二碼:
t - 文字
b - 二進位



讀取

 import csv
 with open('eggs.csv', newline='') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=',', quotechar=',')
     for row in spamreader:
         print(', '.join(row))


csvinstance轉換成串列

lists = []
for row in csv_instance:
    lists.append(row)


寫入

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['001','002','003'])

如果要使用絕對路徑backslash 改成 common slash
c:\123\  to c:/123/



2017年11月11日 星期六

Django - 語系更改

以輸入中文為例:

# settings.py

import sys  
reload(sys)  
sys.setdefaultencoding('utf-8')

LANGUAGE_CODE = 'zh-hant' # Django 1.9之前zh-TW

TIME_ZONE = 'Asia/Taipei'

2017年11月10日 星期五

Django - 客製Admin



1. 修改Admin Title



新增admin資料夾至templates資料夾並新增base_site.html
取得admin templates,參考以下連結,複製內容貼在剛剛新增的base_site.html
https://github.com/django/django

django/contrib/admin/templates/admin/base_site.html

{% extends "admin/base.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('派工系統後台') }}</a></h1> 
{% endblock %}

{% block nav-global %}{% endblock %}


2.客制Admin ListView
首先新增一個class名字為model+admin

# admin.py

from django.contrib import admin
from . import models

class SchoolAdmin(admin.ModelAdmin):
   

記得將新增的class註冊到admin
admin.site.register(models.School, SchoolAdmin)


如要設定只需要override以下ModelAdmin變數

設定ListView欄位名稱排序
 fields = ['field1','field2','field3'] # 左至右

設定ListView搜尋功能
search_fields = ['field1','field2'] # 如此field1, field2都可以搜尋到

設定ListView Filter
list_filter = ['field1','field2']

設定ListView顯示欄位
list_display = ['field1','field2']

設定ListView欄位編輯
list_editable = ['field1','field2']






Bootstrap - Navbar筆記

Bootstrap js cdn 放在body最底部

Django - Dynamic URL

# views.py

from django.shortcuts import render
from .models import Post


def post_detail(request, pk):
    post = Post.objects.get(pk=pk)
    return render(request, 'post.html', {'post': post})


# urls.py

url(r'^post/(?P<pk>\d+)/$', post_detail, name='post_detail'),
url(r'^send_message/(?P<user_id>[\w]+)/(?P<text>\w+)/$', views.send_message, name="send_message"),


2017年11月9日 星期四

Django - Line Message API 筆記

from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
from django.views.decorators.csrf import csrf_exempt

from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError, LineBotApiError
from linebot.models import MessageEvent, TextSendMessage, TextMessage, FollowEvent

line_bot_api = LineBotApi('Channel access token')
handler = WebhookHandler('Channel secret')

# member_ids_res = line_bot_api.get_group_member_ids(group_id)

@handler.add(MessageEvent, message=TextMessage)
def handle_text_message(event):
    # print('room_id: {}'.format(event.room_id))
    print('text: {}'.format(event.message.text))
    text =event.message.text.lower()
    user = event.source.user_id
    print('user_id: {}'.format(user))
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=user)
    )

@handler.add(FollowEvent)
def handle_follow(event):
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text='感謝這位兄長加我')
    )


@handler.default()
def default(event):
    print(event)
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text='Currently Not Support None Text Message')
    )


@csrf_exempt
def callback(request):
    if request.method == 'POST':
        signature = request.META['HTTP_X_LINE_SIGNATURE']
        body = request.body.decode('utf-8')

        try:
            handler.handle(body, signature)
        except InvalidSignatureError:
            return HttpResponseForbidden()
        except LineBotApiError:
            return HttpResponseBadRequest()
        return HttpResponse()
    else:
        return HttpResponseBadRequest()

def send_message(request):
    line_bot_api.multicast(['to1', 'to2','to3'], TextSendMessage(text='各位都是帥哥!'))
    # line_bot_api.push_message('to1', TextSendMessage(text='Hello World!'))
    return HttpResponse()

Django - CBV筆記

之前這筆記都是使用FBV(Function Based Views),以下與CBV(Class Based Views)做對照:

#FBC
def index(request):
    return render(request, 'index.html')

#CBV
class IndexView(TemplateView):
    template_name = 'index.html'


快速體驗CBV

# views.py
from django.views.generic import View

class CBView(View):
def get(self, request):
return HttpResponse('Class Based View are Cool!')

# urls.py
from basic_app import views

url(r'^$', views.CBView.as_view()),



TemplateView應用

from django.views.generic import TemplateView

# views.py
class IndexView(TemplateView):
    template_name = 'index.html'

    # for injection
    def get_context_data(self,**kwargs): # 雙*代表字典,單*代表Tuple
     context = super(IndexView, self).get_context_data(**kwargs)
     context['injectme'] = 'Basic Injection!'
     return context
    
#urls.py
url(r'^$', views.IndexView.as_view()),





ListView應用

from django.views.generic import ListView

class SchoolListView(ListView):
context_object_name = 'schools' # 沒有使用context_object_name django將預設成 school_list
model = models.School



DetailView應用



from django.views.generic import DetailView

class SchoolDetailView(DetailView):
context_object_name = 'school_detail' # 未設定
model = models.School
template_name = 'basic_app/school_detail.html'


url(r'^(?P<pk>\d+)/$', views.SchoolDetailView.as_view(),name='detail'),



CreateView應用

class SchoolCreateView(CreateView):
fields = ('name', 'principal', 'location')
model = models.School
之後要記得去School Model 加上get_absolute_url設定轉換的頁面
url(r'^create/$', views.SchoolCreateView.as_view(), name='create'),



UpdateView應用

class SchoolUpdateView(UpdateView):
fields = ('name','principal')
model = models.School

url(r'^update/(?P<pk>\d+)/$', views.SchoolUpdateView.as_view(),name='update'),


DeleteView應用









2017年11月7日 星期二

Django - Login筆記

新增 LOGIN_URL變數 於 settings.py
新增login template
設定login views
使用decorator
設定urls.py


1. 設定settings LOGIN_URL變數
LOGIN_URL = '/basicapp/user_login'


2. 新增login template
<h1>Login</h1>

<form method="post" action="{% url 'basicapp:user_login' %}" >
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username" placeholder="Enter username...">
<label for="password">Password:</label>
<input type="password" name="password" placeholder="Enter password...">
<input type="submit" name="" value="Login">
</form>


3. 設定views.py

from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

def user_login(request):

if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')

user = authenticate(username=username, password=password)

if user:
if user.is_active :
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse('Account not active')
else:
print('Someone tried to login and failed')
print('Username: {} and password {}'.format(username,password))
return HttpResponse('invalid login details supply')

return render(request, 'basicapp/login.html')



@login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))

@login_required
def special(request):
return HttpResponse('You are logged in!')

4. 設定urls.py

完成

判斷登入使用者

不可等入admin mode
is_authenticated

可登入admin mode
is_staff
is_superuser

Javascript - JSON筆記

透過XMLHttpRequest API 載入JSON

宣告XMLHttpRequest物件
var request = new XMLHttpRequest();

設定請求
request.open('GET', requestURL);

設定取得的格式
request.responseType = 'json';
request.send();

處理取得的JSON
request.onload = function() {
  var superHeroes = request.response;
  populateHeader(superHeroes);

}

function populateHeader(jsonObj) {
  var myH1 = document.createElement('h1');
  myH1.textContent = jsonObj['squadName'];
  header.appendChild(myH1);

  var myPara = document.createElement('p');
  myPara.textContent = 'Hometown: ' + jsonObj['homeTown'] + ' // Formed: ' + jsonObj['formed'];
  header.appendChild(myPara);
}

參考資訊
https://www.fooish.com/jquery/dom-manipulation.html

HTML5 - BackGround Video

使用mp4為目前最保險的方法

控制播放
<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

自動播放
<video width="320" height="240" autoplay>
  <source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

2017年11月6日 星期一

JQuery - AJAX筆記

本筆記將實作JQuery AJAX + Django 按鈕觸發AJAX


# ajax.html

<!DOCTYPE html>
<html>
<head>
<title>Ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
  $( document ).ready(function() {
$("#btn1").on('click', function () {
$.ajax({
url: 'http://localhost:8000/basic_app/get_json_data/',
type: 'get',
data: {},
dataType: 'json',
timeout: 10000,
success: function(result) {
alert('sucess');
$('#txt1').html(result.result);
},
error: function() {
$('#txt1').html("error");
    }
});
});
});
</script>
</head>
<body>
<h1>Ajax</h1>
<h1 id="txt1" >ready</h1></br>
<button type="button" id="btn1">turn off </button>
</body>
</html>


# views

from django.http import HttpResponse
import json

def get_json_data(request):
    a = {
    'result' : 'success',
    }
    return HttpResponse(json.dumps(a), content_type='application/json')


# urls.py

url(r'^get_json_data/', views.get_json_data,name='get_json_data'),

完成

Cordova筆記

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function onBodyLoad(){
document.addEventListener("deviceready", onDeviveReady, false);
window.location.href = "http://139.162.96.42";

}

function onDeviveReady(){

}
</script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
</head>
<body onload="onBodyLoad()">

</body>

</html>



npm install -g cordova

cordova create MyApp

cd MyApp

cordova platform add browser

cordova run browser

2017年11月5日 星期日

Django - User Model & Form

新增與django內建User model關聯的UserProfileInfo model

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class UserProfileInfo(models.Model):

user = models.OneToOneField(User)

# additional
portfolio_site = models.URLField(blank=True)

profile_pic = models.ImageField(upload_to='profile_pics', blank=True)

def __str__(self):
return self.user.username
使用image前先安裝pillow Image Library

pip install pillow


設定form.py

設定views.py

設定urls.py

makemigrations


2017年11月4日 星期六

Django - Password筆記

此筆記將紀錄使用者認證以及密碼

settting.py

我們將使用以下兩個app才能實作
TEMPLATE_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
]

我將使用bcrypt,使用bcrypt加密密碼是較安全的做法

pip install bcrypt
pip install [argon2] # 如果是1.10以上不需要裝argon2,已經內建於django中


於settings新增

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.Argon2PasswordHasher',
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
    'django.contrib.auth.hashers.BCryptPasswordHasher',
]


設定AUTH_PASSWORD_VALIDATORS(可略過)

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        'min_length' : 9, #設定密碼至少9碼
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]



Django - Filter & 自製Tag 筆記

此筆記將紀錄如何使用Filter

{{ value | filter : "params" }}
並非全部的filter都用參數

每個單字以大寫開頭
{{ text | title }}

日期格式
{{ mydate | data : "Y-m-d"  }}

大寫text
{{ text | upper }}

小寫
{{ text | lower }}

數字加法,number變數必須要是數字型別
{{ number | add:"2" }}

顯示前n個字元
{{ text | truncatewords:80 }} # 前80


自製Filter


新增templatetags資料夾於app底下,注意不是project而是app


接著於templatetags新增__init__.py 和 tags.py,內容如下:

from django import template

register = template.Library()

def cut(value, arg):
return value.replace(arg,'')

register.filter('cut', cut) # 註冊cut function 成 'cut'

自製Filter,使用decorator

tags.py改成

from django import template

register = template.Library()

@register.filter(name='cut')
def cut(value, arg):
return value.replace(arg,'')


上述設定完就可以使用自製的Filter

{{ text | cut:'cool' }}



自製Tag

import datetime
from django import template

register = template.Library()

@register.simple_tag
def current_time(format_string):
    return datetime.datetime.now().strftime(format_string)

參考:https://docs.djangoproject.com/en/1.11/topics/templates/





2017年11月1日 星期三

Django - Template Inheritance筆記

此筆記將紀錄以DRY為原則的Template Inheritance
{% extends "basic_app/base.html" %}
{% block body_block %}
{% include "includes/_messages.html" %}

# base.html

{% extends "basic_app/base.html" %}

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Base</title>
  </head>
  <body>
    <nav class="navbar navbar-default navbar-static-top">
    </nav>
    <div class="container">
        {% block body_block %}
        {# Anything outside of this will be inherited if you use extend.#}
        {% endblock %}
    </div>
  </body>
</html>

index.html繼承base.html的navbar

{% extends "basic_app/base.html" %}
    {% block body_block%}
        <h1>This is the index.html page</h1>
    {% endblock %}

2017年10月31日 星期二

基本功 - 佇列

利用python實作佇列Queue

class Queue:
    def __init__(self):
        self.items = []

    def enqueue(self, item):
        self.items.insert(0,item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

基本功 - 堆疊

利用python實作stack堆疊

class Stack:
     def __init__(self):
         self.items = []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def size(self):
         return len(self.items)

2017年10月30日 星期一

Django - Relative URLs筆記

此筆記將紀錄進階Template技巧

1. Relative URLs


<a href="basicapp/index"> Index </a>
簡化成
<a href="{% url 'basicapp:index' %}">  Index </a>

上例我們設定路由urls.py

# urls.py

from django.conf.urls import url
from basicapp import views

app_name = 'basicapp' # 新增app_name變數
urlpatterns = [
    url(r'^$', views.index, name='index'), # 設定屬性name為index
]

由此可得 {% url 'basicapp:index' %}是如何生成


如果要產生admin頁面相對URLs
{% url 'admin:index' %}


注意:'basicapp:index' 請確認有無空白,不然會噴錯

P.S:此方法為Python2.0之後方法

Django - Model Form筆記

使筆記將紀錄使用Model Form,Model Form直接的與Form做連接,來達成驗證。

操作前務必import以下:
form django import forms
form mapp.models import MyModel

P.S:我們將使用Meta class,實作ModelForm


ModelForm操作案例:

1. show model中全部的欄位,使用__all__

class MyForm(models.ModelForm):
class Meta:
model = MyForm()
fields = "__all__"


2. 排除特定欄位,使用exclude

class MyForm(models.ModelForm):
class Meta:
model = MyForm()
exclude = ["field1", "field2", "field3"]


3. 指定特定欄位
class MyForm(models.ModelForm):
class Meta:
model = MyForm()
fields = ("field1", "field2", "field3")


如想要客制驗證,可以直接寫在Form class裡

class MyForm(models.ModelForm):
        # 加在這 name = froms.CharField(...)
class Meta:
model = MyForm()
fields = ("field1", "field2", "field3")
# 更改Modelform field 屬性
        widgets = {
            'field1': forms.TextInput(attrs={'class': 'textinputclass'}),
            'field2': forms.Textarea(attrs={'class': 'editable medium-editor-textarea postcontent'}),
        }


現在我們將進行ModelForm實作,假設我們已經有一個User Model,現在要製作一個輸入User Model頁面

1. 設定form.py

class MyModelForm(forms.ModelForm):
class Meta():
model = User
fields = '__all__'

2. 設定templates

<form method="POST">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" value="Submit">
</form>

3. 設定views.py

from form import MyModelForm

def model_form_name_view(request):
form = forms.MyModelForm()
if request.method == 'POST':
form = MyModelForm(request.POST)
if form.is_valid():
form.save(commit=True) # exection, same as form.save()
return index(request)
else:
print('ERROR FORM INVALID')

return render(request, 'myapp/model_form_page.html', {'form' : form})

4. 設定urls.py

完成



隱藏內建User model  help_text 訊息

# form.py

from django.contrib.auth.models import User

class UserForm(forms.ModelForm):

password = forms.CharField(widget=forms.PasswordInput())

class Meta():
model = User
help_texts = {
'username': None, # 取消掉
                }
fields = ('username', 'email', 'password')

完成

2017年10月29日 星期日

Django - Form資料驗證筆記

此筆記將紀錄詳細的資料驗證。

以驗證機器人為例:
新增一個隱藏的欄位來檢驗機器人,如屬性value有值代表是機器人,即發出資料驗證訊息。
使用clean_XXX()來驗證單一欄位
from django import forms

class FormName(forms.Form):
name = forms.CharField()
email = forms.EmailField()
text = forms.CharField(widget=forms.Textarea)
botcatcher = forms.CharField(required=False,
widget=forms.HiddenInput)

def clean_botcatcher(self):
botcatcher = self.cleaned_data['botcatcher']
if len(botcatcher) > 0:
raise forms.ValidationError("Gotcha bot!!!")

return botcatcher



另一種方法是使用validators,可以讓程式更簡短

from django import forms
from django.core import validators

class FormName(forms.Form):
name = forms.CharField()
email = forms.EmailField()
text = forms.CharField(widget=forms.Textarea)
botcatcher = forms.CharField(required=False,
widget=forms.HiddenInput,
validators = [validators.MaxLengthValidator(0)])

客製驗證,以檢驗開頭必要為z為例:
利用validators客制自己的資料驗證,新增一個method 來寫入檢驗邏輯,成立即發出error訊息。

from django import forms
from django.core import validators

def check_for_z(value):
if value[0].lower() != 'z':
raise forms.ValidationError('Name needs to start with z')


class FormName(forms.Form):
name = forms.CharField(validators=[check_for_z],)
email = forms.EmailField()
text = forms.CharField(widget=forms.Textarea)



驗證再次輸入的email與email欄位是否相符,使用clean()
使用clean()讀取多個欄位

from django import forms
from django.core import validators

class FormName(forms.Form):
name = forms.CharField(validators=[check_for_z],)
email = forms.EmailField()
verify_email = forms.EmailField(label='Enter your email again')
text = forms.CharField(widget=forms.Textarea)

def clean(self):
email = self.cleaned_data.get('email')
vmail = self.cleaned_data.get('verify_email')
if email != vmail:
raise forms.ValidationError("make sure emails match")


Django - Basic Form筆記

此筆記將實作Basic Form


1.
新增form.py並設定欄位,如下:
from django import forms

class FormName(forms.Form):
name = forms.CharField()
email = forms.EmailField()
text = forms.CharField(widget=forms.Textarea)

2.
新增view
from django.shortcuts import render
from . import form as forms

def for_name_view(request):
form = forms.FormName()
if request.method == 'POST':
form = forms.FormName(request.POST)

if form.is_valid():
print('Validation success!!!')
print("Name: "+form.cleaned_data['name'])
print("Email: "+form.cleaned_data['email'])
print("Text: "+form.cleaned_data['text'])
                        # 此處之後進行query或是一些邏輯

return render(request, 'basicapp/form_page.html',{'form' : form})


3.
新增template
<form method="POST"> # 大小寫沒差
{{ form.as_p }}
{% csrf_token %}
<input type="submit" value="Submit">
</form>

4.
設定路由

完成


Django - Model & Admin Interfacel筆記

行前準備

python mange.py migrate

python mange.py makemigratons app

為了讓admin mode可以看到model,必須要在admin.py中註冊

from app.models import Model1, Model2
admin.site.register(Model1)
admin.site.register(Model2)


創立Model

開啟app中的model.py輸入:

class Topic(models.Model):
top_name = models.CharField(max_length=264, unique=True)

def __str__(self):
return self.top_name

class Webpage(models.Model):
topic = models.ForeignKey(Topic)
name = models.CharField(max_length=264, unique=True, verbose_name='名字')
url = models.URLField(unique=True)

def __str__(self):
return self.name

class AccessRecord(models.Model):
name = models.ForeignKey(Webpage)
date = models.DateField()

def __str__(self):
return str(self.date)


python mange.py migrate

python mange.py makemigratons app # 必須要和app name相同

python mange.py migrate

完成以上指令我們進入shell mode確認一下model是否創立成功

python manage.py shell

from apptwo.models import Topic
print(Topic.objects.all()) # 沒東西
t = Topic(top_name="social network")
t.save()
print(Topic.objects.all()) #出現剛剛新增的social network
quit()

接下來將利用admin interface來新增資料,在這之前註冊


# admin.py

from apptwo.models import AccessRecord, Topic, Webpage
admin.site.register(AccessRecord)
admin.site.register(Topic)
admi.site.register(Webpage)

建立superuser
python manage.py createsuperuser

利用superuser帳號登入admin interface
http://127.0.0.1:8000/admin

完成

新增日期, 修改日期
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Django - Form概念筆記

此筆記將介紹Django強大的Input Form功能,以下為基本流程。

1.
新增form.py並定義之,內容類似model

2.
將form.py import views.py,此動作可選兩種方法:
from . import form
from form import FormName

3.
新增新的view給form,如下:
def form_name_view(request):
    form = forms.FormName()
    return render(request, 'form.html', {'form' : form})

4.
設定urls.py的路由

5.
新增template接應view的form,以及templates資料夾路徑

完成


How to inject the form?

<form method="post">
    {{ form.as_p }}
    {% csrf_token %}
    <input type="submit" value="Submit">
</form>

看了之後可能會有以下問題:
as_p是什麼?
inject 的form 加上as_p
將會被<p>包起來而完整的排好

{% csrf_token %}是什麼?
每當使用form發出http verb都要加上這個,否則form無法運行。此機制是利用django加密好的token來防止資安問題。

cleaned_data是什麼?
form input的資料,可利用form.cleaned_data['name']來讀取





2017年10月28日 星期六

Django - MTV架構筆記

MTV是Django Models, Templates, Views的縮寫

此筆記將紀錄開發Django app時的步驟

1.
import 要用的models於views.py

2.
進行query於views.py

3.
將query的結果傳到Template

4.
Template去接應Views.py的結果,如果控制流程必須要有收尾的Tag
如:{% endif %}、{% endfor %}

5.
urls.py設定路由


Django - 利用Faker產生填充資料

開發系統時,往往都會需要一些填充資料去做測試,此筆記將利用Faker來產生填充資料


安裝Faker
pip install Faker


新增py檔於專案跟目錄底下

import os
# Configure settings for project
# Need to run this before calling models from application!
os.environ.setdefault('DJANGO_SETTINGS_MODULE','first_project.settings')

import django
# Import settings
django.setup()

import random
from first_app.models import Topic,Webpage,AccessRecord
from faker import Faker

fakegen = Faker()
topics = ['Search','Social','Marketplace','News','Games']

def add_topic():
    t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
    t.save()
    return t



def populate(N=5):
    '''
    Create N Entries of Dates Accessed
    '''

    for entry in range(N):

        # Get Topic for Entry
        top = add_topic()

        # Create Fake Data for entry
        fake_url = fakegen.url()
        fake_date = fakegen.date()
        fake_name = fakegen.company()

        # Create new Webpage Entry
        webpg = Webpage.objects.get_or_create(topic=top,url=fake_url,name=fake_name)[0]

        # Create Fake Access Record for that page
        # Could add more of these if you wanted...
        accRec = AccessRecord.objects.get_or_create(name=webpg,date=fake_date)[0]


if __name__ == '__main__':
    print("Populating the databases...Please Wait")
    populate(20)
    print('Populating Complete')

備註:
get_or_create()用於讀取以及寫入,查詢不存在時即寫入



2017年10月25日 星期三

Django - 靜態檔案筆記

本筆記同Template需要新增資料夾、設定settings.py



1.新增名為static的file於專案根目錄(同templates),static裡再創一個images file,放入一張圖片近該資料夾


2.設定settings,新增STATIC_DIR變數如下:
STATIC_DIR = os.path.join(BASE_DIR, "static")

新增STATICFILES_DIRS串列,串列中放入剛剛創的STATIC_DIR變數如下:
STATICFILES_DIRS = [
    STATIC_DIR,
]

3.去瀏覽器輸入http://127.0.0.1:8000/static/images/xxx.jpg,將會顯示剛剛放的圖片


4.使用靜態檔案前必須在模板<html>之外加上以下:
{% load staticfiles %}

之後新增img tag
<img src="{% static "images/hp.jpg" %}" alt="me" >

完成


Django的靜態檔案也可以應用在css跟javascript,以css舉例:


1.在static資料夾新增css資料夾並放入css檔

2.模板加入如下
<link rel="stylesheet" type="text/css" href="{% static "css/mycss.css" %}">

完成


設定Media,settings.py新增如下:

MEDIA_DIR = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

如果settings.py
DEBUG = False
runserver 指令要加上--insecure

2017年10月24日 星期二

Django - Templates筆記

這邊要講如何利用Django的Template模板來完成動態網頁

1. 創建Templates資料夾於專案目錄底下

更改settings.py讓Django找到Templates的路徑

2. 賦予一個變數templates資料夾路徑
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates').replace('\\', '/')

找到TEMPLATE = []中的 DIRS 並貼上變數TEMPLATE_DIR

連接templates資料夾完成


3. 新增模板於templates,如index.html,內容加上模板標籤

# index.html
<h1>Hello index</h1>
{{ insert_me }} # 模板變數


4. 在views的function中,使用render()選擇index.html並傳參數給模板

def index(request): 
    my_dict = {'insert_me':'Hello I am from views.py'} 
    return render(request, 'index.html', context = my_dict)

完成



2017年10月23日 星期一

Django - View & Mapping

此筆記將簡略的記錄創造好app之後,該做的事情。

views.py裡新增function

urls.py新增 路徑與app.function做mapping


# views.py

from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
return HttpResponse('<em>My Second App</em>')



# urls.py

from django.conf.urls import url
from django.contrib import admin
from apptwo import views
urlpatterns = [
url(r'^$', views.index),
        url(r'^admin/', admin.site.urls),
]


現在我們已經知道要如何去做到Mapping了,可是會發現一個問題,假設有多個App,每個App都有多個Function,依照上述的設定urls.py將會變的很亂,而Django也有解決此問題的方法。

我們利用include()在project的urls.py中做app的設定,如下:

# first_project/urls.py

from django.conf.urls import url
from django.conf.urls import include # 新增這個

urlpatterns = [
url(r'^first_app/', include('first_app')), 
]


完成之後在App目錄底下新增urls.py

# first_app/urls.py

from django.conf.urls import url
from apptwo import views

urlpatterns = [
url(r'^index$', views.index, name='index'),
]


我大概解釋一下,上述設定將每個App的路由交給各自目錄的urls.py處理而不是全部都集中於project的urls.py,結果將會是http://127.0.0.1:8000/first_app/index


first_app/代表執行first_app這個App都要有first_app/,而之後的index代表著first_app中views的index function

有沒有覺得很神奇呢?

2017年10月21日 星期六

Django - Application筆記

專安中使用startapp新增wenb application
python manage.py startapp first_app

產生的first_app中會有以下檔案

# 作為package用
__init__.py

# 用來設定admin介面
admin.py

# 存放特定的設置
apps.py

# 用來設定資料模型
models.py

# 用於放置測試函式來測試程式碼
tests.py

# 用於控制請求與回應
views.py

# 存放資料庫與模組關聯的資訊
migrations資料夾

新增app之後,要去settings.py註冊新的app以便整個Django專案使用
找出INSTALLED_APPS,加入新的app


2017年10月14日 星期六

Django - 專案

指令django-admin

創造專案名稱為myproject
django-admin startproject myproject


新增專案後,資料夾裡會有以下五個py檔

 # treat django file as a python package  
__init__.py

# configuration
settings.py

# routes
urls.py

# for deploying production
wsgi.py

# include all the django commands as build the web app
manage.py 

使用runserver來啟動server,預設URL http://127.0.0.1:8000/
python manage.py runserver


Django - 虛擬環境與安裝

Virtual Enviroment虛擬環境

安裝Django於虛擬環境,方便安裝失敗重新安裝,或是控制使用新舊版本

安裝虛擬環境
pip install virtualenv

新增虛擬環境
virtualenv env_myproject

於新增虛擬環境的資料夾中輸入以下指令

Windows:
env_myproject\scripts\activate

OSX:
source env_myproject/bin/activate


安裝Django
pip install django

也可以指定版本
pip install django==1.11.2

BTW:注意在開啟虛擬機後才安裝Django,不然會被安裝在本機

2017年10月13日 星期五

Python - Name & Main筆記

常常會在程式的下看到如下:
if __name__ == '__main__':

__name__是什麼?

__name__用於顯示出目前主程式
主程式會顯示__main__
副程式會直接顯示module模組名稱,如:one.py # one

假如有兩個檔案one.py & two.py
# one.py

def func():
print('func() in one')

print('Top level one.py')
s is one.py {}'.format(__name__))


# two.py

import one
print('Top level two.py')
one.func()

print('This is two.py {}'.format(__name__))


執行two.py
python3 two.py

結果:
Top level one.py
This is one.py one
Top level two.py
func() in one
This is two.py __main__

2017年10月7日 星期六

Python - Decorators筆記

將函式當作引數傳給另外一個函式

def new_decorator(func):

    def wrap_func():
        print ("Code would be here, before executing the func")

        func()

        print ("Code here will execute after the func()")

    return wrap_func

def func_needs_decorator():
    print ("This function is in need of a Decorator")

func_needs_decorator()
# Reassign func_needs_decorator
func_needs_decorator = new_decorator(func_needs_decorator)

func_needs_decorator()


等同於以下簡化的方式

def new_decorator(func):

    def wrap_func():
        print ("Code would be here, before executing the func")

        func()

        print ("Code here will execute after the func()")

    return wrap_func

@new_decorator
def func_needs_decorator():
    print "This function is in need of a Decorator"

func_needs_decorator()

2017年10月6日 星期五

Python - Regular Expressions筆記

用法
找出you關鍵字,回傳布林值
import re

text = 'abc1234'

match = re.search('a',text)

if match:
    print(m.group())


利用split()分割字串成串列
print(re.split('@','yous421@gmail.com')) # return list

利用findall()回傳不重複的匹配
print(re.findall('you',text)) # return list



‘\’:跳脫字元
‘.’ :匹配除了\n外任何單一字元
‘*‘:匹配任何數字(包括零)
‘^‘:匹配開始,^abc
‘$‘:匹配結尾,56$
‘?‘:匹配非必須項目(零或一個)
‘|’:或
\d:一個數字
\D:一個非數字
\w:一個英數字元
\W:一個非英數字元
\s:一個空白字元
\S:一個非空白字元
\b:一個單字範圍(介於\w與\W,無論順序為何)
\B:一個非單字範圍

prev{m}:m個連續prev
prev{m, n}:m到n連續prev
prev{m, n}?:
[abc]:a或b或c
[^abc]:not a或b或c
prev(?= next):prev,如果後面有next
prev(?! next):prev,如果後面沒有next
(?<= prev) next:如果next之前是prev,匹配next
(?<! prev) next:如果next之前不是prev,匹配next

2017年10月4日 星期三

Python - Module & Package筆記

利用module使用其他py檔的方法及類別

# mymodule.py

def func_in_module():
    print('using module')

# myprogram.py

import mymodule as md # 使用別名

md.func_in_module

以下方法同上,缺點為容易佔用太多記憶體
from mymodule import *

為了防止記憶體佔用太多,只挑選要用的方法
from mymodule import func_in_module

放在資料夾的情況,以此類推
from os.mymoduls import func_in_module

將__init__.py加入資料夾建立Package
os/
- __init__.py
- func_in_module









2017年10月3日 星期二

Python - 例外處理筆記

用法

try:
f = open('cool', 'r') # mode code is supposed to be 'w'
f.write('Test text')
except Exception as e:
print('Error:{}'.format(e))
else:
        print('success')
finally:
print('finally')

Python - I/O筆記

open(filename, mode):

mode共兩碼
第一碼:
r -讀取
w - 檔案存在與否,均可寫入
x - 檔案不存在時,寫入
a - 檔案存在時,寫入結尾

第二碼:
t - 文字
b - 二進位

write():寫入檔案
read():讀取
readline():行為單位讀取
readlines():行為單位讀取,並建立串列
seek()
close()

Python - OOP筆記

宣告方式

class Dog():
    
    species = 'mammal'

    def __init__(self, name): # 初始化方法
        self.name = name
        print("{} has been created".format(self.name))

mydog = Dog("Blacky") 
print(mydog.mammal) # 類別屬性Class Attribute
print(Dog.mammal) # 類別屬性Class Attribute
print(mydog.name) # 實例(物件)屬性Instance Attribute



例子圓形類別,計算圓面積
class Circle():
pi = 3.14

def __init__(self, radius=1):
self.radius = radius

def area(self):
return (self.radius ** 2) * Circle.pi  # 圓面積公式需要特別註明是哪種類型的屬性

tiny = Circle(2)
print(tiny.area()) 



類別變數Counter為例

class Person:
count = 0
def __init__(self):
Person.count += 1
print(Person.count)

tom = Person()
jack = Person()
amy = Person()


繼承
class Person(Alien, God, Monster):  #可實行多重繼承


封裝
利用雙底線__來封裝變數

class Person:

def __init__(self, a=2, b=3):
self.__a=a
self.__b=b

def do(self):
print(self.__a + self.__b)


呼叫父類別的建構子
class Father():
    def __init__(self):
        print("last name Hsieh")

class Son(Father):
    def __init__(self):
        Father.__init__(self)
        print("Son created")



class Father():
    def __init__(self):
        print("last name Hsieh")

class Son(Father):
    def __init__(self):
        super(Son, self).__init__()
        print("Son created")



特別method名稱

class Book():
def __init__(self, name, pages):
self.name = name
self.pages = pages
def __str__(self):
return "Name:{}".format(self.name)
def __len__(self):
return self.pages
def __del__(self):
print("A book is deleted.")

b = Book("The Guide", 20)

print(b) # __str__
print(len(b)) # __len__
del b # __del__
更多參考https://openhome.cc/Gossip/Python/SpecialMethodNames.html

2017年9月30日 星期六

Python - Random筆記

import random

幫串列洗牌
a = list(range(1,11))
random.shuffle(a)

隨機產生一個範圍的整數
random.randint(0,10) # 0..9

隨機產生浮點數
random.random()
random.uniform(1, 10) # 1..9


隨機產生字元
random.choice('abcdefg') # b

選取多個字元轉換串列
random.sample('abcdefghij',3) # ['c', 'f', 'a']



Python - Function筆記

函式Function

宣告方式
def hello():
    print ('hello')

hello() # hello


設定引數
def hello(name):
    print('hello, {}'.format(name))

hello('Tom')


引數的預設值,呼叫function未設定引數
def hello(name='default'):
    print('hello, {}'.format(name))

hello()


function return值
def hello():
    return 'hello123'

h = hello()
print(h) # hello123

引數為*args時,為Tuple

引數為**args時,為Dictionary


Python - Flow Control 筆記

控制流程Flow Control為程式語言重要的一環,在很多語言中都看得到控制流程,以下我要介紹python的控制流程

Python的布林值Boolean分別為True & False,boolean開頭都是大寫
True 成立
False 不成立

and:全為真才是真
or:任一為真即為真

> 大於 greater than
3>4 # False
4>3 # True

< 小於 less than
5<7 # True
6<4 # False

== 等於 equal
'3' == '3' # True
1 == 2 # False

!= 不等於 not equal
3 != 4 # True
3!= 3 # False

if判斷式
if a > b :
    print('a>b')
elif b > a :
    print('b>a')
else:
    print('a==b')

迴圈Loops,用於處理(迭代iterate)連續的資料sequence

For Loops

迭代串列
seq = [1, 2, 3, 4, 5]
for item in seq:
    print(item)

迭代字典
seq = {'Tom':'29', 'John':'30', 'Amy':'15'}
for key in seq:
    print(key) # 連續的key
    print(seq[key]) # 利用key找出value

迭代串列內容為Tuple
seq = [(1, 2), (3, 4), (5, 6)]

for (tu1, tu2) in seq: # 括號()可省略
    print (tu2)
    print(tu1)

while loops
while i<5:
    print(i)
    i = i +1

利用range()建立數字序列
range(start, stop, step)
備註:stop前停止
range(10) # 0..9
range(1, 10) # 1..9
range(1, 10 , 2) # 奇數1..9
應用
list(range(0, 7)) # [0 ,1 ,2 ,3 ,4, 5, 6]
list(range(0, 7, 2)) # [0, 2, 4, 6]
for i in range(0, 7):
    print(i) # 0 1 2 3 4 5 6

串列生成式
seq = [1, 2, 3, 4]
a = [num**2 for num in seq] # [1, 4, 9 ,16]

如果使用range的情況
a = [num**2 for num in range(1,5)] # [1, 4, 9 ,16]


Python - Set筆記

集合Set有著不重複的特性

宣告方式
s = set()

加入值
s.add(1)
s.add(2)
s.add(2)
s.add(4)
s.add(3)
s.add(3)
print(s) # set([1, 2, 3, 4])

利用list()轉串列
print(list(s)) # [1, 2, 3, 4]

in():某鍵是否存在,回傳布林值
intersection():取得交集(同"&")
union():取得聯集(同"|")
difference():取得差集(同"-")
symmetric_difference():取得互斥(同"^")
issubset():檢查子集合(同"<=")
issuperset():檢查超集合(同">=")
<:檢查真子集
>:檢查真超集合









Python - Tuple筆記

Tuple與List相似,最大的不同即是Tuple內的值不可以更改,否則會發生錯誤

宣告方式
t = ()
t = (1, 2, 3, 4)

利用索引值取出Tuple內的值
print(t(1)) # 2

利用list()轉換成串列
print(list(t)) # [1, 2, 3, 4]





2017年9月29日 星期五

Python - Dictionary筆記

Python的字典(Dictionary)就是雜湊(Hash),運用方式一模一樣,只是說法的不同。
一個字典Key(鍵) & Value(值)可以是任何型態,字典並沒有順序性

宣告方式
d = {}

key & value
d = {"key1":"value1", "key2":"value2", "key3":"value3"}

利用key取value
print(d["key1"]) # value1

賦予key value,已經存在key的情況下
d["key1"] = "value has been modified"
print(d["key1"]) # value has been modified

加入新的key & value,不存在key的情況下
d["key4"] = "value4"
print(d) # {'key3': 'value3', 'key2': 'value2', 'key1': 'value1', 'key4': 'value4'}

巢狀字典
d = {
"key1":"value1",
"key2":"value2",
"key3":"value3",
"key4":{"key4-1":["It","is","a","list"]}
}
print(d["key4"]["key4-1"]) # ["It","is","a","list"]
print(d["key4"]["key4-1"][1]) # is


檢查字典key

if 'key1' in d:
    pass

刪除key

del d['key1']














Python - List筆記

宣告方式
a = []

串列List內容值可接受多種不同的型別
a = [1,2,"3",4.5,['a','b','c','d']]

舉例,除非另外宣告a串列,大部份範例都由以下a串列起始
a = [1,2,3,4,5]

使用len()存取陣列長度
print(len(a)) # 5 

串列可以利用索引值讀取值
print(a[2]) # 3

索引值可以為負值
a[-1] # 等同a[4] display 5

同樣也可以像字串擷取資料
a[2:] # [3, 4, 5]

利用append加入值至串列結尾
a.append(6);
print(a) # [1, 2, 3, 4, 5, 6]

結合不同的串列利用extend(),結合後b會結合在結尾
b = [6, 7, 8]
a.extend(b) 
print(a) # [1, 2, 3, 4, 5, 6, 7, 8]
補充:extend()也等同於+=

利用pop()彈出串列中的值
a.pop()
print(a) # [1, 2, 3, 4] 5被彈出,因為沒有參數預設彈出最後一個值
pop()也可以搭配索引值參數使用
a.pop(0)
print(a) # [2, 3, 4, 5] 1被彈出

利用insert()插入串列,新增在指定索引值裡,其餘的值索引值往後推
a.insert(2,"new")
print(a) # [1, 2, 'new', 3, 4, 5, 6]

利用reverse()顛倒串列
a.reverse()
print(a) # [5, 4, 3, 2, 1]

利用sort()排序串列值,使用sorted()將不會改變內容值而是更改於建立的副本中
b = [3,5,1,3,7,6,4,55,99]
b.sort()
print(b) # [1, 3, 3, 4, 5, 6, 7, 55, 99]

巢狀陣列讀取方式,二維串列為例
c = [['a','b','c'],['A','B','C']]
print(c[0][2]) # c

利用for迭代串列
matrix = [[1,2,3],[4,5,6],[7,8,9]]
first_col = [row[0] for row in matirx]
print(first_col) # [1,4,7]

利用tuple()轉成Tuple
print(tuple(a)) # (1, 2, 3, 4, 5)

利用set()轉成Set
a = [1, 1, 1, 2, 3, 3]
print(set(a)) # set([1, 2, 3])

利用index()尋找索引值
a.index('3') # 2

利用count()找出特定字串元出現次數
a.count('2') # 1








Python - String筆記

字串
a= "abcdefg"
a = 'abcdefg'

字串可直接轉為串列
a[2] # c

擷取索引值2到結尾的字串
a[2:]  # cdefg

擷取索引值2開始到5之前的字串
a[2:5] #cde

擷取跳過1個的字串
a[::2] # aceg

英文大寫
a = a.upper()

英文小寫
a = a.lower()

英文大小寫互換
a = a.swapcase()

開頭大寫
a = a.capitalize()

利用title()讓每個英文單字開頭大寫
a = a.title()

利用split轉換成串列
a = "1,2,3,4,5,6"
x = a.split(",")
print(x) # ['1','2','3','4','5','6']

內插字串
"{},{},{}".format("A","B","C") # A,B,C
"{a},{b},{c}".format(a="1", b="2", c="3") # 123

利用replace()替換字串
a = a.replace(old, new)
a = a.replace(old, new, max)

利用startswith(), endswith()檢查開頭以及結尾的字串元,回傳Boolean
a = 'abcdef'
print(a.startswith("b")) # False
print(a.startswith("a")) # True
print(endswith("e")) # False
print(endswith("f")) # True

利用find(), rfind()找出第一次以及最後一次出現字串元的索引值
a = 'abcdddefghi'
print(a.find('d')) # 3 第一次出現
print(a.rfind('d')) # 5 最後一次出現

利用count()找出特定的字串元出現幾次
a = 'abcdddefghi'
print(a.count('d')) # 3

利用isalnum()檢查是否只有英數
a = 'sdf123423sdfsf'
print(a.isalnum()) # True
a = '111!@#@#'
print(a.isalnum()) # False



Python - Number筆記

整數
a=2

浮點數
a=2.2

加法
a + 1

減法
a - 1

乘法
a * 2

除法
a / 2 

整數除法
a // 2  # 只取整數

取餘數
a % 2

次方
a**2

轉整數
a = 12.345
print (int(a) ) # display 12
print(type(a)) # <type 'int'>

轉浮點數
a = 12
print(float(a)) # diplay 12.0
print(type(a)) # <type 'float'>

轉字串
a = str(2)
print(a) # 2
print(type(a)) # <type 'str'>

Python - Lambda筆記

lambda提供一個匿名函式,可以用完即丟。

舉例個例,我們現在創造一個基數的檢驗器過濾以下串列

mylist = [1,2,3,4,5,6,7,8,9]

def even(num):
    return num % 2 == 0

evens = filter(num, mylist)

print(list(evens))

如果我們用lambda的話將會是

mylist = [1,2,3,4,5,6,7,8,9]

evens = filter(lambda num:num % 2 == 0 , mylist)

print(list(evens))

用法:
lambda args1, args2, args3... : expression

2017年9月20日 星期三

Java - 內部類別如何使用static 變數

class OutterClass{
    static class InnerClass {
static boolean STATUS = false;
    }
    public void do(){
      InnerClass.STATUS = true;
    }
}

OutterClass oc = new OutterClass();
oc.do();

2017年9月15日 星期五

Quotes

What is best in life?
To crush your enemies.
See them driven before you.
And hear the lamentations of their women.
From Conan the Barbarian



Crom, I have never prayed to you before.
I have no tongue for it. No one, not even you, will remember if we were good men or bad.
Why we fought, or why we died.
All that matters is that two stood against many.
That's what's important!
Valor pleases you, Crom... so grant me one request.
Grant me revenge! And if you do not listen, then to HELL with you!
From Conan the Barbarian



Lo, there do I see my father.
Lo, there do I see My mother, and my sisters, and my brothers.
Lo, there do I see The line of my people
Back to the beginning.
Lo, they do call to me.
They bid me take my place among them.
In the halls of Valhalla
Where the brave May live Forever.
From The 13th Warrior

2017年6月14日 星期三

Django 重點整理

Installation
pip install virtualenv
virtualenv env_myproject
env_myproject\scripts\activate
source env_myproject/bin/activate
pip install django==1.11.2

Create Project
django-admin startproject myproject
In settings.py
set debug mode and database

Run Server
python manage.py runserver

Create App
python manage.py startapp myapp
In settings.py INSTALLED_APPS
add myapp

Admin Interface
python manage.py migrate
python manage.py createsuperuser
In app’s admin.py
admin.site.register(Post)
models.py
def __str__(self):
        return self.title

Create Template file
In settings.py TEMPLATES
change
'DIRS': [],
To
'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/')],
creaete index.html in templates file
Create View
views in myapp
set Views

Mapping
In urls.py
url(r'^$', index),
url(r'^index/$', index),
url(r'^post/(?P<id>\d+)/$', post, name='post'),

Main template
In Main
{% block content %}{% endblock %}
In Sub
{% extends "main_template.html" %}
{% block content %}content{% endblock %}

Navigation Bar
In Main
{% include 'includes/_messages.html' %}

Filters
{{string|truncatewords:80}}
{{string|lower}}
{{string|escape|linebreaks}}
{{ post.created_at|date:"Y / m / d" }}

Model
set Model
python manage.py makemigrations
python manage.py migrate
import model to views

CRUD
from trips.models import Post
Create
Post.objects.create(name=arg)
Read
Post.objects.all()
Post.objects.get(pk=1)
Update
posts = Post.objects.filter(title__contains='Trip')
Delete
posts.delete()


Flash

Form

2017年6月8日 星期四

Python 重點整理

Number

宣告方法 a = 1
//:整數除法
**:次方
int():轉整數
float():轉浮點數
str():轉字串

String

宣告方法 a = "1"
單引號雙引號皆可
字串間可以省略 "+"來做連接
三個單、雙引號可以連接跨行的字串
\:跳脫符號
split():字串轉串列
len():取得字串長度
startswith():檢查開頭
endswith():檢查結尾
find():第一次出現位移值
rfind():最後一次出現位移值
count():字串出現次數
isalnum():是否只有字母、數字
strip():分割字串成串列
capitallize():第一個字母大寫
title():所有單字第一個字母大寫
upper():全改大寫
lower():全改小寫
swapcase():大小寫互換
center():置中,引數為空白鍵數量
ljust():置左,引數為空白鍵數量
rjust():置右,引數為空白鍵數量
replace():替換
List
宣告方法 a = []
一個串列可以有多種型態的元素
append():加入串列尾段
insert():指定加入串列
extend():結合串列(同"+=")
del():利用位移值刪除
remove():利用值刪除
pop():利用位移值刪除,或不指定位移值默認刪除最後一個串列值
index():利用值尋找位移值
in():某值是否存在,回傳布林值
count():計算某值出現次數
join():串列轉字串
sort():排序並改變變數
sorted():排序並不改變數,建立副本
len():取得串列長度
copy():複製串列

Tuple

宣告方法 a = ()
宣告之後元素不可修改
宣告Tuple每個元素後都要加逗號",",結尾可以不用加
Tuple一次可指派多的變數
tuple():轉換串列成Tuple

Dictionary

宣告方法 a = {}
a = {"key1": "value1", "key2": "value2"}
dict():轉換雙值序列成字典
update():合併字典
del():利用鍵刪除
clear():清空字典(同"={}")
in():某鍵是否存在,回傳布林值
get():取得某鍵值
keys():取得所有的鍵,可利用list()轉換為串列
values():取得所有值,可利用list()轉換為串列
items():取得所有鍵與值,鍵值以Tuple型態回傳
copy():複製字典

Set

宣告方法 a = set()
set():轉換為set
in():某鍵是否存在,回傳布林值
intersection():取得交集(同"&")
union():取得聯集(同"|")
difference():取得差集(同"-")
symmetric_difference():取得互斥(同"^")
issubset():檢查子集合(同"<=")
issuperset():檢查超集合(同">=")
<:檢查真子集
>:檢查真超集合

Iteration

zip():跌代多個序列,最短序列結束時停止
range():回傳指定範圍的一串數字
sum():計算可迭代物件總和

生成式

產生1-5整數串列
>>>a = [a for a in range(1,6)]
[1,2,3,4,5]
結合if 產生奇數
>>>a = [a for a in range(1,6) if a % 2 == 1]
[1,3,5]
雙for
cell = [(row, col) for row in rows for col in cols]
for cell in cells:
    print(cell)

Function

*引數:Tuple
**引數 :字典
lambda():匿名函式

Namespace

globals():全域
locals():區域

Standard Library

setdefault():當字典沒有找到指定鍵時,使用預設的值回應
defaultdict():宣告字典時,當字典沒有找到指定鍵時,使用預設的值回應
Counter():計數器,回傳字典
most_common():依照降冪回傳元素
OrderedDict():記住加入字典鍵的順序
deque():堆疊+序列
itertools():跌代用搭配函式chain(),cycle(),accmulate()
pprint():印出自動排列的元素

OO

property():設定gette,setter
@property:裝飾器,放在getter之前
@name.setter:裝飾器,放在setter之前
@classmethod:裝飾器,類別方法
@staticmethod:裝飾器,靜態方法
Duck Typeing:

Unicode

encode():編碼
decode():解碼
import re
match()
search()
findall()
split()

I/O

open(filename, mode):

mode二碼
第一碼:
r -讀取
w - 檔案存在與否,均可寫入
x - 檔案不存在時,寫入
a - 檔案存在時,寫入結尾

第二碼:
t - 文字
b - 二進位

write():寫入檔案
read():讀取
readline():行為單位讀取
readlines():行為單位讀取,並建立串列
seek():

DB API

connect():設定引數,連接資料庫
cursor():建立物件處理查詢指令
execute():執行SQL指令
executemany():執行多個SQL指令
fetchone():取得execute
fetchmany():取得多個execute
fetchall():取得全部execute

NoSql

安裝Redis:pip install redis
宣告
import redis
conn = redis.Redis()

String
set():設定鍵,值
get():取得值
setnx():鍵不存在時才會設定
getset():回傳舊值,設定新值
getrange():取得子字串
setrange():替換子字串
mset():一次設定多個鍵
delete():刪除鍵
incr():遞增
incrbyfloat():遞增
decr():遞減
decrbyfloat():

List(只能放字串)
lpush():插入一個或多個於開頭
linsert():插入某值之前與之後
lset():插入於指定位移值
rpush():插入於結尾
lindex():利用位移值取值
lrange():取得位移值範圍的值
ltrim():擷取位移值範圍串列

Hash(只能放字串)
hmset():設定欄位值
hset():設定單一欄位值
hget():取得欄位值
hmget():取得多個欄位值
hkeys():取得所有欄位鍵
hvals():取得所有欄位值
hlen():取得欄位數量
hgetall():取得所有欄位鍵/值
hsetnx():設定欄位值,當欄位不存在時

Set
sadd():加入一個或多個集合
scard():取得集合值數量
smembers():取得所有集合值
srem():移除集合值
sinter():取得交集
sinterstore():取得交集並存入進集合
sunion():取得聯集
sunionstore():取得聯集並存入進集合
sdiff():取得差集
sdiffstroe():取得差集並存入進集合

Sorted Set
zadd():加入
zrank():取得數列
zscore():取得值
zrange():數列範圍的串列

Cache
expire():設定逾期時間

For Test

using doctest
import doctest
doctest.testmod()


Flask
run():debug=true 除錯網頁


繼承
class Person(Alien, God, Monster):  #可實行多重繼承

封裝
利用雙底線__來封裝變數

class Person:

def __init__(self, a=2, b=3):
self.__a=a
self.__b=b

def do(self):
print(self.__a + self.__b)

類別變數Counter為例

class Person:
count = 0
def __init__(self):
Person.count += 1
print(Person.count)

tom = Person()
jack = Person()
amy = Person()


呼叫父類別的建構子
class Father():
    def __init__(self):
        print("last name Hsieh")

class Son(Father):
    def __init__(self):
        Father.__init__(self)
        print("Son created")


特別method名稱
class Book():
def __init__(self, name, pages):
self.name = name
self.pages = pages
def __str__(self):
return "Name:{}".format(self.name)
def __len__(self):
return self.pages
def __del__(self):
print("A book is deleted.")

b = Book("The Guide", 20)

print(b) # __str__
print(len(b)) # __len__
del b # __del__
更多參考https://openhome.cc/Gossip/Python/SpecialMethodNames.html

//lambda用完即丟 可搭配 map(), filter()等等
lambda num: n % 2==0