Solution 1 :

The error message tells you the problem. Django is trying to access the URL externalApp/ but can’t find that URL when “Using the URLconf defined in FBZ.urls”.

In your HTML form you’ve set the action to be /externalApp/.

In your urls.py you have the URL ShopBot/app.py pointing to you externalApp view.

I would change that last path in your urls.py to use the URL externalApp/ .

Other points:

  1. Read up on giving names to URLs. That way you can refer to them in your templates (like in your form’s action) using the url template tag and you won’t have this problem.

  2. It seems very weird to me that you’re calling an external python file. I’d have this code within the Django project itself, but maybe you have a good reason for this structure.

  3. In the view I would use a Django Form, which will make it easier to make the form more robust, and provide validation, which you’re not currently doing.

Good luck!

Problem :

I am trying to have a user input their information in the HTML form and then send it to my Python app that is located in the root of the directory and then run the python program. Below is my code for views.py and my html form.

My program I want to execute is app.py and would take the variables and use them in the program.

How do I pass a variable from the HTML to my Python app and then execute the program? I followed along with a youtube video to setup the externapApp function but I’m having problems executing app.py and making sure the variables are passed to the app.

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 (file I want to populate from HTML form, then execute)

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)

ursl.py

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)
]

The error I get is: Using the URLconf defined in FBZ.urls, Django tried these URL patterns, in this order:

admin/
ShopBot/
ShopBot/app.py
The current path, externalApp/, didn’t match any of these.

By