我正在处理一个Django项目,在try 将应用程序中的函数导入urls.py文件时遇到了ImportError.尽管定义了函数,并将应用程序添加到Installed_apps中,但Django似乎无法找到该函数. 项目 struct : 我的项目/ Manage.py 地卡尼考/ init.py Settings.py Urls.py Asgi.py Wsgi.py 静力学/ Find.js
myapp/
migrations/
__init__.py
templates/
base.htm
index.html
__init__.py
文本处理.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'),
]
文本处理.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('static\dictionary.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(`文本处理.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.