4

我是 python 和 django 的新手,最近遇到了一些问题。Django-admin.py runserver 不起作用(ubuntu 12.04)。

终端错误代码:

    linas@ubuntu:~$ django-admin.py runserver
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 8, in <module>
    from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
  File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 26, in <module>
    from django.views import static
  File "/usr/local/lib/python2.7/dist-packages/django/views/static.py", line 95, in <module>
    template_translatable = ugettext_noop(u"Index of %(directory)s")
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 75, in gettext_noop
    return _trans.gettext_noop(message)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 48, in __getattr__
    if settings.USE_I18N:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

谢谢你的帮助。

编辑:这是我使用 ./manage.py runserver 时得到的

linas@ubuntu:~/Dokumentai/python/FirstBlog$ ./manage.py runserver
Validating models...

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1148c90>>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 103, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
    self.cursor()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 387, in _cursor
    self.connection = Database.connect(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1049, "Unknown database 'FirstBlog'")
4

3 回答 3

7

manage.py从您的项目目录中使用

cd project
./manage.py runserver
于 2012-05-21T18:13:08.283 回答
0

你能发布你的模型吗?您还创建了数据库吗?

跑:

python manage.py syncdb
于 2012-05-21T19:46:38.587 回答
0

首先(我希望你已经这样做了)用你的项目名称创建一个目录,然后启动你的 django 项目执行命令: python django-admin.py startproject projname (如果它没有正确执行删除关键字“python”并尝试)现在从同一目录运行 python manage.py runserver 。如果您已正确安装,您将获得: Validating models 。. 发现 0 个错误

于 2012-05-22T10:48:33.747 回答