首页 > 解决方案 > 输出未显示在 django 框架中

问题描述

下面是我的代码!我想在 html 中运行 python 脚本,但是每次执行时我都看不到输出!请帮忙!下面是我的代码!我想在 html 中运行 python 脚本,但是每次执行时我都看不到输出!请帮忙!下面是我的代码!我想在 html 中运行 python 脚本,但是每次执行时我都看不到输出!请帮忙!下面是我的代码!我想在 html 中运行 python 脚本,但是每次执行时我都看不到输出!请帮忙!

views.py

from django.shortcuts import render
import requests
def button(request):
    return render(request,'home.html')

def output(request):
    data=requests.get('https://www.google.com/')
    print(data.text)
    data=data.text
    return render(request,'home.html',{'data':data})
urls.py

from django.contrib import admin
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
    url(r'admin/', admin.site.urls),
    url(r'^$', views.button),
    url(r'^$', views.output, name="script") 

]
home.html
</!DOCTYPE html>
<html>
<head>
    <title>Python Script</title>
</head>
<body>

<button onclick="location.href='{% url 'script' %}'">Click Me</button>
<hr>
{% if data %}
{{data | safe}
{% endif %}

</body>
</html>

标签: pythonhtmldjango

解决方案


推荐阅读