2017-05-07 1 views
0

해당 열을 선택하여 MySQL에 csv 파일을 업로드하려고하는데 문제가되면 경로가 변경되면 파일이 닫힙니다. 그래서 같은 경로에 두 개의 템플릿을 렌더링하려했습니다. 첫 번째는 파일을로드하고 두 번째는 열을 선택합니다. 첫 번째 템플릿에만 액세스 할 수 있습니다. 내가 함께 두 번째 양식을 테스트입니다 (env.is_submitted)하지만 난 그것을플라스크 다른 파일에 두 가지 양식을 렌더링하지만 첫 번째 양식을 얻을 수 있습니다.

@app.route('/upload', methods=['GET', 'POST']) 
    def upload(): 
    form = UploadForm() 
    global columnscsv, salessource 
    if form.validate_on_submit(): 
     try: 
     filename = secure_filename(form.csv.data.filename) 
     file = form.csv.data 

     if file and allowed_file(filename):     
      print 'file_path'     
      salessource = CSVSource(file, delimiter=',') 
      columnscsv = salessource.fieldnames 
      print columnscsv 

     finally: 
      return render(salessource) 
    return render_template('upload.html', form=form) 

    def render(salessource): 
     env = envForm() 
     if env.is_submitted(): 
      print "submitted" 
     return render_template('form.html',columnscsv = columnscsv ,env =env) 

하고 Upload.html

<html> 
    <head> 
    <title>Upload</title> 
     </head> 
    <body> 
     <form method="post" enctype="multipart/form-data" > 
     {{ form.hidden_tag() }} 
     {{ form.csv }} 
     <input type="submit"> 
    </form></body> 
    </html> 

 {% block body %} 
     <form name = "mapping" method="POST" enctype="multipart/form-data" > 
      {{ env.hidden_tag() }} 
      <table> 
     {% for csv in columnscsv %} 
     <tr> <td> {{ csv }}</td> 
      <td><select name = "{{ csv }}" >  
      <option >year </option> 
      <option >month</option> 
      <option >day</option>  
      <option>reference</option> 
      <option>designation</option> 
      </select></td> 
     </tr> 
     {% endfor %} 
     </table> 
      <input type="submit" value="Submit" name = "submit" > 
      </form> 

     {% endblock %} 
form.html "제출"인쇄합니다 제출하고 있지 않다하더라도

답변

0

양식을 제출할 때 form.html을 렌더링 할 수 있습니다 (render(salessource)은 양식 제출 확인에 포함됨). 따라서이 방법으로 "제출 됨"이 인쇄되지 않습니다. 당신이 템플릿을 렌더링하려면

,이 같은 일을 arround를 찾을 : 파일이 후 제출

  • 다시 자체에 리디렉션 제출 된 경우

    • 알고 임시로 session['fileName'] = filename 추가
    • 확인 session['fileName'] 렌더링 무엇을 템플릿을 선택할 수있는 경우

      @app.route('/upload', methods=['GET', 'POST']) 
      def upload(): 
          form = UploadForm() 
          global columnscsv, salessource 
          if form.validate_on_submit(): 
           try: 
            filename = secure_filename(form.csv.data.filename) 
            file = form.csv.data 
            session['fileName'] = filename 
      
            if file and allowed_file(filename):     
             print 'file_path' 
             salessource = CSVSource(file, delimiter=',') 
             columnscsv = salessource.fieldnames 
             print columnscsv 
            redirect(url_for('upload')) 
           except: 
            raise 
      
          if session.get('fileName') != None: 
           render_template('form.html',columnscsv = columnscsv ,env=env) 
          else: 
           return render_template('upload.html', form=form) 
      
    ,
  • +0

    고마워요.하지만 여전히 모르겠어요. 어떻게 form.html의 폼에 접근 할 수 있습니까? – nourelhouda

    +0

    '액세스'에 대한 더 자세한 정보가 있습니까? –

    +0

    템플릿을 'form.hml'로 렌더링하면 의미가 없습니다. 양식의 태그를 가져옵니다. 그럼 다른 해결책을 찾았습니다. 파일을 로컬로 업로드하고 다시 사용합니다. 고맙습니다 – nourelhouda

    관련 문제