我正在处理一个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);

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.

推荐答案

对代码进行以下更改 在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?

而在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'),
]

Javascript相关问答推荐

如何使用React渲染器放置根dis?

如何通过在提交时工作的函数显示dom元素?

类型脚本中只有字符串或数字键而不是符号键的对象

我应该绑定不影响状态的函数吗?'

切换时排序对象数组,切换不起作用

如何从调整大小/zoom 的SVG路径定义新的d属性?""

还原器未正确更新状态

检索相加到点的子项

当用户点击保存按钮时,如何实现任务的更改?

在css中放置所需 colored颜色 以填充图像的透明区域

<;img>;标记无法呈现图像

JS Animate()方法未按预期工作

将基元传递给THEN处理程序

不同表的条件API端点Reaction-redux

在HTML5画布上下文中使用putImageData时,重载解析失败

如何在每隔2分钟刷新OKTA令牌后停止页面刷新

如何在Java脚本中并行运行for或任意循环的每次迭代

是否有静态版本的`instanceof`?

如何缩小函数中联合返回类型的范围

正在发出错误的URL请求