分类: Python/Ruby
2013-09-11 17:08:42
6 表单的应用
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms 导入表单模块
from sshCMD.models import SerInfo
import paramiko
import os,sys
class UserForm(forms.Form):
name=forms.CharField() 定义表单类
def register(req):
if req.method == "POST":
form = UserForm(req.POST) 将类与表单绑定
if form.is_valid():
print form.cleaned_data
return HttpResponse('ok')
else:
form = UserForm()
return render_to_response('register.html',{'form':form,})
def index(request):
return render_to_response('index.html',)
def list_ip(request):
a = "hello"
ip_object = SerInfo.objects.all()
return render_to_response('index.html',{"ips":ip_object,})
------------------------------------------------------------------------------------
vim Templates/register.html
{{ form }}
------------------------------------------------------------
from django.conf.urls import patterns, include, url
#add by hans 20130906
from sshCMD.views import index,list_ip,register
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'serManager.views.home', name='home'),
# url(r'^serManager/', include('serManager.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^$',index),
(r'bootstrap/(?P
# (r'^search/$',search_form),
(r'^ips/$',list_ip),
(r'^reg/$',register),
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)