简体   繁体   English

如何使用 Django 项目设置 SQLite?

[英]How do I set up SQLite with a Django project?

I have read the Django documentation (For reference, https://docs.djangoproject.com/en/1.3/intro/tutorial01/ ), but I'm still having issues setting up a Django project to work with a database.我已阅读 Django 文档(供参考, https://docs.djangoproject.com/en/1.3/intro/tutorial01/ ),但在设置 Django 项目以使用数据库时仍然遇到问题。 Because I'm new to web development, I figured I would start with SQLite for development since I'm not putting anything into production yet and don't have much experience with databases.因为我是 Web 开发的新手,所以我想我会从 SQLite 开始进行开发,因为我还没有将任何东西投入生产,并且对数据库没有太多经验。 (I would be fine starting with MySQL or PostgreSQL though.) (不过,我可以从 MySQL 或 PostgreSQL 开始。)

In my Django project folder, I know I need to edit the settings.py folder to specify the database and a few other things.在我的 Django 项目文件夹中,我知道我需要编辑 settings.py 文件夹来指定数据库和其他一些内容。 The problem is that when I run python manage.py syncdb in terminal, it fails with this message:问题是当我在终端中运行python manage.py syncdb时,它失败并显示以下消息:

django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 
or sqlite3 modules (tried in that order): No module named _sqlite3

I am assuming part of my problem is that I installed Python (2.7) from source code (explained in-depth in How would I build python myself from source code on Ubuntu? ), yet my terminal is probably defaulting to the Python installed by default on Ubuntu.我假设我的部分问题是我从源代码安装了 Python (2.7)(在我如何从 Ubuntu 上的源代码自己构建 python中进行了深入解释 ),但我的终端可能默认使用默认安装的 Python在 Ubuntu 上。 Or I suppose it could be that the Python (2.7) I built from source code somehow wasn't linked to SQLite.或者我想这可能是我从源代码构建的 Python (2.7) 以某种方式没有链接到 SQLite。 Or I suppose it could be that I need to specify an SQLite file in settings.py under the NAME item in DATABASES, although I'm pretty sure that isn't necessary for SQLite.或者我想可能是我需要在 DATABASES 的 NAME 项下的 settings.py 中指定一个 SQLite 文件,尽管我很确定这对于 SQLite 不是必需的。

Can anyone identify the culprit?谁能认出罪魁祸首? How would I remedy this as a long-term solution (ie Is there any way I can remove the ambiguity of having two co-existing Python installations on Ubuntu?) Most importantly, what do I need to do to set up a working database?作为长期解决方案,我将如何解决这个问题(即有什么方法可以消除在 Ubuntu 上同时安装两个 Python 的歧义?)最重要的是,我需要做什么来设置一个工作数据库?

Do a 'sudo apt-get install libsqlite3-dev' and then recompile Python 2.7.执行“sudo apt-get install libsqlite3-dev”,然后重新编译 Python 2.7。

In the long term, to reduce ambiguity of co-existing Python installations and packages, I highly recommend installing pip, virtualenv, and virtualenvwrapper.从长远来看,为了减少共存的 Python 安装和包的歧义,我强烈建议安装 pip、virtualenv 和 virtualenvwrapper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM