# Create the project directory # Create a virtual environment to isolate our package dependencies locally source env/bin/activate # On Windows use `env\Scripts\activate` # Install Django and Django REST framework into the virtual environment pip install djangorestframework # Set up a new project with a single application django-admin startproject tutorial . # Note the trailing '.' character django-admin startapp quickstart
項目布局應(yīng)如下所示:
./tutorial/quickstart/__init__.py ./tutorial/quickstart/admin.py ./tutorial/quickstart/apps.py ./tutorial/quickstart/migrations ./tutorial/quickstart/migrations/__init__.py ./tutorial/quickstart/models.py ./tutorial/quickstart/tests.py ./tutorial/quickstart/views.py
現(xiàn)在首次同步您的數(shù)據(jù)庫:
python manage.py migrate
我們還將創(chuàng)建一個名為admin password的初始用戶password123 。我們稍后將在我們的示例中對該用戶進行身份驗證。
python manage.py createsuperuser --email admin@example.com --username admin
在處理數(shù)據(jù)庫時python3報錯及解決辦法如下:
1、Error loading MySQLdb module
pip install pymysql
pymysql.install_as_MySQLdb()
2、AttributeError: 'str' object has no attribute 'decode'
File "/home/zwb/tutorial/env/lib64/python3.6/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query query = query.decode(errors='replace') AttributeError: 'str' object has no attribute 'decode'
錯誤代碼:query = query.encode(errors='replace')
解決方法:把decode改為encode即可。
3、django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解決辦法:找到Python安裝路勁下的Python36-32\Lib\site-packages\django\db\backends\mysql\base.py文件
將文件中的如下代碼注釋
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
|