The following article demonstrates How to Create a Django Web App to Use Parameters in View.py using a real-life example of a short-selling web app.
Basically, it is a trading strategy in which an investor borrows certain shares of a specific stock from a broker and sells them, expecting the price to go down. The investor then buys back the shares at a lower price, returns them to the broker, and pockets the difference as profit. Here we will use this application as an example for a Django web application that takes the parameters.
Let’s say we want to build a simple Django web app that allows users to enter a stock symbol and see its current price, as well as enter a short position on the stock. We can use the Yahoo Finance API to get the stock price data.
First, we’ll create a new Django app called “shortselling” using the command python manage.py startapp shortselling
. Inside the app directory, we’ll create a new file called views.py
.
In the views.py
file, we’ll define two functions: get_stock_price
and short_position
.
import requests
from django.shortcuts import render
def get_stock_price(request, symbol):
api_url = f'https://finance.yahoo.com/quote/{symbol}?p={symbol}&.tsrc=fin-srch'
response = requests.get(api_url)
price = response.json()['price']['regularMarketPrice']['raw']
context = {'symbol': symbol, 'price': price}
return render(request, 'shortselling/stock_price.html', context)
def short_position(request, symbol):
if request.method == 'POST':
# process short position form submission
pass
else:
context = {'symbol': symbol}
return render(request, 'shortselling/short_position.html', context)
The get_stock_price
function takes a symbol
parameter, which represents the stock symbol that the user entered. It uses the Yahoo Finance API to get the current price of the stock and returns a rendered template that displays the stock symbol and price.
The short_position
function also takes a symbol
parameter and is responsible for rendering the form that the user will use to enter their short position. If the user submits the form (using the HTTP POST method), the function will process the form data and execute the short position. For now, we’ll just leave the form processing code as a placeholder.
Next, we’ll create two HTML templates: stock_price.html
and short_position.html
. The stock_price.html
template will display the stock symbol and price, and the short_position.html
template will display the short position form.
<!-- stock_price.html -->
<h1>Current price for {{ symbol }} is ${{ price }}</h1>
<!-- short_position.html -->
<h1>Short {{ symbol }}</h1>
<form method="post">
<!-- short position form fields go here -->
</form>
Finally, we’ll add two URL patterns to the urls.py
file for the shortselling
app. The first URL pattern will map to the get_stock_price
function and will include the stock symbol as a parameter. The second URL pattern will map to the short_position
function and will also include the stock symbol as a parameter.
from django.urls import path
from . import views
urlpatterns = [
path('stock_price/<str:symbol>/', views.get_stock_price, name='stock_price'),
path('short_position/<str:symbol>/', views.short_position, name='short_position'),
]
Now, when a user visits the URL http://example.com/shortselling/stock_price/AAPL/
, Django will call the get_stock_price
function with the symbol
parameter set to “AAPL”. The function will use the Yahoo Finance API.
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP