1、创建Django项目2、views.py文件3、urls.py文件4、html页面5、运行结果
最近在学习Django后台开发,以一个简单的页面来记录一下知识点
1、创建Django项目项目结构如下
配置服务器
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render
# 计算器
def test_mycount(request):
if request.method == 'GET':
return render(request,'computer.html')
elif request.method == 'POST':
#处理计算
x = int(request.POST['x'])
y = int(request.POST['y'])
op = request.POST['op']
result = 0
if op == 'add':
result = x + y
elif op == 'sub':
result = x - y
elif op == 'mul':
result = x * y
elif op == 'div':
result = x / y
return render(request,'computer.html',locals()) #locals()表示将原本请求的数据重新返回
3、urls.py文件
from django.contrib import admin
from django.urls import path
from djangoProject import views
urlpatterns = [
path('admin/', admin.site.urls),
path('count',views.test_mycount)
]
4、html页面
计算器
5、运行结果



