惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
博客园 - 聂微东
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
L
LangChain Blog
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
S
Secure Thoughts
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Help Net Security
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
G
Google Developers Blog
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
Kaspersky official blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives

博客园 - 福娃

Ubuntu 16.04 LTS更新 程序员如何走出迷茫的困境? PHP vs Python Apache2.4中开通HTTP基本认证 NPM 与 left-pad 事件:我们是不是早已忘记该如何好好地编程? Groovy split竖杆注意 使用Flask-Migrate进行管理数据库升级 Fabric自动部署太方便了 程序员的复仇:11行代码如何让Node.js社区鸡飞狗跳 CAS认证原理图 grails 私有库相关设置 A successful Git branching model String to Date 多种格式转换 IBatis.Net如何支持多个数据库 [django]the story about Django and TurboGears Since NHibernate 1.2.0, objects are lazy by default - 福娃 [Castle]Castle.Model被Castle.Core代替了 [Castle]Castle也范型 - 福娃 - 博客园 [Castle]Asp.Net中获取Castle容器中的服务的另一方法
[django]newforms两种方式示例
福娃 · 2007-02-11 · via 博客园 - 福娃

newforms是django新的对表单处理功能,查看官方文档:
http://www.djangoproject.com/documentation/newforms/
最近也在看newsform的文章,总结了两种使用的方法,如下所示:
一、根据model自动生成newsform
model:

# Create your models here.
class ResType(models.Model):
    typename 
= models.CharField('typename',maxlength=20)
    
    
class Admin: pass
    
    
def __str__(self):
        
return self.typename
    
class Resource(models.Model):
    title 
= models.CharField('标题',maxlength=200)
    content 
= models.TextField('内容',null=True, blank=True)
    resdate 
= models.DateTimeField('发布日期',default=DEFAULT_DATE, blank=True)
    restype 
= models.ForeignKey(ResType)
    
class Admin: 
        list_display 
= ('title',)
    
def __str__(self):
        
return self.title

view:

from django import newforms as forms

def

 public(request, id=None):
    entry 
= None
    
if id is None:
        EntryForm 
= forms.models.form_for_model(Resource)
    
else:
        entry 
= Resource.objects.get(id=id)
        EntryForm 
= forms.models.form_for_instance(entry)
 
    
#EntryForm.fields['detail'].widget = TinyMCE()
 
    
if request.method == 'POST':
        form 
= EntryForm(request.POST)
        
if form.is_valid():
            entry 
= form.save(commit=False)
            entry.save()
 
            
return HttpResponseRedirect("/")
    
else:
        form 
= EntryForm()
 
    t 
= loader.get_template('public.html')
 
    c 
= Context({'form': form,'resource':entry})
    
return HttpResponse(t.render(c)) 

template:

{% load i18n utiltags %}
{% extends "base.html" %}

{% block head%}

{% endblock %}

{% block body %}  

<form enctype="multipart/form-data" action="/resource/public/{%ifnotequal resource None %}{{resource.id}}/{%endifnotequal%}" method="post" id="resource_edit">
        
<div id="message">ddd</div>
        
<table>
        {{ form }}
        
</table>
        
<input type="submit" value="OK">
        
    
</form>
{% endblock %}

这一种方式主要有如下关键点
1.EntryForm = forms.models.form_for_model(Resource)
生成一个具有Model模型的空的Form,用来显示添加表单
2.EntryForm = forms.models.form_for_instance(entry)
根据一个对象来生成含有该对象数据的Form,用来显示带数据的表单
3.数据保存直接可以通过Post过来的数据创建一Form实例,并调用该实例的save方法

二、自定义Form
有时候通过Model自动创建的Form不适合,譬如:Model中的某些属性我不需要显示在页面上,或数据处理方式比较复杂,这个时候你就需要自定义Form.我们先看看代码:
自定义的Form:

from django.newforms import *
from apps.resource.models import ResType,Tag,Resourceclass ResourceForm(Form):
    title 
= CharField(label='标题',required=True)
    content 
= CharField(label='内容',widget=Textarea(attrs={'rows'10'cols'50}))
    resdate 
= DateField(label='发布日期')
    restype 
= ChoiceField(label='类别',
        choices
=[(c.id,c.typename) for c in ResType.objects.all()])
   
    
def __init__(self, data=None, auto_id='id_%s', prefix=None,initial=None, instance=None):
        
if instance is not None:
            self.instance 
= instance
            new_data 
= {}
            new_data[
"title"= instance.title
            new_data[
"content"= instance.content
            new_data[
"resdate"= instance.resdate
            new_data[
"restype"= instance.restype
            data 
= new_data
        
else:
            self.instance 
= None
        super(ResourceForm, self).
__init__(data, auto_id, prefix, initial) 
    
    
def save(self, commit=True):
        
if self.instance is not None:
            instance 
= self.instance
        
else:
            instance 
= Resource()
        
        
print 'test:'+ self.clean_data["restype"]
        
        instance.title 
= self.clean_data["title"]
        instance.content 
= self.clean_data["content"]
        instance.resdate 
= self.clean_data["resdate"]
        instance.restype 
= ResType.objects.get(id=self.clean_data["restype"])
        
        
if commit:
            instance.save()
        
return instance 

该自定义Form类中主要有两部分:
1、重写构造函数,主要是传递数据
2、自定义save方法,用来将Form接收的数据进行处理

View:

def add_entry(request,id=None):
    
if id is not None:
        instance 
= Resource.objects.get(id=id)
        InstanceForm 
= ResourceForm(instance=instance)
    
else:
        InstanceForm 
= ResourceForm()
        
    
if request.POST:
        form1 
= ResourceForm(request.POST)
        
if form1.is_valid():
            form1.save()
            
return HttpResponseRedirect('/')
            
 
    t 
= loader.get_template('add_entry.html')
 
    c 
= Context({'form': InstanceForm})
    
return HttpResponse(t.render(c)) 

其实View部分和第一种方式很相似的,不同的是Form的创建方式。
Template和以前一样。

只写这么多,作为自己学习的一个记录。

参考资料:
http://code.pui.ch/2007/01/07/using-djangos-newforms/
http://www.djangoproject.com/documentation/newforms/
http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/forms/tests.py