2011-05-26 74 views
2

我使用sqlalchemy在數據庫中創建一個表,現在想根據數據庫使用django製作一個表單,並使用formencode來驗證它。 (提我使用Django Web框架) 的Python代碼低於Python django sqlalchemy和formencode

from sqlalchemy import * 
from sqlalchemy.orm import * 

engine = create_engine('mysql+mysqldb://root:@localhost/testdb', echo = True) 

metadata = MetaData(engine) 

session = create_session() 

one_table = Table('one', metadata, 
       Column('id',Integer, primary_key = True), 
       Column('name',String(40)) 
      ) 
many_table = Table('many_i', metadata, 
       Column('id', Integer, primary_key = True), 
       Column('name', String(40)), 
       Column('one_id',Integer, ForeignKey('one.id')) 
       ) 

metadata.create_all(engine) 

class One(object): 
def __init__(self, name): 
    self.name = name 
def __repr__(self): 
    return self.name 
#pass 

class Man_i(object): 
def __init__(self, name): 
    self.name = name 

def __repr__(self): 
    return self.name 
#pass 

mapper(One, one_table, 
    properties={'o2m':relationship(Man_i) 
       } 
    ) 

mapper(Man_i, many_table) 
+1

請提供更具體的說明你的問題。 – 2011-05-26 04:32:55

+0

我使用sqlalchemy在數據庫中創建了一個表,現在想根據數據庫製作一個表單,並使用formencode有效。 (提及我使用Django Web Framework) – Sabbir 2011-05-26 06:21:43

+0

你真的在使用Django嗎?哪一點,因爲你也使用sqlalchemy和formencode? – 2011-05-26 06:36:38

回答

2

最後我克里特我自己的解決方案,可以在任何一個喜歡我比這更好的 **,如果任何一個希望,他們可以用它其真正保存數據到數據庫(不評論來自settings.py-此行> MIDDLEWARE_CLASSES - >>#「django.middleware.csrf.CsrfViewMiddleware」,使用POST方法)**

from django.shortcuts import render_to_response 

from sqlalchemy import * 
from sqlalchemy.orm import * 

import formencode 
from formencode import validators, htmlfill 

engine = create_engine("mysql+mysqldb://root:@localhost/testdb",echo = True) 
#create database link 

metadata = MetaData(engine) 
session = create_session() 

#database table 
person_table = Table('person', metadata, 
        Column('id', Integer, primary_key = True), 
        Column('name', String(40)), 
        Column('age', Integer), 
        Column('about', String(100)) 
        ) 

metadata.create_all(engine)#create database if not exist 

class Person(object):#create a class for mapping 
    def __init__(self,name,age,about): 
     self.name = name 
     self.age = age 
     self.about = about 
    def __repr__(self): 
     return self.name, self.age, self.about 

mapper(Person, person_table) #define map 

class PersonValid(formencode.Schema):#for validation 
    name = validators.String(not_empty=True, min = 3, max = 40) 
    age = validators.Int(not_empty = True, min=1, max=120) 
    about = validators.String(not_empty=True, min = 5, max = 100) 


def insert_d(request): #insert def 

#for template page 
    out = """ 

    <table> 
     <tr> 
     <td>Name:</td> 
     <td><input type="text" name = "name"/> 
     <form:error name = "name"/> 
     <!--form:iferror name="name">Horrible horror message</form:iferror--> 
     <td> 
     </tr> 
     <tr> 
     <td>Age:</td> 
     <td><input type="text" name="age"/> 
     <form:error name="age" /> 
     <!--form:iferror name="age">Horrible horror message</form:iferror--> 
     </td> 
     </tr> 
     <tr> 
     <td>About</td> 
     <td><textarea name="about"></textarea> 
     <form:error name="about" /> 
     <!--form:iferror name="about">Horrible horror message</form:iferror--> 
     <td> 
     </tr> 
     <table> 
     <input type = "submit" value = "Submit"> 

    """ 

    if request.method == 'POST': 
     inp = {'name': request.POST['name'], 
       'age': request.POST['age'], 
       'about': request.POST['about'] 
       } 
     try: 
      PersonValid.to_python(inp) 

      a_person = Person(['name'], 
           inp['age'], 
           inp['about'] 
          ) 

      session.add(a_person) 
      session.flush() 
      return render_to_response('formencode/htmlfill.html', 
             {'out': htmlfill.render(out,{}), 
             'text':"Saved OK"} 
            ) 


     except validators.Invalid, e: 

      val = htmlfill.render(out, e.error_dict or {}) 

    else: 
     return render_to_response('formencode/htmlfill.html', 
            {'out':out,'text':"POST NOT START"} 
           ) 


    #html form 

    #the htmlfill.html locate in "templete/formencode/" 
    directory contain the page below 

<form name="out" method="POST" action="/fcode_alch/"> 

{%autoescape off%}  
{{text}} 
{{out}}  
{%endautoescape%}