我试图让用户在HTML表单中输入他们的信息,然后将其发送到位于目录根目录中的Python应用程序,然后运行Python程序。下面是我的views.py代码和我的html表单
我要执行的程序是app.py,它将获取变量并在程序中使用它们
如何将一个变量从HTML传递到Python应用程序,然后执行该程序?我跟随youtube视频设置externapApp函数,但在执行app.py和确保将变量传递给应用程序时遇到问题
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Flatbush Zombies Shop Bot</h1>
<p class="lead">This is a bot written in Python that will shop <a href="https://thegloriousdead.com/">this</a> website for you. This website sells out of its products right away when they are released so this bot can purchase your desired items in an instant!</p>
</div>
</div>
<div class="form">
<form action="/externalApp/" method="POST">
{% csrf_token %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">First name</span>
</div>
<input id="first_name" type="text" name="user_first_name" value="{{ user_first_name }}">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Last name</span>
</div>
<input id="last_name" type="text" name="user_last_name" value="{{ user_last_name }}">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Email</span>
</div>
<input id="email" type="text" name="user_email" value="{{ user_email }}">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Phone</span>
</div>
<input id="phone" type="text" name="user_phone" value="{{ user_phone }}">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Address</span>
</div>
<input id="address" type="text" name="user_address" value="{{ user_address }}">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">City</span>
</div>
<input id="city" type="text" name="user_city" value="{{ user_city }}">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Zipcode</span>
</div>
<input id="zipcode" type="text" name="user_zipcode" value="{{ user_zipcode }}">
</div>
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>
views.py
from django.shortcuts import render
from django.http import HttpResponse
import requests
from subprocess import run,PIPE
import sys
def shopbotView(request):
context = {}
return render(request, "index.html", context)
def externalApp(request):
user_first_name = request.POST.get('user_first_name')
user_last_name = request.POST.get('user_last_name')
user_email = request.POST.get('user_email')
user_phone = request.POST.get('user_phone')
user_address = request.POST.get('user_address')
user_zipcode = request.POST.get('user_zipcode')
user_city = request.POST.get('user_city')
output= run([sys.executable, '//Users//will/Documents//GitHub//FBZbot//env//FBZ//ShopBot//app.py',user_first_name,user_last_name,user_email,user_phone,user_address,user_zipcode,user_city],shell=False,stdout=PIPE)
print(output)
return render(request, 'index.html',{'data1':output})
app.py(我想从HTML表单填充的文件,然后执行)
import time
from selenium import webdriver
from views.py import externalApp
# Init broswer
driver = webdriver.Chrome("/Users/will/Documents/GitHub/FBZbot/chromedriver")
driver.get("https://thegloriousdead.com/")
driver.implicitly_wait(10)
# Find product and select size
item = driver.find_element_by_xpath('//*[@id="ProductImage-14397485580370"]')
item.click()
# Add item to cart
add_to_cart = driver.find_element_by_xpath('//*[@id="product_form_4512114081874"]/div[5]/button')
add_to_cart.click()
# Checkout with cart
checkout = driver.find_element_by_xpath('//*[@id="PageContainer"]/main/section/form/div/div/input[2]')
checkout.click()
# Enter checkout information
email = driver.find_element_by_xpath('//*[@id="checkout_email"]')
first_name = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_first_name"]')
last_name = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_last_name"]')
address = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_address1"]')
city = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_city"]')
zipcode = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_zip"]')
phone = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_phone"]')
# Fill out checkout data
email.send_keys(user_email)
time.sleep(1)
first_name.send_keys(user_first_name)
last_name.send_keys(user_last_name)
address.send_keys(user_address)
city.send_keys(user_city)
zipcode.send_keys(user_zipcode)
phone.send_keys(user_phone)
乌拉尔·皮伊
from django.contrib import admin
from django.urls import path
from ShopBot.views import shopbotView
from ShopBot.views import externalApp
urlpatterns = [
path('admin/', admin.site.urls),
path('ShopBot/', shopbotView),
path('ShopBot/app.py', externalApp)
]
我得到的错误是:Django使用FBZ.URL中定义的URLconf尝试了以下URL模式,顺序如下:
管理员/ 购物机器人/ ShopBot/app.py 当前路径externalApp/与其中任何路径都不匹配
错误消息会告诉您问题所在。Django正在尝试访问URL
externalApp/
,但在“使用FBZ.URL中定义的URLconf”时找不到该URL在HTML表单中,您已将
action
设置为/externalApp/
在
urls.py
中,您有指向您的ShopBot/app.py
视图的URL我会将
urls.py
中的最后一个path
更改为使用URLexternalApp/
其他要点:
仔细阅读giving names to URLs。这样,您可以使用
url
模板标记在模板中引用它们(如在表单的action
),而不会出现此问题对我来说,调用外部python文件似乎很奇怪。我会在Django项目本身中包含这段代码,但也许您有一个很好的理由来解释这种结构
在视图中,我将使用Django Form,这将使表单更加健壮,并提供验证,而您目前没有这样做
祝你好运
相关问题 更多 >
编程相关推荐