【问题标题】:Prettify html page in django在 django 中美化 html 页面
【发布时间】:2021-03-23 16:57:41
【问题描述】:

所以我正在制作这个 Django 网站,它正是我想要的。但是现在我需要将带有缩进的 HTML 代码输出到 HTML 页面。我使用了 bs4 `s .prettify() 方法,但它只在控制台上显示缩进代码。这是我的views.py 主要功能

from django.shortcuts import render
import requests
from bs4 import BeautifulSoup as soup
from . forms import InForm


def main(request):
    if request.method == 'POST':
        form = InForm(request.POST)
        if form.is_valid():
            you = form.cleaned_data.get('you')
            htm = requests.get(you)
            dat = soup(htm.content, 'html.parser').prettify()
            print(dat)
            return render(request, 'main.html', {'dat':dat})
    else:
        form = InForm()
    return render(request, 'main.html', {'form':form})

我得到的输出:

I`m using an android emulator for the project

【问题讨论】:

标签: python django beautifulsoup python-requests


【解决方案1】:

出于缩进的目的,您需要 css 帮助

style.css

.article{
white-space: pre-line;
} 

main.html

<div class="article">
{{dat}}
</div>

【讨论】:

    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2014-10-07
    • 1970-01-01
    • 2020-11-21
    • 2015-10-10
    相关资源
    最近更新 更多