python 自定义类和视图

svujldwt  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(175)

我已经设置了以下自定义类,并试图将其内容输出到Django视图中,但我一直遇到各种属性和类型错误。有人能给我指个路吗?
自定义类

from pickle import OBJ
from urllib.request import HTTPPasswordMgr
from django.shortcuts import render
from django.http import request

class Bloodcancer():

    def __init__(self, my_symptoms=None, my_causes=None, my_treatments=None):
        self.symptoms = my_symptoms
        self.causes = my_causes
        self.treatments = my_treatments

class AcuteMyeloidLeukemia(Bloodcancer):

    def my_symptoms():
        symptomatology = {
            tuple({'a': "pale appearance",'b': "weakness or feeling tired", 'c': "shortness of breath", 'd': "easy brusing or bleeding",'e': "petechiae which are flat, pinpoint spots under the skin caused by bleeding", 'f': "loss of weight", 'g': "loss of appetite", 'h': "fever", 'i': "frequent minor infection"}
            )
            }
        print (symptomatology)

    def my_causes(self):
        causes = ['age', 'being male', 'genes', 'environment', 'other bone marrow diseases']
        return render(request, 'Healthcare/Conditions/Blood/Causes.html', {{causes}})

    def my_treatments(self):
        treatments = ['induction therapy', 'consolidation therapy', 'chemotherapy', 'allogenic stem cell transplantation', 'central nervous system treatment', 'treatment of older and frail patients with AML', 'Treatment of refractory or relapsed AML', 'Supportive treatment']
        return render(request, 'Healthcare/Conditions/Blood/Treatments.html', {{treatments}})

views.py

from django.shortcuts import render
from django.http import HttpResponseBadRequest, request
from django.urls import path
from Healthcare.Conditions.Blood.blood import AcuteMyeloidLeukemia as a

def symptoms_view(self):
    sample_set = a.my_symptoms()
    x = sample_set['b'].get()
    return render('Symptoms.html', {xx})

urls.py

from datetime import datetime
from django.urls import path
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView
from Healthcare import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('Healthcare/Conditions/Blood/Symptoms/', views.symptoms_view)
]

carvr3hs

carvr3hs1#

我浏览了你的代码,我发现,你有一些OOP问题:首先,你必须从AcuteMyeloidLeukemia类创建一个示例,然后你可以使用my_symptoms方法,并打印其中的值,另一件事你必须改变的是返回方法中的symptomology,如果不是,你不能打印值。而不是html文件,我使用HttpResponse来打印结果,结果是:虚弱或感到疲倦。自定义类:

from pickle import OBJ
from urllib.request import HTTPPasswordMgr
from django.shortcuts import render
from django.http import request

class Bloodcancer():
    def __init__(self, my_symptoms=None, my_causes=None, my_treatments=None):
        self.symptoms = my_symptoms
        self.causes = my_causes
        self.treatments = my_treatments

class AcuteMyeloidLeukemia(Bloodcancer):
    def my_symptoms(self):
        symptomatology = {
            'a': "pale appearance", 'b': "weakness or feeling tired", 'c': "shortness of breath", 'd': "easy brusing or bleeding",'e': "petechiae which are flat, pinpoint spots under the skin caused by bleeding", 'f': "loss of weight", 'g': "loss of appetite", 'h': "fever", 'i': "frequent minor infection"
            }
        return symptomatology

    def my_causes(self):
        causes = ['age', 'being male', 'genes', 'environment', 'other bone marrow diseases']
        return render(request, 'Healthcare/Conditions/Blood/Causes.html', {{causes}})

    def my_treatments(self):
        treatments = ['induction therapy', 'consolidation therapy', 'chemotherapy', 'allogenic stem cell transplantation', 'central nervous system treatment', 'treatment of older and frail patients with AML', 'Treatment of refractory or relapsed AML', 'Supportive treatment']
        return render(request, 'Healthcare/Conditions/Blood/Treatments.html', {{treatments}})

view.py :

from django.shortcuts import render
from django.http import HttpResponseBadRequest, request
from django.urls import path
from django.http import HttpResponse
from .custom import AcuteMyeloidLeukemia as a

def symptoms_view(self):
    sample_set = a()
    x = sample_set.my_symptoms()["b"]
    # return render('Symptoms.html', {"x": x})
    return HttpResponse(x)

urls.py :

from datetime import datetime
from django.urls import path
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView
from . import views

urlpatterns = [
    path('Healthcare/Conditions/Blood/Symptoms/', views.symptoms_view)
]

相关问题