[Book]Python 201

Python 201

Posted in python | Leave a comment

custom form 생성하기

in order to apply attributes to a custom form you have to change form.custom.begin before.

The following code should work:
#view html
{{extend ‘layout.html’}}
# add class to form
form[‘_class’] = “form-horizontal”
# change form.custom.begin
form.custom.begin = XML(“<%s %s>” % (form.tag, form._xml()[0]))
your content form
Moreover, if do you want a form with bootstrap layout you could simply add formstyle=’bootstrap” to the form via auth.settings.formstyle argument http://web2py.com/books/default/chapter/29/09?search=auth#Settings-and-messages avoiding to customize manually the html of the form.
 def login():
auth.settings.formstyle = ‘bootstrap’
form = auth.login()
return dict(form=form)
and in the view




In this group there are several discussions on this subject (i.e. https://groups.google.com/d/msg/web2py/gLTthVDhqFM/dWldB9xGavgJ)

Search for messages containing the word ‘_xml’
We are replacing the original form.custom.begin value with a new one within which is the ‘_class’ attribute
From web2py code documentation in gluon/html.py (class DIV):
DIV class has a tag variable that is the tag name
FORM is a subclass of DIV and updates it
therefore, if form is an istance of FORM, form.tag will be = ‘form’
_xml() is a helper for xml generation. Returns separately in a tuple:
– the component attributes
– the generated xml of the inner components
Component attributes start with an underscore (‘_’) and
do not have a False or None value. The underscore is removed.
A value of True is replaced with the attribute name.
Therefore form._xml()[0] returns the component attributes like ‘_action’, ‘_class’
by using XML you can prevent escaping
XML(“<%s %s>” % (form.tag, form._xml()[0]))
‘<form action=””>’

Il giorno martedì 20 novembre 2012 23:37:17 UTC+1, Daniele ha scritto:

Posted in python, web2py | Leave a comment

PDF파일을 첨부파일로 다운로그 하기



    response.headers[‘Content-Disposition’] = ‘attachment; filename=abc.pdf’# to force download as attachment
# replace abc with whatever pdf name you want the user to see.
    return response.stream(open(request.folder+’/static/temp.pdf’,’rb’),chunk_size=4096)
Posted in python, web2py | Leave a comment

Database에 파일 저장하는 방법



in model

        Field(‘attachment’, ‘upload’, uploadfield=’payload’),
        Field(‘filename’, length=512),
        Field(‘Description’, ‘text’),
        Field(‘payload’, ‘blob’),
in controller
att_id = db2.attachments.insert(
                    attachment = db2.attachments.attachment.store(…),
                    payload = stream.read(),
                    filename = fname,
                    Description = r.attachments.attachment_brief_description,
Posted in Uncategorized | Tagged , , , , | Leave a comment

User accessed site without logging in

User accessed site without logging in

로긴한적없는 유저가 다른유저로 로긴되어 있는 현상 발생

알고봤더니 uWSGI의 async버그로 발생한 문제,

uWSGI의 ipv6 address 처리 오류

forum: https://groups.google.com/d/msg/web2py/Tt8Rmp5UlCE/_p2gRxoviioJ

client ipaddress의 validation추가

git: https://github.com/web2py/web2py/commit/5a16a35ef2fa759eefdd20e67d2d41103dc48b10

Posted in python, web2py | Leave a comment

web2py 2.0 rc – lazy table


Posted in Uncategorized | Leave a comment

.table 과 DB migration

포럼에 자주 나오는 질문중에 하나가 db migration과 관련된 migration_enable, migrate, fake_migrate 과 .table에 관계에 대한 내용이다. 이번에도 Niplod가 또 정리해 줬네요.

1 .table 파일
.table 파일은 migration할때만 사용된다.
2. DAL, define_table
DAL정의를 보면 다음과 같이 migration관련 인자를 볼수 있는데 migration은 기본적으로 enable된 상태이다.

__init__(self, uri='sqlite://dummy.db', ...,

DAL이나 모든 define_table에 migrate_enabled=False or migrate=False가 설정된다면 web2py는 model 안에 테이블 선언을 (define_table) 신뢰하기 때문에 .table파일을 필요로 하지 않게 됩니다. 즉 .table파일이 있던없던 상관없다는 뜻입니다.

.table이 없는 상태에서 migrate=True인 경우에는 web2py가 .table파일을 생성합니다.

migrate이 .table파일을 기반으로 db table을 생성한다면(.table->db table) fake_migrate 은  model안의 테이블 선언(define_table)을 기준으로 .table파일을 생성하거나 갱신한다는 것입니다(define_table -> .table).

The bottomline is:

– .table files are just needed for migrations.

– If you have migrate_enabled=False or migrate=False on all tables (auth too), web2py is instructed to trust your models to be in sync with the database, so no .table files are neither created nor required.
– if you don’t have .table files and you turn migration on, if table files are not found web2py tries to create them (because if you don’t have any .table files, web2py believes that the underlying database doesn’t has them)
– fake_migrate enables a different behaviour: it creates/updates .table files according to your models without triggering any create/update on the database
Happy Coding~:)
Posted in DAL, web2py | Leave a comment