‘IT DOC’ Posts

 

django view太大,切分到不同的目录

Read full article  | 评论关闭

(新增目录下面一定得有__init__.py,    方法1,需要修改__init__.py,  方法2可以将__init__.py 保持为空)

备注:view 子目录引用model, 可用 from app_name.models import * 方式引用, 记得加上 app_name

方法1:

Remove the views.py file and create a directory named views. Add a __init__.py file inside it and create the separated view files.

|∙∙core/
  |∙∙__init__.py
  |∙∙admin.py
  |∙∙migrations/
  |∙∙models.py
  |∙∙tests.py
  |∙∙urls.py
  |∙∙views/
    |∙∙__init__.py
    |∙∙alpha.py
    |∙∙beta.py

views/__init__.py

This is an important step: import all the modules inside each view file.

from .alpha import *
from .beta import *

views/alpha.py

from django.shortcuts import render

def view_a(request):
    return render(request, 'view_a.html')

def view_b(request):
    return render(request, 'view_b.html')

views/beta.py

from django.shortcuts import render

def view_c(request):
    return render(request, 'view_c.html')

def view_d(request):
    return render(request, 'view_d.html')

urls.py

You don’t need to change anything here.

from django.conf.urls import url
import .views

urlpatterns = [
    url(r'^aaa$', views.view_a, name='view_a'),
    url(r'^bbb$', views.view_b, name='view_b'),
    url(r'^ccc$', views.view_c, name='view_c'),
    url(r'^ddd$', views.view_d, name='view_d'),
]

方法2:

Remove the views.py file and create a directory named views. Add a __init__.py file inside it and create the separated view files.

|∙∙core/
  |∙∙__init__.py
  |∙∙admin.py
  |∙∙migrations/
  |∙∙models.py
  |∙∙tests.py
  |∙∙urls.py
  |∙∙views/
    |∙∙__init__.py
    |∙∙alpha.py
    |∙∙beta.py

views/alpha.py

from django.shortcuts import render

def view_a(request):
    return render(request, 'view_a.html')

def view_b(request):
    return render(request, 'view_b.html')

views/beta.py

from django.shortcuts import render

def view_c(request):
    return render(request, 'view_c.html')

def view_d(request):
    return render(request, 'view_d.html')

urls.py

You don’t need to change anything here.

from django.conf.urls import url
import .views

urlpatterns = [
    url(r'^aaa$', views.view_a, name='view_a'),
    url(r'^bbb$', views.view_b, name='view_b'),
    url(r'^ccc$', views.view_c, name='view_c'),
    url(r'^ddd$', views.view_d, name='view_d'),
]



Read More

virtualenv 准备django 环境

Read full article  | 评论关闭

virtualenv ./env_chq -p /opt/apps/python/bin/python

pip install django

django-admin startproject pj_chq .

python manage.py startapp app_chq

Read More

根据列表内字典的某个KEY 进行排序

Read full article  | 评论关闭

方法1 (lambda):

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 
备注:  如果key类型为不同类型,注意一下转换,比如int  (k: int(k['name']))

方法2:

from operator import itemgetter

newlist = sorted(list_to_be_sorted, key=itemgetter('name')) 

newlist = sorted(list_to_be_sorted, key=itemgetter('name'), reverse=True)

Read More

python mongo Q

Read full article  | 评论关闭

 

from mongoengine.queryset.visitor import Q as mongo_Q

from django.db.models import Q as normal_Q

Read More

减小AWS EC2 根分区大小

Read full article  | 评论关闭

 

https://serverfault.com/questions/673048/how-to-reduce-aws-ebs-root-volume-size

 

核心点:

1,容量小的EBS, 一定不能直接创建,而是从一个小容量ROOT EBS 克隆 (snapshot, recovery)过来的,这样克隆过来的,才有分驱表,不然根本就不能启动

2. 小容量EBS 的e2label 一定要与 /etc/fstab 里面的label  一致

3. 小容易EBS 的UUID, 应用到grub.conf , 不然照样不能启动 (可用blkid 查看 UUID)

blkid /dev/sda6

/dev/sda6: LABEL=“/” UUID=”ae6dcc02-3f7f-47cc-8a6e-e29218b4d345 ” TYPE=”ext3″ SEC_TYPE=”ext2 ”

 

 

sudo rsync -aHAXxSP /mnt/snap/ /mnt/small

 

 

Read More

调整AWS EC2 根分区大小

Read full article  | 评论关闭

 

https://serverfault.com/questions/673048/how-to-reduce-aws-ebs-root-volume-size

Read More

