preamble
Recently just started to learn how to combine python backend with html frontend, now write a blog to record it, I use the front and back end is not separated form.
Without further ado, let's implement a simple calculation function, the front-end input calculation data, the back-end calculation results, return results to the front-end display.
development tool
I went with pycharm pro because the community version can't create django programs.
2.Project Creation
Step 1: Open pycharm and create a django program
The blue circle is the customized name, click create in the lower right corner to create a django project
As shown below, the circled names correspond to the image above
Step 2: Write the back-end code
① Write the following code in the blog folder below:
from import render from calculate import jisuan # Create your views here. def calculate(request): return render(request, '') def show(request): x = ('x') y = ('y') result = jisuan(x, y) return render(request, '', {'result': result})
②Add the following two lines of code to the csdn folder below the bolded part
from import render from calculate import jisuan # Create your views here. def calculate(request): return render(request, '') def show(request): x = ('x') y = ('y') result = jisuan(x, y) return render(request, '', {'result': result})
③ Create a new file with the contents:
def jisuan(x, y): x = int(x) y = int(y) return (x+y)
Step 3: Write front-end code
① The page for data entry with the contents:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post" action="/getdata/"> {% csrf_token %} <input type="text" name="x" placeholder="Please enter x."/><br> <input type="text" name="y" placeholder="Please enter y."><br> <input type="submit" value="Perform calculations."> </form> </body> </html>
② The page returned by the result, the content of which is:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 style="color:blue">The results of the calculations are{{ result }}</h1> </body> </html>
Step 4: Start the background program
Type http://127.0.0.1:8000/jisuan into your browser's address bar
Enter to go to the data entry page
We enter x=10, y=20
Click on the Calculate button and the page jumps to display the results of the calculation.
Okay, a simple django project is done!
If you want to perform complex computational operations, you can write more complex functions in the
Source Code Resource Links:django learning, front-end and back-end are not separated
summarize
This article on python + html to achieve front and back data interaction interface display is introduced to this article, more related python + html front and back data interaction content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!