在 Ubuntu 上手动部署在 Visual Studio 中创建的 Django 应用程序

Manually deploying on Ubuntu a Django application made in Visual Studio

我在Ubuntu服务器上安装了:

apt-get install python-pip apache2 libapache2-mod-wsgi
pip install virtualenv
mkdir -p /django/projects/prj01
cd /django/projects/prj01

创建虚拟环境:

virtualenv prj01env

激活虚拟环境:

source prj01env/bin/activate

在虚拟环境中安装django:

pip install django

我之前用winscp上传过项目文件夹 现在我将该项目复制到 /django/projects/prj01:

cp /home/myuser/prj01/ ./
cd prj01

更改权限:

chmod +x /django/projects/prj01
chown -R www-data:www-data /django/projects/

将文本文件从 windows 格式转换为 linux 格式:

dos2unix manage.py

最后我运行manage.py

./manage.py makemigrations

然后我收到以下错误消息:

(prj01env) root@RSR0008:/django/projects/prj01/prj01# ./manage.py createsuperuser
Traceback (most recent call last):
  File "./manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/management/base.py", line 342, in execute
    self.check()
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/django/projects/prj01/prj01env/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/django/projects/prj01/prj01/prj01/urls.py", line 6, in <module>
    from django.conf.urls import patterns, url
ImportError: cannot import name patterns

谁能告诉我这个程序有什么问题?

如果我直接从服务器创建一个空项目,一切正常,但如果我从我的 windows 客户端复制我的项目,它就无法工作。

当您安装 Django 时,您可能安装了已删除此方法的最新版本 (1.10) (patterns has been deprecated since 1.8),但您的项目文件来自旧版本的 Django。

而不是像这样使用 patterns

urlpatterns = patterns('',
        url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
        url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
        url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),   
)

您应该只使用一个简单的列表,如下所示:

urlpatterns = [
    url(r'^index/$', index_view, name="main-view"),
    ...
]

或者,您可以在 virtualenv 中安装旧版本的 Django,但我不推荐这样做,除非绝对必要。