2013年4月2日 星期二

[Django]引用ajax,出現csrf錯誤



2013年3月11日 星期一

[Django] refresh div with JQuery

Example:

Template 1 

<script>

function refresh_infos(element){
    var id = "test"
    $.ajax({
        type: "POST",
        url: '/part_update/',
        data: {'id':id},
        success: function(data) {
            $('#testDIV').html(data);
        }
    });
}
</script>
<div id="testDIV"></div>



View 

def part_update(request):

    id = request.POST.get('id', "")
   
    #/* get infomation */
    infos = DB.objects.filter(id = id)
   

    return render_to_response('supply/template.html', { 'infos': infos })

Template 2 
    {% for info in infos %}
        <p>{{ info.id }}</p>
    {% endif %}


Urls 
    adding:
    (r'^/part_update/$','part_update'),

2013年3月9日 星期六

[Codeigniter]ajax upload


Simple Example:

Controller:

public function upload_file(){
        $file_element_name = $_POST['file_element_name'];
        $config['file_name'] = "test.jpg";
        $config['upload_path'] = "/upload/";
        $config['allowed_types'] = 'gif|jpg|png|doc|txt|pdf';
        $config['max_size']  = 1024 * 8;
        $config['encrypt_name'] = FALSE;
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);
            if (!$this->upload->do_upload($file_element_name)){
                $status = 'error';
                $msg = $this->upload->display_errors('', '');
            }else{
                $data = $this->upload->data();
                $file_id = true;
                if($file_id){
                    $status = "success";
                    $msg = "File successfully uploaded";
                }else{
                    unlink($data['full_path']);
                    $status = "error";
                    $msg = "Something went wrong when saving the file, please try again.";
                }
            }
            @unlink($_FILES[$file_element_name]);
        }
        $upload_info = $this->upload->data();
        echo json_encode( array(
            "status" => $status,
        ));
}


View:

<form method="post" action="" id="form">
        <input type="file" name="file" id="file" />
</form>

<script type="text/javascript">
    $(function() {
        $('#form').submit(function(e) {
            $.ajaxFileUpload({
                url         :'<?=site_url("upload/")?>',
                secureuri      :false,
                fileElementId  :'file',
                dataType    : 'json',
                data        : {
                    'file_element_name' : 'file'
                },
                success  : function (data){
                    alert(data.status);
                }
            });
            return false;
      });
    }
</script>



2013年2月21日 星期四

[Code_Note] 引用外部API - 設計排版

在設計排版部分,可以將外部API獨立成method,
只要將需要的資料利用變數丟入即可
這樣可以加強程式的彈性


ex:



requestData("1")

def requestData(a):

   request_data =
   '''<%s xmlns="urn:ebay:apis:eBLBaseComponents">'''%a


2013年1月30日 星期三

[Django] 如何取得django form資料


先簡單記錄一下,如何在python後台取得前端傳送的django form資料:


if request.method == "POST":
    form = xxForm(request.POST , prefix='fd')
    if form.is_valid(): # 通過驗證才可取得資料
        print form.cleaned_data['col']
else:
   
    print "It's not from POST !!"

2013年1月21日 星期一

[Python] 開發環境檔案夾選擇


小技巧:

在不同系統中可以建立多個不同變數:

local_folder = 'D:/projects/a'
online_folder = '/django_projects/b'


利用if判斷讓系統自行判斷存入資料夾

if os.path.exists(local_folder):
    base_folder = local_folder  
if os.path.exists(online_folder):
    base_folder = online_folder