flask: AttributeError: 'NoneType' object has no attribute 'medlist'

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderators: mindphp, ผู้ดูแลกระดาน

penrudee
PHP Newbie
PHP Newbie
Posts: 6
Joined: 19/04/2020 7:52 am

flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by penrudee »

เราได้ลองเขียน code web ด้วย flask sqlalchemy แต่เกิดปัญหา error ตามภาพตลอดเลย รบกวนผู้รู้ช่วยแนะนำหน่อยครับ

Image

models.py

Code: Select all

class MedList(db.Model):
    m_id = db.Column(db.Integer, primary_key=True)
    medlist = db.Column(db.String(200), nullable = False)
    medlist = db.Column(db.String(200),unique = True, nullable = False)
    med_unit = db.Column(db.Integer)
    med_howToUse = db.Column(db.String(200))
    med_disease = db.Column(db.String(200))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    complete = db.Column(db.Boolean)
    
    
    def __repr__(self):
        return f"MedList('{self.m_id}','{self.medlist}','{self.med_howToUse}','{self.med_disease}')"
routes.py

Code: Select all

@app.route('/drug',methods=['GET','POST'])
    def drug():
      form = adddrug()
      eform = editdrug()
      details = MedList.query.all()
      exists = bool(MedList.query.all())
      if exists == False and request.method == 'GET':
            flash(f'Add drug to view','info')
      
     elif eform.validate_on_submit() and request.method == 'POST':
        d_id = request.form.get("drugid","")
        d_name = request.form.get("drugnamess","")
        details = MedList.query.all()
        drugss = MedList.query.filter_by(m_id = d_id).first()
        drugss.medlist = eform.editname.data
        drugss.med_disease = eform.editDisease.data
        drugss.med_howToUse = eform.editHowToUse.data
        try:
            db.session.commit()
            flash(f'Your drug list has been updated!','success')
            return redirect('/drug')
        except IntegrityError:
            db.session.rollback()
            flash(f'This drug already exists', 'danger')
            return redirect('/drug')
       elif form.validate_on_submit()  and request.method == 'POST':
            druglist = MedList(medlist = form.drugname.data, med_disease = form.drugDisease.data, 
            med_howToUse = form.drugHowToUse.data)
            db.session.add(druglist)
          try:
            db.session.commit()
            flash(f'Your drug {form.drugname.data} has been added!', 'success')
            return redirect(url_for('drug'))
          except IntegrityError:
            db.session.rollback()
            flash(f'This drug already exists','danger')
            return redirect('/drug') 
      return render_template('drug.html',title='Drug List',details=details,eform=eform)
form.py

Code: Select all

class adddrug(FlaskForm):
    drugname = StringField('Add Drug Name')
    drugDisease = StringField('Disease')
    drugHowToUse = StringField('Dosage')
    drugsubmit = SubmitField('Save Add Change')

class editdrug(FlaskForm):
    editname = StringField('Edit Drug Name')
    editDisease = StringField('Disease')
    editHowToUse = StringField('Dosage')
    editsubmit = SubmitField('Save Edit Change')
html file

Code: Select all

{% extends "base.html" %}

{% block app_content %}
<div class="container">
    <h1>Drug List</h1>
    <div>
        <h3 style="display: inline-block;margin: 0;">Manage Drug List</h3>
        <button type="button" class="btn btn-add" data-toggle="modal" data-target
        ="#exampleModal">
        <span class="btn-icon-add"><i class="glyphicon glyphicon-plus"></i>Add Drug</span>
    </button>
    </div>
</div>
<!-- add Modal code-->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="margin-top:150px;">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h3 class="modal-title" id="exampleModalLabel">Add Product</h3>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div id="modal-cont" class="modal-body" >
          <form  action="" method="POST" id="drugs">
            {{ form.csrf_token() }}
            {{form.drugname.label(class="form-control-label")}}<br>
            {{ form.drugname(class="form-control form-control-lg") }}
  
            {{form.drugDisease.label(class="form-control-label")}}<br>
            
            {{form.drugDisease(class="form-control form-control-lg")}}
            

            {{ form.drugHowToUse.label(class="form-control-label")}}<br>
            {{ form.drugHowToUse(class="form-control form-control-lg")}}
  
  
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <div >
            {{ form.drugsubmit(class="btn btn-primary") }}
          </div>
        </div>
      </form>
     </div>
   </div>
  
  </div>
  <!--end of modal design-->

