views.py中的Django python语法错误

时间:2014-06-27 09:07:06

标签: python django postgresql

我最近在Django中使用我的Web应用程序遇到了这个问题:

SyntaxError at /
invalid syntax (views.py, line 98)
Request Method: GET
Request URL:    http://craft.irisa.fr/
Django Version: 1.6.5
Exception Type: SyntaxError
Exception Value:    
invalid syntax (views.py, line 98)
Exception Location: /usr/lib/python3.2/importlib/_bootstrap.py in get_code, line 413
Python Executable:  /root/CRAFT/trunk/sources/env/bin/python
Python Version: 3.2.3

我的views.py文件代码的一部分:

def login_view(request):
""" Logs a user into the application """

# Redirect if user is already connected
if request.user.is_authenticated():
    return HttpResponseRedirect('/')

if request.method == 'POST':
    # Get the values from the sent form
    login_form = AuthenticationForm(data=request.POST)

    # Log the user in if the form is valid
    if login_form.is_valid():
        login(request, login_form.get_user())

#line 98 is here : 
        messages.add_message(request, messages.INFO, u'Welcome back '+ request.user.username +'!')

        next = request.POST.get('next','/')
        return HttpResponseRedirect(next)
else:
    next = request.GET.get('next','/')
    # Create the form
    login_form = AuthenticationForm()

# Display the page
return render(request, 'taskHandler/login.html', locals())

正如您所看到的,此行中没有语法错误且我的代码有效,我测试了它。

但我做的新事物是:我已将django sgbd从sqlite3更改为postgresql。

当我做这些修改时出现了这些麻烦,所以我想理解为什么python会引发这样的错误!我尝试使用manage.py shell在我的数据库中添加数据,我没有问题!

感谢您的帮助!

艾万

编辑:完整的错误追溯

环境:

Request Method: GET
Request URL: http://craft.irisa.fr/

Django Version: 1.6.5
Python Version: 3.2.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'taskHandler',
 'widget_tweaks',
 'south')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/core/handlers/base.py" in get_response
  99.                 resolver_match = resolver.resolve(request.path_info)
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/core/urlresolvers.py" in resolve
  339.                     sub_match = pattern.resolve(new_path)
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/core/urlresolvers.py" in resolve
  339.                     sub_match = pattern.resolve(new_path)
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/core/urlresolvers.py" in resolve
  223.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/core/urlresolvers.py" in callback
  230.         self._callback = get_callable(self._callback_str)
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/utils/functional.py" in wrapper
  32.         result = func(*args)
File "/root/CRAFT/trunk/sources/env/lib/python3.2/site-packages/django/core/urlresolvers.py" in get_callable
  97.             mod = import_module(mod_name)
File "/usr/lib/python3.2/importlib/__init__.py" in import_module
  124.     return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in _gcd_import
  821.                     loader.load_module(name)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in load_module
  436.         return self._load_module(fullname)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in decorated
  141.             return fxn(self, module, *args, **kwargs)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in _load_module
  330.         code_object = self.get_code(name)
File "/usr/lib/python3.2/importlib/_bootstrap.py" in get_code
  413.                                 dont_inherit=True)

Exception Type: SyntaxError at /
Exception Value: invalid syntax (views.py, line 98)

2 个答案:

答案 0 :(得分:1)

问题来自python版本,

在python 3.2中,字符串前面的'u'编码字符无法使用!

答案 1 :(得分:0)

u是Python 2.x中使用的编码字符。

对于Python 3.2,您可以使用str()或完全删除u。