Archive

文章標籤 ‘Form’

使用django modelform的save()之後,可以取得model的資料

2012年2月21日 尚無評論

原來在用modelform時,還有一個方便的小地方
原文件請看:https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

當新增一筆資料後,我想取得這筆資料的id,其實可以這樣做:
model:
[ccc language="python"]
class ADProject(models.Model):
platform = models.ForeignKey(Platform)
name = models.CharField(_(‘name’), max_length=200)
[/ccc]

form:
[ccc language="python"]
class ADProjectBGMAdminForm(forms.ModelForm):
class Meta:
model = ADProject
[/ccc]

view:
[ccc language="python" highlight="5″]
def adproject_create(request):
form = AdprojectForm(request.POST or None)

if form.is_valid():
f = form.save()
return HttpResponse(‘platform id is:%s, this new id is:%s’ % (str(f.platform.id), str(f.id)))

return render(request, ‘adproject-create.html’, {
‘form’: form
})
[/ccc]

重點就在第5行,似乎是方便多了

Categories: Python Tags: , ,

Zend Framework 產生 FORM

2011年1月21日 尚無評論

產生留言版的 FORM

指令:
zf create form Guestbook

andy@nb3vm:~/www/rdlab/application$ zf create form Guestbook
Creating a form at /home/andy/www/rdlab/application/forms/Guestbook.php
Updating project profile ‘/home/andy/www/rdlab/.zfproject.xml’

產生 application/forms/Guestbook.php

編輯一下原本的內容修改成我們需要的,如下:

<?php
class Application_Form_Guestbook extends Zend_Form
{

    public function init()
    {
        /* Form Elements & Other Definitions Here ... */
        // Set the method for the display form to POST
        $this->setMethod('post');

        // Add an email element
        $this->addElement('text', 'email', array(
            'label'      => 'Your email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'EmailAddress',
            )
        ));

        // Add the comment element
        $this->addElement('textarea', 'comment', array(
            'label'      => 'Please Comment:',
            'required'   => true,
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(0, 20))
                )
        ));

        // Add a captcha
        $this->addElement('captcha', 'captcha', array(
            'label'      => 'Please enter the 5 letters displayed below:',
            'required'   => true,
            'captcha'    => array(
                'captcha' => 'Figlet',
                'wordLen' => 5,
                'timeout' => 300
            )
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Sign Guestbook',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }
}

接著要將 signAction() 加到 GuestbookController 以處理 FORM 的遞送
用以下的方式建立 action並且關聯到 view 裡

andy@nb3vm:~/www/rdlab/application$ zf create action sign Guestbook
Creating an action named sign inside controller at /home/andy/www/rdlab/application/controllers/GuestbookController.php
Updating project profile ‘/home/andy/www/rdlab/.zfproject.xml’
Creating a view script for the sign action method at /home/andy/www/rdlab/application/views/scripts/guestbook/sign.phtml
Updating project profile ‘/home/andy/www/rdlab/.zfproject.xml’

經由執行結果看到新增了一個 signAction() 方法到 controller 以及相對應的 view 內容。

閱讀全文…

Categories: PHP, Zend Framework Tags: ,