jenkins 以自定义时区运行

Read full article  | 评论关闭

 

java -Duser.timezone=Asia/Shanghai -Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Shanghai -jar jenkins.war

 

两个参数都需要

Read More

AWS EC2 调整centos 云主机根卷大小

Read full article  | 评论关闭

[root@ip-10-0-8-58 ~]# fdisk -u /dev/xvda

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’).

Command (m for help): p

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0×00098461

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1 * 2048 41943039 20970496 83 Linux

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-41943039, default 63): p
First sector (63-41943039, default 63): 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039

Command (m for help): p

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0×00098461

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1 2048 41943039 20970496 83 Linux

Command (m for help): a
Partition number (1-4): 1

Command (m for help): p

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0×00098461

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1 * 2048 41943039 20970496 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource
busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@ip-10-0-8-58 ~]# reboot

Broadcast message from root@ip-10-0-8-58
(/dev/pts/0) at 4:03 …

The system is going down for reboot NOW!
[root@ip-10-0-8-58 ~]# Connection to 54.223.220.111 closed by remote host.
Connection to 54.223.220.111 closed.

$ ssh -i aws-ssh-key.pem root@54.223.220.111
Last login: Wed Jul 29 03:59:36 2015 from 223.71.247.139
[root@ip-10-0-8-58 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 7.9G 802M 6.7G 11% /
tmpfs 7.3G 0 7.3G 0% /dev/shm
/dev/xvdb 493G 198M 467G 1% /mnt
[root@ip-10-0-8-58 ~]#
[root@ip-10-0-8-58 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 7.9G 802M 6.7G 11% /
tmpfs 7.3G 0 7.3G 0% /dev/shm
/dev/xvdb 493G 198M 467G 1% /mnt
[root@ip-10-0-8-58 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 20G 0 disk
└─xvda1 202:1 0 20G 0 part /
xvdb 202:16 0 500G 0 disk /mnt
[root@ip-10-0-8-58 ~]# resize2fs /dev/xvda1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/xvda1 to 5242624 (4k) blocks.
The filesystem on /dev/xvda1 is now 5242624 blocks long.

[root@ip-10-0-8-58 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 806M 18G 5% /
tmpfs 7.3G 0 7.3G 0% /dev/shm
/dev/xvdb 493G 198M 467G 1% /mnt

Read More

innodb optimize

Read full article  | 评论关闭

 

MySQL Innodb Settings
The most important ones are:
innodb_buffer_pool_size 70-80% of memory is a safe bet. I set it to 12G on 16GB box.
UPDATE: If you’re looking for more details, check out detailed guide on tuning innodb buffer pool
innodb_log_file_size – This depends on your recovery speed needs but 256M seems to be a good balance between reasonable recovery time and good performance
innodb_log_buffer_size=4M 4M is good for most cases unless you’re piping large blobs to Innodb in this case increase it a bit.
innodb_flush_log_at_trx_commit=2 If you’re not concern about ACID and can loose transactions for last second or two in case of full OS crash than set this value. It can dramatic effect especially on a lot of short write transactions.
innodb_thread_concurrency=8 Even with current Innodb Scalability Fixes having limited concurrency helps. The actual number may be higher or lower depending on your application and default which is 8 is decent start
innodb_flush_method=O_DIRECT Avoid double buffering and reduce swap pressure, in most cases this setting improves performance. Though be careful if you do not have battery backed up RAID cache as when write IO may suffer.
innodb_file_per_table – If you do not have too many tables use this option, so you will not have uncontrolled innodb main tablespace growth which you can’t reclaim. This option was added in MySQL 4.1 and now stable enough to use.

Also check if your application can run in READ-COMMITED isolation mode – if it does – set it to be default as transaction-isolation=READ-COMMITTED. This option has some performance benefits, especially in locking in 5.0 and even more to come with MySQL 5.1 and row level replication.

 

 

innodb_buffer_pool_size

innodb_log_file_size

innodb_log_buffer_size=4M

innodb_flush_log_at_trx_commit=2   ?

 

innodb_flush_method=O_DIRECT

innodb_file_per_table

transaction-isolation=READ-COMMITTED   ?   repeatable-read

 

innodb_write_io_threads = 8

innodb_read_io_threads = 8

innodb_thread_concurrency=8     ?

 

innodb_thread_sleep_delay  10000

Read More

RDS 清除日志

Read full article  | 评论关闭

 

PROMPT> CALL mysql.rds_rotate_slow_log;
PROMPT> CALL mysql.rds_rotate_general_log;

CALL mysql.rds_skip_repl_error;
 

Read More