我是當前的web2py用戶,但發現我偶爾會回到Django(我開始的時候)。特別是在我想要使用web2py中尚不存在的某些特定django應用程序/插件/擴展的項目上工作時。從數據庫模式生成自定義表格
有一件事,我不能沒有在web2py,我正在尋找解決方案在Django,是從db表創建html表單,然後能夠自定義他們的外觀和佈局在查看,沒有JavaScript。
- 生成一個數據庫表
- 指定自定義CSS類/ IDS每個字段中生成的HTML表格(JS禁用)
- 每個地方的HTML表單:我找
主要事項通過在視圖中的方法調用的表單字段/元件在預先製備的HTML視圖
即
我有一個表A.在web2py中我可以做(在控制器):
def display_form():
form = SQLFORM(db.table_A)
#Can I do the following in Django? Assign custom CSS to each form field?
form.element(_name='email')['_class'] = = "custom_css_classes, list"
if form.accepts(request.vars, session):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return dict(form=form)
然後,在查看我可以這樣做:
form.custom.start
form.custom.widget.name
form.custom.widget.email
form.custom.widget.form_field_name
...
<div class="span-5 last"><input type="submit" class="register_btn" value="Sign Up"></input></div>
form.custom.end
上述需要DB表,創建一個HTML表單,然後讓我將每個單獨的表單字段粘貼在我想要的預製HTML中的任何位置(對傳遞的「表單」對象使用這些「自定義」方法調用。包括我分配給生成的html表單的每個單獨字段的自定義css類。不必擔心弄髒了我的JavaScript與
http://web2py.com/book/default/chapter/06?search=define_table
http://web2py.com/book/default/chapter/07?search=sqlform#SQLFORM
http://web2py.com/book/default/chapter/05?search=#Server-side-DOM-and-Parsing
http://web2py.com/book/default/chapter/07?search=form.custom
我該怎麼做上面在Django:關於上面的代碼細節
查看文檔佈局黑客。假設在需要運行我的應用程序的瀏覽器中禁用javascript。此外,我很樂意使用Django管理員。塔塔解決方案也歡迎!
鏈接到文章/教程/ howtos爲此將不勝感激。 此外,請使用您在響應中提到的方法制作上述代碼的等效結果...
其他文檔/示例:http://groups.google.com/group/django-users/browse_thread/thread/653af4944d904562/ff467e7e752efd89?hl=zh-CN&lnk=gst&q=Custom+CSS+for+newforms+using+ModelForm#ff467e7e752efd89 – watr 2010-08-08 04:24:25
您可以在一行中對web2py完全相同: db.mytable.myfield.widget = lambda f,v:SQLFORM.widgets.string.widget(f,v,_class ='special') – mdipierro 2010-08-13 16:57:18
而且didn'您是否注意到我爲了可讀性將代碼行分成兩行? – satoru 2010-08-14 00:00:52