python
  • flask
  • 2013-10-27 42 views 0 likes 
    0

    我使用燒瓶創建聯繫人形成使用瓶時,我想使用這張聯絡表格,發送電子郵件獲得「語法錯誤:。EOL同時掃描字符串常量」上創建聯繫人的形式

    我不斷收到此錯誤。在我的終端每當我運行python routes.py

    File "routes.py", line 25 
        msg = Message(form.subject.data, sender='[email protected]<script type="text/javascript"> 
    
    SyntaxError: EOL while scanning string literal 
    

    這裏是我的routes.py文件。

    @app.route('/contact', methods=['GET', 'POST']) 
    def contact(): 
        form = ContactForm() 
    
        if request.method == 'POST': 
        if form.validate() == False: 
         flash('All fields are required.') 
         return render_template('contact.html', form=form) 
        else: 
         msg = Message(form.subject.data, sender='[email protected]<script type="text/javascript"> 
    /* <![CDATA[ */ 
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); 
    /* ]]> */ 
    </script>', recipients=['[email protected]<script type="text/javascript"> 
    /* <![CDATA[ */ 
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); 
    /* ]]> */ 
    </script>']) 
         msg.body = """ 
         From: %s <%s> 
         %s 
         """ % (form.name.data, form.email.data, form.message.data) 
         mail.send(msg) 
    
         return 'Form posted.' 
    
        elif request.method == 'GET': 
        return render_template('contact.html', form=form) 
    

    回答

    3

    使用多行字符串和收盤報價三重引號應該是來]後:

    msg = Message(form.subject.data, sender='''[email protected]<script type="text/javascript"> 
    /* <![CDATA[ */ 
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); 
    /* ]]> */ 
    </script>', recipients=['[email protected]<script type="text/javascript"> 
    /* <![CDATA[ */ 
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); 
    /* ]]> */ 
    </script>]''') 
    
    +0

    這完美地工作! –

    2

    你不能CRE吃了一個多行字符串'...'。您必須使用''' ... '''""" ... """

    嘗試:

    msg = Message(form.subject.data, sender="""[email protected]<script type="text/javascript"> 
    /* <![CDATA[ */ 
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); 
    /* ]]> */ 
    </script>', recipients=['[email protected]<script type="text/javascript"> 
    /* <![CDATA[ */ 
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); 
    /* ]]> */ 
    </script>"""]) 
    
    相關問題