<table class="table table-hover">
    <thead>
     <tr>
       <th>ID</th>
       <th>Drug Name</th>
       <th>Condition</th>
       <th>Dosage</th>
       <th></th>
       <th></th>
     </tr>
    </thead>
    <tbody>
      {% for d in details %}
      <tr>
       <td>{{d.m_id}}</td>
       <td>{{d.medlist}}</td>
       <td>{{d.med_disease}}</td>
       <td>{{d.med_howToUse}}</td>
       
       <td>
           <button type="button" class="btn btn-edit" data-toggle="modal" data-target = "#edit{{d.m_id}}" >
           <span class="btn-icon"><i class="glyphicon glyphicon-pencil"></i></span>Edit</button>
    
       </td>
       <!--editmodal-->
   <div class="modal fade" id="edit{{d.m_id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="margin-top:150px;">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h3 class="modal-title" id="exampleModalLabel">Edit Drug List</h3>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div id="modal-cont" class="modal-body" >
          <form  action="" method="POST" id="drugs">
            <input type="hidden" name="drugid" value="{{d.m_id}}"/>
            <input type="hidden" name="drugnamess" value="{{d.medlist}}"/>
            {{ eform.csrf_token() }}
            {{ eform.editname.label(class="form-control-label") }}<br>
            {{ eform.editname(class="form-control form-control-lg",placeholder=d.medlist) }}

            {{ eform.editDisease.label(class="form-control-label") }}<br>
            {{ eform.editDisease(class="form-control form-control-lg",placeholder=d.med_disease) }}

            {{ eform.editHowToUse.label(class="form-control-label")}}<br>
            {{ eform.editHowToUse(class="form-control form-control-lg",placeholder=d.med_howToUse) }}
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <div >
             
            {{ eform.editsubmit(class="btn btn-primary") }}
            
          </div>
        </div>
      </form>
     </div>
   </div>

  </div>
  <!--editmodal end-->


       <td>
        <a href="{{ url_for('delete', d_id= d.m_id, type='drug' )}}">
         <button type="button" class="btn btn-delete">
         <span class="btn-icon"><i class="glyphicon glyphicon-trash"></i></span>Delete</button></a>
     </td>
   </tr>
  {%endfor%}
  </tbody>
  
  </table>
  <script type="text/javascript">
  
  </script>


{% endblock %}
User avatar
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
Posts: 25918
Joined: 22/09/2008 6:18 pm
Contact:

Re: flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by mindphp »

ลอง comment บรรทัดที่ error ออกไปก่อน
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
penrudee
PHP Newbie
PHP Newbie
Posts: 6
Joined: 19/04/2020 7:52 am

Re: flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by penrudee »

ทดลอง comment ไล่เรียงทีละบรรทัดผลจะเป็นเช่นเดิมครับ
#drugss.medlist = eform.editname.data
#drugss.med_disease = eform.editDisease.data
drugss.med_howToUse = eform.editHowToUse.data
try:
penrudee
PHP Newbie
PHP Newbie
Posts: 6
Joined: 19/04/2020 7:52 am

Re: flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by penrudee »

ถ้าผมกดปุ่มแก้ไขใน id ที่มีอยู่ในฐานข้อมูลแล้วจะไม่ปรากฎ error
แต่ถ้ากด Add drug ใหม่ จะไม่ error
penrudee
PHP Newbie
PHP Newbie
Posts: 6
Joined: 19/04/2020 7:52 am

Re: flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by penrudee »

อีกประเด็นที่ผมสงสัยคือ
ถ้าผมกด ปุ่ม edit ">>>> code ควรทำงานที่ฟังชั่นนี้

Code: Select all

elif eform.validate_on_submit() and request.method == 'POST':
        d_id = request.form.get("drugid","")
        d_name = request.form.get("drugnamess","")
        details = MedList.query.all()
        drugss = MedList.query.filter_by(m_id = d_id).first()
        drugss.medlist = eform.editname.data
        drugss.med_disease = eform.editDisease.data
        drugss.med_howToUse = eform.editHowToUse.data
        try:
            db.session.commit()
            flash(f'Your drug list has been updated!','success')
            return redirect('/drug')
        except IntegrityError:
            db.session.rollback()
            flash(f'This drug already exists', 'danger')
            return redirect('/drug')
ถ้ากดปุ่ม +Add drugs ก็ควรวิ่งไปทำงานที่

Code: Select all

 elif form.validate_on_submit()  and request.method == 'POST':
            druglist = MedList(medlist = form.drugname.data, med_disease = form.drugDisease.data, 
            med_howToUse = form.drugHowToUse.data)
            db.session.add(druglist)
          try:
            db.session.commit()
            flash(f'Your drug {form.drugname.data} has been added!', 'success')
            return redirect(url_for('drug'))
          except IntegrityError:
            db.session.rollback()
            flash(f'This drug already exists','danger')
            return redirect('/drug') 
แต่เหมือนกับ elif form... และ elif eform... จะไม่ทำงาน
แต่ code ทำงานที่บรรทัด elif eform เลยเพราะเป็นส่วนที่อยู่ก่อน
User avatar
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
Posts: 25918
Joined: 22/09/2008 6:18 pm
Contact:

Re: flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by mindphp »

ถ้า Error หลายๆ ลอง เช็คเวอร์ชั่น Python ในเครื่องเรา กับ ที่ framework ตัวนี้ต้องการก่อนครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
penrudee
PHP Newbie
PHP Newbie
Posts: 6
Joined: 19/04/2020 7:52 am

Re: flask: AttributeError: 'NoneType' object has no attribute 'medlist'

Post by penrudee »

mindphp wrote: 20/04/2020 4:01 pm ถ้า Error หลายๆ ลอง เช็คเวอร์ชั่น Python ในเครื่องเรา กับ ที่ framework ตัวนี้ต้องการก่อนครับ
จะลองดูครับ
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Programming - C/C++ & java & Python”

Who is online

Users browsing this forum: Bing [Bot] and 10 guests