Django Ajax请求 - 500内部服务器错误,“匹配查询不存在”。错误

时间:2015-09-25 17:17:31

标签: python ajax django

我在构建基于Django的站点时遇到了问题; 我需要在开始时使用Ajax运行一些模板更新:

 $(document).ready(function () {
        var trees = ['1','2','3','4','5','6']
        for (i = 0; i < trees.length; i++) {
            $.ajax({
                'url': '/structure/showtrees',
                'data': {
                    number:trees[i]
                },
                'type': 'GET',
                'success': function (data) {
                    $("#phylo"+i).html(data);
                }
            });
        };
    });

这是通过urls.py重定向的,如下所示: 来自django.conf.urls导入模式,url

from structure import views
from django.conf import settings

urlpatterns = patterns('',
    ...,
    ...,
url(r'^showtrees', views.RenderTrees, name='render') 
)

在“views.py”中是目标函数:

def RenderTrees(request):
print(request.session)
number = request.session.GET['number']
tree = open('static/home/images/00'+number+'_tree.xml').read()

return render(request, 'tree'+number+'.html', {'tree':tree,'num':number})

我有“打印(会话)”作为标记,但它永远不会显示。 在页面加载期间,我得到:

GET http://localhost:8000/structure/showtrees?number=1 500 INTERNAL SERVER ERROR        464ms

响应是

DoesNotExist at /structure/showtrees
Structure matching query does not exist.

我正在搜索和搜索,但我找不到任何答案。

编辑: 完全追溯到这里:

    DoesNotExist at /structure/showtrees
Structure matching query does not exist.

Request Method: GET
Request URL: http://localhost:8000/structure/showtrees?number=1
Django Version: 1.8.4
Python Executable: /usr/bin/python3
Python Version: 3.4.3
Python Path: ['/vagrant/protwis', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '
/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages'
]
Server time: Fri, 25 Sep 2015 19:05:08 +0200
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'debug_toolbar',
 'rest_framework',
 'rest_framework_swagger',
 'django_nvd3',
 'common',
 'api',
 'documentation',
 'news',
 'pages',
 'home',
 'protein',
 'family',
 'residue',
 'alignment',
 'similaritysearch',
 'similaritymatrix',
 'structure',
 'ligand',
 'interaction',
 'mutation',
 'phylogenetic_trees',
 'sitesearch',
 'build_gpcr')
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/vagrant/protwis/structure/views.py" in StructureDetails
  64.     crystal = Structure.objects.get(pdb_code__index=pdbname)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py" in get
  334.                 self.model._meta.object_name

Exception Type: DoesNotExist at /structure/showtrees
Exception Value: Structure matching query does not exist.
Request information:
GET:
number = '1'

POST: No POST data

FILES: No FILES data

COOKIES:
djdt = 'hide'
csrftoken = 'fH9XuekPOAU6drwD8tHHGBecE7YxMqms'
sessionid = 'lf0wjer4wyyp98ez9y5ngbcawb1xxbak'

2 个答案:

答案 0 :(得分:1)

您调用的网址是/structure/showtrees,但在您提供的urls.py文件中,您只定义了/showtrees。这导致了你的错误。要解决此问题,请将JS调用更改为/showtrees或将urls.py文件插入名为structure 的新应用程序中
看来你在某处使用了Structure.objects.get,Django提出了DoesNotExists exception。检查数据库中是否存在结构对象。
你的问题就在这一行:

crystal = Structure.objects.get(pdb_code__index=pdbname)

检查该查询集上的过滤器。

答案 1 :(得分:0)

好的,我在我的代码中发现了问题,urls.py中的一个url有一个正则表达式,它抓住了传递的任何字符串,因而不是调用它:

url(r'^showtrees', views.RenderTrees, name='render')

它调用了另一个函数。谢谢你的帮助!