0
我有一个html模板,其中包含一个按钮,当您按该按钮时,它将在views.py中执行一个函数,我将列出它:
from django.shortcuts import render
from django.http import HttpResponse
import requests
from bs4 import BeautifulSoup
def button(request):
return render(request, 'home.html')
def output(request):
def trade_spider(max_pages):
dat = ''
page = 1
while(page <= max_pages):
search = 'Ipad'
url = 'https://www.ebay.com/sch/i.html?_from=R40&_nkw=' + search + '&_sacat=0&_pgn=' + str(page)
src = requests.get(url)
text = src.text
soup = BeautifulSoup(text, features="html.parser")
for link in soup.findAll('a', {'class': 's-item__link'}):
href = link.get('href')
title = link.string
if(title == None):
title = "Sorry the title was unavailable however you can check the price."
price = get_item_price(href)
dat += href + '\n' + title + '\n' + price
page+=1
return dat
def get_item_price(item_url):
src = requests.get(item_url)
text = src.text
soup = BeautifulSoup(text, features="html.parser")
for item_price in soup.findAll('span', {'class': 'notranslate'}):
price = item_price.string
return price
data = trade_spider(1)
return render(request, 'home.html',{'data': data})
home.html:
<!DOCTYPE html>
<html>
<head>
<title>
Python button script
</title>
</head>
<body>
<button>Execute Script</button> <hr>
{% if data %}
{{data | safe}}
{% endif %}
</body>
</html>
一切正常,但是当我运行页面时,它会显示按钮和我不希望的输出,我希望它在我按下按钮后显示输出。 python版本是3.6.0 django = 2.1,它在使用pipenv的virtualenv中,多数民众赞成在我拥有的所有细节上。如果您需要另一个文件的代码,也可以添加注释,以防您需要目录,并且其中的文件是一棵树:
C:.
│ Pipfile
│ Pipfile.lock
│
└───comparison
│ db.sqlite3
│ manage.py
│
├───comparison
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└───register
│ admin.py
│ apps.py
│ models.py
│ tests.py
│ views.py
│ __init__.py
│
├───migrations
│ __init__.py
│
└───templates
home.html
views.py:
from django.contrib import admin
from django.urls import path
from register.views import output
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', output)
]
答案 0 :(得分:0)
一个孤独的按钮本身不会做任何事情。
要使按钮可单击,请将按钮包装在<form>
元素内,并向表单action
加上要向其发送表单提交的页面。同时给它method
post
。同时为按钮提供type
中的submit
和一个值。像这样:
<form name="myform" method="post" action="/register/">
<button type="submit" value="execute">Execute Script</button>
</form>
然后在Django视图中,仅在单击按钮后才打印输出。 您可以通过查看请求方法是否为post以及提交按钮是否已提交来进行检查。
类似这样的东西:
def output(request):
data = False #Setting the default to false makes it easy to check for it in your template.
submitbutton= request.POST.get('execute') #Looking for the value of the button in the POST variables.
if submitbutton:
# Only if the button has been clicked, do we execute the code to get the output.
def trade_spider(max_pages):
dat = ''
page = 1
while(page <= max_pages):
search = 'Ipad'
url = 'https://www.ebay.com/sch/i.html?_from=R40&_nkw=' + search + '&_sacat=0&_pgn=' + str(page)
src = requests.get(url)
text = src.text
soup = BeautifulSoup(text, features="html.parser")
for link in soup.findAll('a', {'class': 's-item__link'}):
href = link.get('href')
title = link.string
if(title == None):
title = "Sorry the title was unavailable however you can check the price."
price = get_item_price(href)
dat += href + '\n' + title + '\n' + price
page+=1
return dat
def get_item_price(item_url):
src = requests.get(item_url)
text = src.text
soup = BeautifulSoup(text, features="html.parser")
for item_price in soup.findAll('span', {'class': 'notranslate'}):
price = item_price.string
return price
data = trade_spider(1)
# We still return the same thing, but unless the button has been clicked, the value of data will remain False
return render(request, 'home.html',{'data': data})
现在,如果您单击模板中的{% if data %}
,只会返回True
,因此如果没有单击该按钮,它将不会尝试显示任何内容。