pagination flask ใช้ไม่ได้ แต่อีกไฟล์ใช้ได้ ทั้งที่วิธีเขียนเหมือนกัน

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

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

ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25602
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

pagination flask ใช้ไม่ได้ แต่อีกไฟล์ใช้ได้ ทั้งที่วิธีเขียนเหมือนกัน

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

code ออกมาแล้ว error โชว์แค่หน้าเดียว พอเปลี่ยนหน้าข้อมูลหายทั้งหน้า

โค้ด: เลือกทั้งหมด

 page, per_page, offset = get_page_args(page_parameter='page',
                                                   per_page_parameter='per_page')
            sql3 = "SELECT * FROM des_ur WHERE ur_id= '%s' " % (id1)
            sql3 = sql3.encode('utf-8')
            cur.execute(sql3)
            rows = cur.fetchall()

            total = len(rows)
            pagination = Pagination(page=page, per_page=per_page, total=total,
                                    css_framework='bootstrap4')
            print(rows[offset: offset + per_page])
            return render_template('inputurl.html',
                                   rows=rows[offset: offset + per_page],
                                   row=row,
                                   page=page,
                                   per_page=per_page,
                                   pagination=pagination)

โค้ด: เลือกทั้งหมด

    def post(self,r=None,s=None):
        try:
            con = p.connect(host='127.0.0.1', database='test', user='postgres', password='123')
            cur = con.cursor()
            URL = request.form['url']
            req = Request(URL, headers={
                'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'})

            urlinfo = urlparse(URL)
            start = time.time()
            ip = socket.gethostbyname(urlinfo.netloc)
            dns_tm = time.time() - start
            print('DNS:\t\t{:.3f} seconds'.format(dns_tm))

            start = time.time()
            html = str(urlopen(req).read())
            load_tm = time.time() - start
            print('load:\t\t{:.3f} seconds'.format(load_tm))
            print('w/o DNS:\t{:.3f} seconds'.format(load_tm - dns_tm))

            sql = """INSERT INTO ur (url,dns,load_ur,wo_dns)
                                        VALUES ('%s','%s','%s','%s') RETURNING id;""" % (
            URL, dns_tm, load_tm, load_tm - dns_tm)
            sql = sql.encode('utf-8')
            cur.execute(sql)
            id1 = str(cur.fetchone()[0])
            con.commit()
            soup = BeautifulSoup(html, "html.parser")

            for link in soup.find_all('a', href=re.compile('^https')):

                print(link.get('href'))
                urlinfo2 = urlparse(link.get('href'))
                req2 = Request(link.get('href'), headers={
                    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'})

                start = time.time()
                ip = socket.gethostbyname(urlinfo2.netloc)
                dns_tm2 = time.time() - start
                print('DNS:\t\t{:.3f} seconds'.format(dns_tm2))

                start = time.time()
                html = str(urlopen(req2).read())
                load_tm2 = time.time() - start
                print('load:\t\t{:.3f} seconds'.format(load_tm2))
                print('w/o DNS:\t{:.3f} seconds'.format(load_tm2 - dns_tm2))
                # time.sleep(1)
                url = str(link.get('href'))
                sql2 = "SELECT * FROM des_ur WHERE url = '%s' AND ur_id= '%s' " % (url, id1)
                sql2 = sql2.encode('utf-8')
                cur.execute(sql2)
                check = cur.fetchall()
                if check:
                    pass
                else:
                    sql5 = """INSERT INTO des_ur (url,dns,load_ur,wo_dns,ur_id)
                                              VALUES ('%s','%s','%s','%s','%s')""" % (
                        url, dns_tm2, load_tm2, load_tm2 - dns_tm2, id1)
                    sql5 = sql5.encode('utf-8')
                    cur.execute(sql5)
                    con.commit()

            sql4 = "SELECT * FROM ur WHERE id= '%s' " % (id1)
            sql4 = sql4.encode('utf-8')
            cur.execute(sql4)
            row = cur.fetchall()
            print(row)
            page, per_page, offset = get_page_args(page_parameter='page',
                                                   per_page_parameter='per_page')
            sql3 = "SELECT * FROM des_ur WHERE ur_id= '%s' " % (id1)
            sql3 = sql3.encode('utf-8')
            cur.execute(sql3)
            rows = cur.fetchall()

            total = len(rows)
            pagination = Pagination(page=page, per_page=per_page, total=total,
                                    css_framework='bootstrap4')
            print(rows[offset: offset + per_page])
            return render_template('inputurl.html',
                                   rows=rows[offset: offset + per_page],
                                   row=row,
                                   page=page,
                                   per_page=per_page,
                                   pagination=pagination)
        except Exception as e:
            current_app.logger.info(e)
            response = 'error'
            return str(e)

ส่วน code นี้ เขียนเหมือนกันแต่ใช้ได้ปกติ

โค้ด: เลือกทั้งหมด

@app.route('/')
def index():
    con = p.connect(host='127.0.0.1', database='test', user='postgres', password='123')
    cur = con.cursor()
    page, per_page, offset = get_page_args(page_parameter='page',
                                           per_page_parameter='per_page')
    sql4 = "SELECT * FROM ur  "
    sql4 = sql4.encode('utf-8')
    cur.execute(sql4)
    rows = cur.fetchall()
    total = len(rows)
    # pagination_users = get_users(offset=offset, per_page=per_page)
    pagination = Pagination(page=page, per_page=per_page, total=total,
                            css_framework='bootstrap4')

    return render_template('test.html',
                           users=rows[offset: offset + per_page],
                           page=page,
                           per_page=per_page,
                           pagination=pagination,
                           )
                           
ผลลัพธ์ระหว่าง 2 code
codeแรก
ezgif.com-video-to-gif (1).gif
ezgif.com-video-to-gif (1).gif (81.58 KiB) Viewed 813 times


codeสอง
ezgif.com-video-to-gif (2).gif
ezgif.com-video-to-gif (2).gif (232.06 KiB) Viewed 813 times
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41231
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: pagination flask ใช้ไม่ได้ แต่อีกไฟล์ใช้ได้ ทั้งที่วิธีเขียนเหมือนกัน

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

- ต้องส่ง parameter ของหน้า ที่กำลังกด ไป แล้วเอาตัวเลข รับแบบ get มาให้ งาน
*** ในภาพมองไม่เห็น url ว่ารูปแบบถูกหรือไม่

หา total ให้ count ด้วย SQL มาใช้ ถ้า นับทีละแถวด้วย python จะทำให้โปรแกรมทำงานช้า กินแรม
ติดตาม 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
ภาพประจำตัวสมาชิก
MBMoo
PHP VIP Members
PHP VIP Members
โพสต์: 25602
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: pagination flask ใช้ไม่ได้ แต่อีกไฟล์ใช้ได้ ทั้งที่วิธีเขียนเหมือนกัน

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

mindphp เขียน: 13/06/2020 1:09 am - ต้องส่ง parameter ของหน้า ที่กำลังกด ไป แล้วเอาตัวเลข รับแบบ get มาให้ งาน
*** ในภาพมองไม่เห็น url ว่ารูปแบบถูกหรือไม่

หา total ให้ count ด้วย SQL มาใช้ ถ้า นับทีละแถวด้วย python จะทำให้โปรแกรมทำงานช้า กินแรม
ค่ะพี่ ขอบคุณค่ะ

อันนี้เป็น url ของตัวที่ใช้ได้ค่ะ
C++ & java & Python-1.png
C++ & java & Python-1.png (50.94 KiB) Viewed 787 times


อันนี้ของตัวที่ใช้ไม่ได้ค่ะ
C++ & java & Python-2.png
C++ & java & Python-2.png (24.6 KiB) Viewed 787 times
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41231
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: pagination flask ใช้ไม่ได้ แต่อีกไฟล์ใช้ได้ ทั้งที่วิธีเขียนเหมือนกัน

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

กำหนด route เพื่อรับค่า page id ของหน้า เพิ่ม ทาง get

โค้ด: เลือกทั้งหมด

@app.route('/<int:id>',methods=['GET'])
ติดตาม 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
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 95