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);
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
Try the following which might help
Example:
Make the following changes to your code
in the
views.py
And in the
urls.py