37创客科创中心

 找回密码
 立即注册
查看: 990|回复: 4

在python文件中的动态模板

  [复制链接]

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
发表于 2024-6-5 14:36:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from flask import render_template_string


  2. def test_render_form_row(app, client, hello_form):
  3.     @app.route('/form')
  4.     def test():
  5.         form = hello_form()
  6.         return render_template_string('''
  7.                 {% from 'bootstrap5/form.html' import render_form_row %}
  8.                 {{ render_form_row([form.username, form.password]) }}
  9.                 ''', form=form)
  10.     response = client.get('/form')
  11.     data = response.get_data(as_text=True)
  12.     assert '<div class="form-row">' not in data
  13.     assert '<div class="row">' in data
  14.     assert '<div class="col">' in data
复制代码

回复

使用道具 举报

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
 楼主| 发表于 2024-6-5 14:56:16 | 显示全部楼层
message  动态消息代码
  1. from flask import flash, render_template_string


  2. def test_render_messages(app, client):
  3.     @app.route('/messages')
  4.     def test_messages():
  5.         flash('test message', 'danger')
  6.         return render_template_string('''
  7.                         {% from 'bootstrap5/utils.html' import render_messages %}
  8.                         {{ render_messages(dismissible=True) }}
  9.                         ''')

  10.     response = client.get('/messages')
  11.     data = response.get_data(as_text=True)
  12.     assert 'class="btn-close" data-bs-dismiss="alert" aria-label="Close">' in data
复制代码

回复

使用道具 举报

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
 楼主| 发表于 2024-6-5 14:57:35 | 显示全部楼层
form_row
  1. from flask import render_template_string


  2. def test_render_form_row(app, client, hello_form):
  3.     @app.route('/form')
  4.     def test():
  5.         form = hello_form()
  6.         return render_template_string('''
  7.                 {% from 'bootstrap5/form.html' import render_form_row %}
  8.                 {{ render_form_row([form.username, form.password]) }}
  9.                 ''', form=form)
  10.     response = client.get('/form')
  11.     data = response.get_data(as_text=True)
  12.     assert '<div class="form-row">' not in data
  13.     assert '<div class="row">' in data
  14.     assert '<div class="col">' in data
复制代码
回复

使用道具 举报

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
 楼主| 发表于 2024-6-5 14:58:28 | 显示全部楼层

动态表单案例代码

