Home > database >  AttributeError at type object 'Services' has no attribute 'app'
AttributeError at type object 'Services' has no attribute 'app'

Time:01-30

I newly started to develop Django. I want to connect my python algorithm and Django web ui so I tried to connect both of them in views.py but I encountered a problem.

It says AttributeError at type object 'Services' has no attribute 'app' but I declared it in the queryOnMetadataModel.init. I don't know what is wrong. Could someone help me?

These are my code snippets;

  • queryOnMetadataModel.py
from Neo4jConnection import App   
import json

class Services:
    def __init__(self):
        uri = "neo4j s://a50f760a.databases.neo4j.io:7687"
        user = "neo4j"
        password = "password"
        self.app = App(uri, user, password)
    
    def find_node_type(self,nodeName):
        node_type = self.app.find_node_type(nodeName)
        self.app.close()
        return node_type
  • views.py
from django.shortcuts import render
from BitirmeTeziSourceCode.queryOnMetadataModel import Services

import sys
sys.path.append(".")

# Create your views here.

def home(request):

    data = Services.find_node_type(Services , 'Region')
    
    nodes = {
        "nodes" : data
    }
    
    return render(request , "index.html" , nodes)

urls.py


from django.urls import path
from . import views

urlpatterns = [
    path("" , views.home)
]

Error Image

I want to access output of Services.find_node_type(Services , 'Region') from index.html

CodePudding user response:

The error message you're seeing, "Attribute 'app' of 'Services' objects is not writable," is occurring because you are trying to set the value of an attribute on the class 'Services' rather than an instance of that class. In the line data = Services.find_node_type(Services , 'Region') you are calling the find_node_type method on the class 'Services' instead of an instance of that class.

To fix this, you should first create an instance of the 'Services' class, and then call the find_node_type method on that instance. You can do this by creating a variable services = Services() before calling the method, and then calling data = services.find_node_type('Region').

Here is the updated views.py code:

from django.shortcuts import render
from BitirmeTeziSourceCode.queryOnMetadataModel import Services

import sys
sys.path.append(".")

def home(request):
    services = Services()
    data = services.find_node_type('Region')
    
    nodes = {
        "nodes" : data
    }
    
    return render(request , "index.html" , nodes)

Also, you could add some error handling in your function if you want to handle any exceptions that might occur while running the function.

CodePudding user response:

You need to make instance first instead of directly using class so:

def home(request):
    instance=Services()
    data = instance.find_node_type('Region')
    
    nodes = {
        "nodes" : data
    }
    
    return render(request , "index.html" , nodes)

Note: Also it is better to write classes in singular case so it is better to write Service not Services.

  • Related