skip to Main Content

I’m working on a Django project and encountering an ImportError when trying to import a function from my app into the urls.py file. Despite the function being defined and the app added to INSTALLED_APPS, Django cannot seem to locate the function.
Project Structure:
myproject/
manage.py
dictionarapicol/
init.py
settings.py
urls.py
asgi.py
wsgi.py
statics/
find.js

myapp/
    migrations/
        __init__.py
    templates/
       base.htm
       index.html

    __init__.py
    text_processing.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
    urls.py

myapp/urls.py:

from django.urls import path
from myapp import views
from .text_processing import process_text

urlpatterns = [
    path('', views.home, name='index'),
    path('contact/', views.contact, name='contact'),
    path('stiri_apicole/', views.login, name='stiri_apicole'),
    path('text_processing/', process_text, name='text_processing'),
]

text_processing.py

from django.http import JsonResponse
import json
from nltk.stem.snowball import SnowballStemmer
import stanza
import spacy_stanza
from rowordnet import RoWordNet

# Load dictionary data
with open('staticdictionary.json', 'r', encoding='utf-8') as file:
    dictionary_data = json.load(file)

# Initialize NLTK, spaCy, Stanza, andin stall RoWordNet
stemmer = SnowballStemmer("romanian")
nlp = spacy_stanza.load_pipeline('ro')
import rowordnet as rwn
wn = RoWordNet()

def process_text(request):
    text = request.GET.get('text', '')
    stemmed_text = stemmer.stem(text)
    doc = nlp(text)
    lemmatized_text = ' '.join([token.lemma_ for token in doc])
    synset_ids = wn.synsets(literal=text)
    synsets = [wn.synset(synset_id).definition for synset_id in synset_ids]

    return JsonResponse({
        'stemmed_text': stemmed_text,
        'lemmatized_text': lemmatized_text,
        'RoWordNet_synsets': synsets
    })

views.py

from django.shortcuts import render

# Create your views here.
def home(request):
    return render(request, 'index.html')

def contact(request):
    return render(request, 'contact.html')

def login(request):
'stiri_apicole.html')

# views.py
from .text_processing import process_text

find.js

ocument.getElementById('searchInput').addEventListener('input', function (e) {
    const searchTerm = e.target.value;

    // Send the search term to Django backend for processing
    fetch(`text_processing.py/?text=${encodeURIComponent(searchTerm)}`)
        .then(response => response.json())
        .then(data => {
            // Use processed text (stemmed, lemmatized, etc.) from backend for Fuse.js search
            // Assuming the backend sends back a similar JSON structure
            const processedText = data.lemmatized_text; // Choose between stemmed_text or lemmatized_text
            const results = fuse.search(processedText);
            displayResults(results.map(result => result.item));
        })
        .catch(error => {
            console.error("Error processing text:", error);

enter image description here

I’ve verified that init.py exists in each directory, so Python should recognize them as packages.
I’ve tried importing process_text directly in views.py and then referencing it in urls.py, but the error persists.
The app is included in INSTALLED_APPS in settings.py.

2

Answers


  1. Try the following which might help

    1. Import using project directory name as package for import. Reduces bug and easier to migrate in future

    Example:

    from myapp.text_processing import process_text
    
    1. Always import packages at top, it is a standard practice
    Login or Signup to reply.
  2. Make the following changes to your code
    in the views.py

    from django.shortcuts import render
    
    def home(request):
        return render(request, 'index.html')
    
    def contact(request):
        return render(request, 'contact.html')
    
    def login(request):
        return render(request, 'stiri_apicole.html')
    
    # Notice that this line has been removed?
    

    And in the urls.py

    from django.urls import path
    from myapp import views, text_processing
    
    urlpatterns = [
        path('', views.home, name='index'),
        path('contact/', views.contact, name='contact'),
        path('stiri_apicole/', views.login, name='stiri_apicole'),
        path('text_processing/', text_processing.process_text, name='text_processing'),
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search