form 表单
  1. from flask import render_template_string
  2. from flask_wtf import FlaskForm
  3. from flask_bootstrap import SwitchField
  4. from wtforms import IntegerRangeField, DecimalRangeField


  5. def test_switch_field(app, client):

  6.     class TestForm(FlaskForm):
  7.         remember = SwitchField('Remember me', description='Just check this')

  8.     @app.route('/switch')
  9.     def test_switch():
  10.         form = TestForm()
  11.         return render_template_string('''
  12.         {% from 'bootstrap5/form.html' import render_form %}
  13.         {{ render_form(form) }}
  14.         ''', form=form)

  15.     response = client.get('/switch')
  16.     data = response.get_data(as_text=True)
  17.     assert 'Remember me' in data
  18.     assert 'custom-control custom-switch' not in data
  19.     assert 'form-check form-switch' in data
  20.     assert 'role="switch"' in data
  21.     assert '<small class="form-text text-body-secondary">Just check this</small>' in data


  22. # test render IntegerRangeField and DecimalRangeField
  23. def test_range_fields(app, client):

  24.     class TestForm(FlaskForm):
  25.         decimal_slider = DecimalRangeField()
  26.         integer_slider = IntegerRangeField(render_kw={'min': '0', 'max': '4'})

  27.     @app.route('/range')
  28.     def test_range():
  29.         form = TestForm()
  30.         return render_template_string('''
  31.         {% from 'bootstrap5/form.html' import render_form %}
  32.         {{ render_form(form) }}
  33.         ''', form=form)

  34.     response = client.get('/range')
  35.     data = response.get_data(as_text=True)
  36.     assert 'Decimal Slider' in data
  37.     assert 'Integer Slider' in data
  38.     assert 'form-range' in data


  39. def test_form_group_class(app, client, hello_form):
  40.     @app.route('/default')
  41.     def test_default():
  42.         form = hello_form()
  43.         return render_template_string('''
  44.                     {% from 'bootstrap5/form.html' import render_form %}
  45.                     {{ render_form(form) }}
  46.                     ''', form=form)

  47.     @app.route('/custom')
  48.     def test_custom():
  49.         form = hello_form()
  50.         return render_template_string('''
  51.                     {% from 'bootstrap5/form.html' import render_form %}
  52.                     {{ render_form(form, form_group_classes='mb-2') }}
  53.                     ''', form=form)

  54.     response = client.get('/default')
  55.     data = response.get_data(as_text=True)
  56.     assert '<div class="mb-3' in data
  57.     response = client.get('/custom')
  58.     data = response.get_data(as_text=True)
  59.     assert '<div class="mb-3' not in data
  60.     assert '<div class="mb-2' in data


  61. def test_form_group_class_config(app, client, hello_form):
  62.     app.config['BOOTSTRAP_FORM_GROUP_CLASSES'] = 'mb-4'

  63.     @app.route('/config')
  64.     def test_config():
  65.         form = hello_form()
  66.         return render_template_string('''
  67.                         {% from 'bootstrap5/form.html' import render_form %}
  68.                         {{ render_form(form) }}
  69.                         ''', form=form)

  70.     response = client.get('/config')
  71.     data = response.get_data(as_text=True)
  72.     assert '<div class="mb-3' not in data
  73.     assert '<div class="mb-4' in data


  74. def test_form_inline_classes(app, client, hello_form):
  75.     @app.route('/default')
  76.     def test_default():
  77.         form = hello_form()
  78.         return render_template_string('''
  79.                     {% from 'bootstrap5/form.html' import render_form %}
  80.                     {{ render_form(form, form_type='inline') }}
  81.                     ''', form=form)

  82.     @app.route('/custom')
  83.     def test_custom():
  84.         form = hello_form()
  85.         return render_template_string('''
  86.                     {% from 'bootstrap5/form.html' import render_form %}
  87.                     {{ render_form(form, form_type='inline', form_inline_classes='custom-inline-classes') }}
  88.                     ''', form=form)

  89.     response = client.get('/default')
  90.     data = response.get_data(as_text=True)
  91.     assert '<div class="mb-3' not in data
  92.     assert '<div class="col-12' in data
  93.     assert 'row row-cols-lg-auto g-3 align-items-center' in data
  94.     assert '<label class="sr-only' not in data
  95.     assert '<label class="visually-hidden' in data
  96.     response = client.get('/custom')
  97.     data = response.get_data(as_text=True)
  98.     assert 'row row-cols-lg-auto g-3 align-items-center' not in data
  99.     assert 'custom-inline-classes' in data


  100. def test_form_inline_classes_config(app, client, hello_form):
  101.     app.config['BOOTSTRAP_FORM_INLINE_CLASSES'] = 'custom-inline-classes'

  102.     @app.route('/config')
  103.     def test_config():
  104.         form = hello_form()
  105.         return render_template_string('''
  106.                         {% from 'bootstrap5/form.html' import render_form %}
  107.                         {{ render_form(form, form_type='inline') }}
  108.                         ''', form=form)

  109.     response = client.get('/config')
  110.     data = response.get_data(as_text=True)
  111.     assert 'row row-cols-lg-auto g-3 align-items-center' not in data
  112.     assert 'custom-inline-classes' in data
复制代码


回复

使用道具 举报

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
 楼主| 发表于 2024-6-5 14:59:27 | 显示全部楼层
themes主题
  1. from flask import current_app
  2. import pytest
  3. from tests.test_bootstrap4.test_themes import themes


  4. bootstrap5_themes = themes + [
  5.     'morph',
  6.     'quartz',
  7.     'vapor',
  8.     'zephyr',
  9. ]


  10. @pytest.mark.parametrize('theme', bootstrap5_themes)
  11. def test_bootswatch_local(theme, client):
  12.     current_app.config['BOOTSTRAP_SERVE_LOCAL'] = True
  13.     current_app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = theme
  14.     data = client.get('/').get_data(as_text=True)
  15.     assert 'https://cdn.jsdelivr.net/npm/bootswatch' not in data
  16.     assert f'bootswatch/{theme}/bootstrap.min.css' in data
  17.     with client.get(f'/bootstrap/static/css/bootswatch/{theme}/bootstrap.min.css') as css_response:
  18.         assert css_response.status_code != 404


  19. @pytest.mark.parametrize('theme', bootstrap5_themes)
  20. def test_bootswatch_cdn(bootstrap, theme, client):
  21.     current_app.config['BOOTSTRAP_SERVE_LOCAL'] = False
  22.     current_app.config['BOOTSTRAP_BOOTSWATCH_THEME'] = theme
  23.     data = client.get('/').get_data(as_text=True)
  24.     assert 'https://cdn.jsdelivr.net/npm/bootswatch' in data
  25.     assert f'dist/{theme}/bootstrap.min.css' in data
  26.     css_rv = bootstrap.load_css()
  27.     assert f'/bootstrap/static/css/bootswatch/{theme}/bootstrap.min.css' not in data
  28.     assert 'https://cdn.jsdelivr.net/npm/bootswatch' in css_rv
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-11 18:21 , Processed in 0.268556 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表