我已经使用 django 几个月了,通常一个简单的谷歌搜索可以解决我的任何问题(通常带有指向 SO 的链接)。我终于遇到了一些我找不到解决方案并且完全莫名其妙的东西。
一般错误信息:
请求方法:GET 请求 URL:localhost:8000/switch/1 Django 版本:1.4 异常类型:SyntaxError 异常值:
无效语法(第 1 行)异常位置:解析中的 /usr/lib/python2.7/ast.py,第 37 行 Python 可执行文件:/usr/bin/python Python 版本:2.7.3
相关查看代码
interfaces = Interface.objects.filter(switch=switch)
interfaces = sorted(interfaces, key=lambda x: x.name)
interfaces = sorted(interfaces, key=lambda x: len(x.name))
interface_table = InterfaceTable(interfaces)
相关型号代码
class Interface(models.Model):
class Meta:
app_label = "Network_Builder"
name = models.CharField(max_length=20)
fullname = models.CharField(max_length=30)
switch = models.ForeignKey("Switch", related_name='switch')
physical_state = models.CharField(max_length=30, blank=True, null=True)
ip_address = models.CharField(max_length=20, blank=True, null=True)
administrator_notes = models.TextField(max_length=200, blank=True)
access_vlan = models.ForeignKey(Vlan, related_name='access_vlan', blank=True, null=True)
native_vlan = models.ForeignKey(Vlan, related_name='native_vlan', blank=True, null=True)
admin_mode = models.CharField(max_length=20, blank=True, null=True)
operational_mode = models.CharField(max_length=20, blank=True, null=True)
admin_encapsulation = models.CharField(max_length=20, blank=True, null=True)
operational_encapsulation = models.CharField(max_length=20, blank=True, null=True)
switchport_state = models.CharField(max_length=20, blank=True, null=True)
negotiation = models.BooleanField(blank=True)
native_vlan_tagging = models.BooleanField(blank=True)
allowed_vlans_list = ListField(blank=True, null=True)
allowed_vlans_string = models.CharField(max_length=100, blank=True, null=True)
active_vlans = models.ManyToManyField(Vlan, related_name='active_vlans', blank=True, null=True)
unpruned_vlans = models.ManyToManyField(Vlan, related_name='unpruned_vlans', blank=True, null=True)
protected = models.BooleanField()
unicast_blocked = models.BooleanField()
multicast_blocked = models.BooleanField()
堆栈跟踪:
Environment:
Request Method: GET
Request URL: http://localhost:8000/switch/2
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'Network_Builder',
'django.contrib.admin',
'django_tables2')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/home/ellis/Projects/Network_Builder/Network_Builder/views/switch/info_views.py" in switch_page
46. interfaces = sorted(interfaces, key=lambda x: x.name)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __len__
87. self._result_cache.extend(self._iter)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in iterator
301. obj = model(*row[index_start:aggregate_start])
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in __init__
300. setattr(self, field.attname, val)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/subclassing.py" in __set__
34. obj.__dict__[self.field.name] = self.field.to_python(value)
File "/home/ellis/Projects/Network_Builder/Network_Builder/models/model_fields.py" in to_python
18. return ast.literal_eval(value)
File "/usr/lib/python2.7/ast.py" in literal_eval
49. node_or_string = parse(node_or_string, mode='eval')
File "/usr/lib/python2.7/ast.py" in parse
37. return compile(source, filename, mode, PyCF_ONLY_AST)
Exception Type: SyntaxError at /switch/2
Exception Value: invalid syntax (<unknown>, line 1)
在这一行:
interfaces = Interface.objects.filter(switch=switch)
interfaces 填充了一个查询集(可通过 pdb 观察),但是一旦我尝试对变量执行任何操作(包括上面代码中所示的打印、迭代或排序),就会引发错误。完全令人困惑的部分是,如果我将调用放入 try:except: 中,则变量“interfaces”完全可以使用。
有没有人以前见过这样的事情,或者对如何找到解决方案有任何见解?