django语法无效(pagination_tags.py,第225行)

时间:2017-06-13 09:14:48

标签: python django django-templates django-pagination

Environment:
Request Method: GET
Request URL: http://www.qiuqingyu.cn/cnki_spider/todolist/
Django Version: 1.9
Python Version: 3.6.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'pagination',
 'myapp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'myapp.middleware.CheckSoureMiddware']
> 
> Traceback:
File "/root/anaconda3/lib/python3.6/site-packages/django/template/utils.py" in __getitem__
  86.             return self._engines[alias]
During handling of the above exception ('django'), another exception occurred:
File "/root/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)
File "/root/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./myapp/views.py" in todolist
  29.     return render(request, 'todolist.html', context=locals())
File "/root/anaconda3/lib/python3.6/site-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/root/anaconda3/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  96.             template = get_template(template_name, using=using)
File "/root/anaconda3/lib/python3.6/site-packages/django/template/loader.py" in get_template
  26.     engines = _engine_list(using)
File "/root/anaconda3/lib/python3.6/site-packages/django/template/loader.py" in _engine_list
  143.     return engines.all() if using is None else [engines[using]]
File "/root/anaconda3/lib/python3.6/site-packages/django/template/utils.py" in all
  110.         return [self[alias] for alias in self]
File "/root/anaconda3/lib/python3.6/site-packages/django/template/utils.py" in <listcomp>
  110.         return [self[alias] for alias in self]
File "/root/anaconda3/lib/python3.6/site-packages/django/template/utils.py" in __getitem__
  101.             engine = engine_cls(params)
File "/root/anaconda3/lib/python3.6/site-packages/django/template/backends/django.py" in __init__
  31.         options['libraries'] = self.get_templatetag_libraries(libraries)
File "/root/anaconda3/lib/python3.6/site-packages/django/template/backends/django.py" in get_templatetag_libraries
  49.         libraries = get_installed_libraries()
File "/root/anaconda3/lib/python3.6/site-packages/django/template/backends/django.py" in get_installed_libraries
  131.             for name in get_package_libraries(pkg):
File "/root/anaconda3/lib/python3.6/site-packages/django/template/backends/django.py" in get_package_libraries
  144.             module = import_module(entry[1])
File "/root/anaconda3/lib/python3.6/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)
Exception Type: SyntaxError at /cnki_spider/todolist/
Exception Value: invalid syntax (pagination_tags.py, line 225)

有人遇到过这个问题吗?我该如何解决? 我在CentOS上遇到过这个,django是1.9,我用的是uwsgi python是3.6 有没有人遇到过这个问题?我该如何解决? 我在CentOS上遇到过这个,django是1.9,我用的是uwsgi python是3.6 谢谢!

views.py在这里:

    import os

from django.http import HttpResponse, HttpResponseRedirect
from django.http import StreamingHttpResponse
from django.shortcuts import render, render_to_response
from myapp.forms import TodolistaddForm
from myapp.models import Todolist
import datetime
from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger
import datetime
from .tasks import cnki_spider


# Create your views here.
def todolist(request):
    todolist_lists = Todolist.objects.order_by("-id")

    limit = 10  
    paginator = Paginator(todolist_lists, limit)
    page = request.GET.get('page')
    try:
        todolist_lists = paginator.page(page) 
    except PageNotAnInteger:  
        todolist_lists = paginator.page(1) 
    except EmptyPage:  
        todolist_lists = paginator.page(paginator.num_pages) 
    return render(request, 'todolist.html', locals()) #error!!!!!!!!


def add_todolist(request):
    if request.method == "POST":
        body = request.POST['body']
        add_date = datetime.datetime.now()
        tag_type = 0
        print(add_date)
        print(body)
        #add.delay(5,6)
        cnki_spider.delay(body)
        if body:
            Todolist.objects.create(
                body=body,
                add_date=add_date,
                tag_type=tag_type,
            )
        return HttpResponseRedirect('/todolist/')
    else:
        return HttpResponseRedirect('/todolist/')


def del_todolist(request):
    rowid = request.GET['rowid']
    body = Todolist.objects.filter(id=rowid)[0].body
    Todolist.objects.filter(id=rowid).delete()
    the_file_name = "data_out_" + body + ".xls"
    os.remove(the_file_name)
    print(the_file_name)
    os.remove('data-detail-'+ body +'.txt')
    return HttpResponse('{"code":0}')


def edit_todolist(request):
    rowid = request.POST['rowid']
    todolist_id = Todolist.objects.get(id=rowid)
    todolist_id.body = request.POST['body']
    todolist_id.save()
    return HttpResponseRedirect('/todolist/')

def download_todolist(request):
    rowid = request.POST['rowid']
    todolist_id = Todolist.objects.get(id=rowid)
    todolist_id.body = request.POST['body']
    def readFile(fn, buf_size=262144):#大文件下载,设定缓存大小
        f = open(fn, "rb")
        while True:#循环读取
            c = f.read(buf_size)
            if c:
                yield c
            else:
                break
        f.close()
    the_file_name = "data_out_"+todolist_id.body+".xls"
    response = StreamingHttpResponse(readFile(the_file_name))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)

    return response


def chk_todolist(request):
    rowid = request.GET['rowid']
    todolist_id = Todolist.objects.get(id=rowid)
    todolist_id.tag_type = request.GET['status']
    todolist_id.save()
    return HttpResponse('{"code":0}')

2 个答案:

答案 0 :(得分:0)

最新的django-pagination 1.0.7于2010-05-06发布。 (另外Django 1.9不再支持我建议你升级到更新的版本或使用LTS版本。)但Django 1.9有自己内置的分页https://docs.djangoproject.com/en/1.9/topics/pagination/和Django 1.8 LTS和Django 1.11。我建议你使用内置的django.core.paginator并停止使用2010版的django-pagination。

答案 1 :(得分:0)

pagination的已安装应用中删除settings.py

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pagination',       //remove this line 
    'phone_field',
]