我正在使用django rest框架,我使用pygments突出显示代码,但是我在pygmentify.py中遇到了语法错误

时间:2017-10-21 09:33:16

标签: django-rest-framework

异常类型:/ snippets /中的SyntaxError 异常值:语法无效(pygmentify.py,第13行)

环境:

请求方法:GET 请求网址:http://127.0.0.1:8000/snippets/

Django版本:1.11.2 Python版本:3.6.1

Installed Applications:   
['snippets.apps.SnippetsConfig',   
'django.contrib.admin',   
'django.contrib.auth',   
'django.contrib.contenttypes',   
'django.contrib.sessions',   
'django.contrib.messages',   
'django.contrib.staticfiles',   
'rest_framework',   
'django_pygments']   

' snippets.models':

from django.db import models    ``                    
from pygments.lexers import get_all_lexers, get_lexer_by_name                       
from pygments.formatters.html import HtmlFormatter 
from pygments import highlight  
from pygments.styles import get_all_styles

LEXERS = [item for item in get_all_lexers() if item[1]]    
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])    
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())    


class Snippet(models.Model):    
   created = models.DateTimeField(auto_now_add=True)    
   title = models.CharField(max_length=100, blank=True, default='')    
   code = models.TextField()      
   linenos = models.BooleanField(default=False)    
   language = models.CharField(choices=LANGUAGE_CHOICES, default='python', 
   max_length=100)    
   style = models.CharField(choices=STYLE_CHOICES, default='friendly', 
   max_length=100)    
   owner = models.ForeignKey('auth.User', related_name='snippets', 
   on_delete=models.CASCADE)    
   highlighted = models.TextField()    

   class Meta:    
      ordering = ('created',)    

   def save(self, *args, **kwargs):    
       lexer = get_lexer_by_name(self.language)    
       linenos = self.linenos and 'table' or False    
       options = self.title and {'title': self.title} or {}    
       formatter = HtmlFormatter(style=self.style, linenos=linenos,
                              full=True, **options)    
       self.highlighted = highlight(self.code, lexer, formatter)    
       super(Snippet, self).save(*args, **kwargs)  

2 个答案:

答案 0 :(得分:1)

这个包不支持python 3。 发布版本0.1尚不支持,但开发版0.3完成了它,但他们从未发布该版本

答案 1 :(得分:0)

您可以直接安装此github repo并且它可以正常工作。 https://github.com/odeoncg/django-pygments pip install git+https://github.com/odeoncg/django-pygments.git