验证HTML标记,如果它们是有效的HTML标记还是不是python



我正在使用Django来构建网站,我想检查我是否使用了有效的HTML标记。这是我的密码。

class Actions:
view_class = None
action_name = None
action_code = None
action_icon = None
action_attr = None
action_title = "icon"  # icon,text ,both
with_url = False
actions_template = "core/core/actions/action.html"
action_css = []
action_js = []
action_class = "btn btn-secondary hide action-item"
action_position = "above_form"  # above_form, above_grid, both ,on_form , on_grid
action_tag = "a"
def render_actions(self, request):
return render_to_string(
self.actions_template,
{
"action_name": self.action_name,
"action_code": self.action_code,
"action_icon": self.action_icon,
"action_class": self.action_class,
"action_title": self.action_title,
"action_tag": self.action_tag,
"with_url": self.with_url,
"action_attr": self.action_attr,
"with_modal": self.with_modal,
"form_name": form_name,
"action_url": reverse_lazy(self._related.__class__.__name__.lower()) if self.with_url else "",
},
)

例如,如果action_tag的值是buttun,则代码应该引发错误;如果action_tag的值是button或任何有效的HTML标记,则不会引发错误。我怎么能离线而不是在线做这件事。

首先,您可以在constants.py文件中存储所有html标记的列表:

html_tags = ['<html>', '<base>', '<head>', '<link>', '<meta>', '<style>', '<title>', '<body>', '<address>', '<article>', '<aside>', '<footer>', '<header>', '<h1>', '<h2>', '<h3>', '<h4>', '<h5>', '<h6>', '<main>', '<nav>', '<section>', '<blockquote>', '<dd>', '<div>', '<dl>', '<dt>', '<figcaption>', '<figure>', '<hr>', '<li>', '<ol>', '<p>', '<pre>', '<ul>', '<a>', '<abbr>', '<b>', '<bdi>', '<bdo>', '<br>', '<cite>', '<code>', '<data>', '<dfn>', '<em>', '<i>', '<kbd>', '<mark>', '<q>', '<rp>', '<rt>', '<ruby>', '<s>', '<samp>', '<small>', '<span>', '<strong>', '<sub>', '<sup>', '<time>', '<u>', '<var>', '<wbr>', '<area>', '<audio>', '<img>', '<map>', '<track>', '<video>', '<embed>', '<iframe>', '<object>', '<param>', '<picture>', '<portal>', '<source>', '<svg>', '<math>', '<canvas>', '<noscript>', '<script>', '<del>', '<ins>', '<caption>', '<col>', '<colgroup>', '<table>', '<tbody>', '<td>', '<tfoot>', '<th>', '<thead>', '<tr>', '<button>', '<datalist>', '<fieldset>', '<form>', '<input>', '<label>', '<legend>', '<meter>', '<optgroup>', '<option>', '<output>', '<progress>', '<select>', '<textarea>', '<details>', '<dialog>', '<menu>', '<summary>', '<slot>', '<template>']

我是用这个页面写的(没有不推荐使用的元素,但你可以添加它们(。

如果您的操作标签不在该列表中,那么您可以很容易地引发异常:

if action_tag not in html_tags:
raise ValueError('Not a valid html tag.')

最新更新