我在我的应用myapp
中创建了一个名为urls.py
的Python文件,内容如下:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('predict/', views.predict),
path('predict/result', views.result)
]
接着,我按照指示修改了项目的urls.py
,现在它的内容是这样的:
from django import views
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('predict/', views.predict),
path('predict/result', views.predict)
]
在myapp
中的views.py
文件,我编写了如下的代码:
def result(request):
data = pd.read_csv(r"C:\Users\user\Desktop\MAX\diabetes.csv")
X = data.drop("Outcome", axis=1)
Y = data['Outcome']
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
model = LogisticRegression(max_iter=1000)
model.fit(X_train, Y_train)
val1 = float(request.GET['n1'])
val2 = float(request.GET['n2'])
val3 = float(request.GET['n3'])
val4 = float(request.GET['n4'])
val5 = float(request.GET['n5'])
val6 = float(request.GET['n6'])
val7 = float(request.GET['n7'])
val8 = float(request.GET['n8'])
prediction = model.predict([[val1, val2, val3, val4, val5, val6, val7, val8]])
result_text = ""
if prediction == [1]:
result_text = "阳性"
elif prediction == [0]:
result_text = '阴性'
return render(request, "predict.html", {"result": result_text})
并且,我已经在我的项目设置文件settings.py
的INSTALLED_APPS列表中添加了我的应用myapp
。