Dajaxice 0.5.1 - 似乎无法使教程工作

时间:2012-08-25 16:40:16

标签: ajax django dajaxice

我反复检查了我的东西,但我找不到问题。 这是我项目的布局:

  • 项目
    • allthings
      • 初始化的.py
      • admin.py
      • settings.py
      • models.py
      • views.py
      • tests.py
    • 静态
      • CSS
      • dajaxice
        • dajaxice.core.js
      • 图像
      • JS​​
    • 模板
      • testing.html
    • 项目
      • ajax.py
      • 初始化的.py
      • settings.py
      • urls.py
      • views.py
      • wsgi.py
    • manage.py

settings.py

DEBUG = True
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL =/static/

STATICFILES_DIRS = (
    'C:/Python27/djcode/project/static',
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'dajaxice.finders.DajaxiceFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'project.urls'
WSGI_APPLICATION = 'project.wsgi.application'

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'
)

TEMPLATE_DIRS = (
    "C:/Python27/djcode/project/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.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
'allthings',
'dajaxice',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
)

urls.py

from django.conf.urls import patterns, include, url
from project.views import hello, testing
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    ('^hello/$', hello),
    ('^testing/$', testing),
    url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)


urlpatterns += staticfiles_urlpatterns()

views.py

from django.shortcuts import render_to_response, HttpResponse
from django.template import RequestContext
from dajaxice.core import dajaxice_autodiscover 
dajaxice_autodiscover()

def hello(request):
    return HttpResponse("Hello world!")

def testing(request):
    return render_to_response('testing.html', context_instance = RequestContext(request))

ajax.py

from django.utils import simplejson
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def sayhello(request):
    return simplejson.dumps({'message':'Hello World'})

testing.html

<!DOCTYPE html/>
{% load dajaxice_templatetags %}
<html>
<head><title></title>
{% dajaxice_js_import %}
<script>
function my_callback(data){
    alert(data.message);
}
</script>
</head>
<body>
    This is to test stuff
    <input type="button" onclick="Dajaxice.project.sayhello(my_callback)" value="Get Message from Server"></input>
</body>
</html>

所有这一切都会产生一个发出警报的按钮。它应该非常简单,但我一无所获。我做错了什么?

1 个答案:

答案 0 :(得分:1)

由于dajaxice.core.js是非静态模板文件,DajaxFinder会找到它并将其“渲染”为临时文件夹作为静态文件。

因此,您的STATICFILES_DIRS设置不应包含dajaxice.core.js的文件路径,否则FileSystemFinder而不是DajaxFinder会找到该文件。

您可以运行findstatic检查是否找到了正确的内容:

manage.py findstatic dajaxice\dajaxice.core.js

或者也许:

manage.py findstatic dajaxice/dajaxice.core.js

输出结果应该在某个临时文件夹中(取决于您的操作系统),不应该在您应用的任何文件夹中。