达贾西。无法调用未定义的方法“...”。再次

Posted

技术标签:

【中文标题】达贾西。无法调用未定义的方法“...”。再次【英文标题】:Dajaxice. Cannot call method '...' of undefined. Again 【发布时间】:2012-11-14 09:37:00 【问题描述】:

我一直在尝试使用 dajaxice 创建一个最简单的应用程序。 我在这里阅读了有关此问题的所有主题,不仅在这里,多次重写所有代码,但仍然看不到问题所在。 最有趣的是,这些例子(几乎所有)都有效: https://github.com/jorgebastida/django-dajaxice/downloadsdajaxice-examples.tar.gz

但在我的项目中,我有这个:

Uncaught TypeError: Cannot call method 'sayhello' of undefined 

我的工具:

Windows 7 64 python-2.7.3 Django-1.4.2 django-dajaxice-0.2

项目结构:

BlocalProject/
    templates/
        template_1.html
    manage.py
    BlocalProject/
        ajapp/
            __init__.py
            ajview.py
        __init__.py
        settings.py
        urls.py
        views.py
        wsgi.py

urls.py:

from django.conf.urls.defaults import *
import settings
from dajaxice.core import dajaxice_autodiscover
dajaxice_autodiscover()


urlpatterns = patterns('',
    (r'^%s/' % (settings.DAJAXICE_MEDIA_PREFIX), include('dajaxice.urls')),
    (r'^$', 'BlocalProject.views.start_page'),
)

views.py:

from django.shortcuts import render

def start_page(request):
    return render(request,'template_1.html')

ajapp.py:

from django.utils import simplejson
from dajaxice.core import dajaxice_functions


def sayhello(request):
    return simplejson.dumps('message': 'Trololo!')

dajaxice_functions.register(sayhello)

template_1.html:

% load dajaxice_templatetags %
<html>
  % dajaxice_js_import %
  <script>
    function alertMessage(data)
      alert(data.message);
      return false;
    
  </script>

  <body>
    Some text
    <input type="button" value="Get!" onclick="Dajaxice.ajapp.sayhello(alertMessage);" />
  </body>
</html>

settings.py:

# Django settings for BlocalProject project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASE_ENGINE = ''           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = ''             # Or path to database file if using sqlite3.
DATABASE_USER = ''             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.

TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# URL prefix for admin media -- CSS, javascript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/'


# Make this unique, and don't share it with anybody.
SECRET_KEY = ')er9!%4v0=nmxd#2=j1*tlktmidq8aam2y)-%fjf6%^xp*5r)c'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    #'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

ROOT_URLCONF = 'BlocalProject.urls'

# Python dotted path to the WSGI application used by Django's runserver.
#WSGI_APPLICATION = 'BlocalProject.wsgi.application'

TEMPLATE_DIRS = (
    'D:/_/Site_test/Djpr/BlocalProject/templates',
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
                               "django.core.context_processors.debug",
                               "django.core.context_processors.i18n",
                               "django.core.context_processors.media",
                               "django.core.context_processors.static",
                               "django.core.context_processors.request",
                               "django.contrib.messages.context_processors.messages",)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'dajaxice',
    'BlocalProject.ajapp',  
)

DAJAXICE_MEDIA_PREFIX = "dajaxice"
DAJAXICE_DEBUG = True
DAJAXICE_JS_DOCSTRINGS = True
#DAJAXICE_NOTIFY_EXCEPTIONS = True

import logging
logging.basicConfig(level=logging.DEBUG)

【问题讨论】:

您选择 djaxice 是否有原因,而您可以使用简单的 javascript 库来做到这一点? 我的编程知识,尤其是js,不高。这只是一种爱好。我现在的目标 - 学习如何在 Django 中使用 Ajax。在我看来,dajaxice 最简单。即使有一个更简单的,我想了解这个例子有什么问题,以便更深入地理解一般概念 现在我试试这个github.com/jorgebastida/django-dajaxice/tree/master/examples 同样的结果。未捕获的类型错误:无法调用未定义的方法“你好” 为什么不使用装饰器@dajaxice_register 来注册sayhello 视图? django-dajaxice.readthedocs.org/en/latest/quickstart.html 这是蝙蝠的国家,我们不能停在这里。我停在达贾西斯。 jQuery 真的更简单 【参考方案1】:

这可能为时已晚,但是...

恕我直言,dajaxice_autodiscover() 实际上并没有得到你的方法。我记得另一个 ajax lib 也有类似的问题,我通过在models.py(或views.py)中添加一个导入解决了这个问题,该导入在应用程序启动时被导入。你提到的example 在 simple/views.py 中有一个导入:

# Create your views here.
from django.shortcuts import render

# THIS ONE!
from dajaxice.core import dajaxice_functions
# ---------


def index(request):

    return render(request, 'simple/index.html')

这看起来像是在初始化东西。我的方法是:

在您的ajapp.py 中添加几条打印语句,看看它们是否被打印出来。 在BlocalProject.ajapp 中创建空的models.pyviews.py(django 是否仍然使用models.py 来验证模块是否为django 应用程序?)

如果您的打印语句没有被触发,那么您需要找出原因。正如我所提到的,它可能就像在模型中导入你的 ajax 模块一样简单:)

【讨论】:

以上是关于达贾西。无法调用未定义的方法“...”。再次的主要内容,如果未能解决你的问题,请参考以下文章

LiveSearchGridPanel ExtJS 的 FiltersFeature 上的“无法调用未定义的方法 'getColumns'”

TypeError:无法调用未定义的方法“setSchema”

Cordova:无法调用未定义的方法“exec”

chrome.runtime.sendMessage 引发“未捕获的类型错误:无法调用未定义的方法‘sendMessage’”

未捕获的类型错误:无法调用 jquery jsonrpc 客户端中未定义的方法“设置”

chrome.tabs.create 给出错误 - “未捕获的类型错误:无法调用未定义的方法‘create’”