在REPL环境中使用django

在REPL环境中使用django

想在 repl 环境中测试一下 django 的 QueryDict 对象,先装一下这个包:

1
python3 -m pip install django --user

发现直接使用会报错:

1
2
3
4
5
6
7
8
9
10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lolimay/Library/Python/3.8/lib/python/site-packages/django/http/request.py", line 445, in __init__
self.encoding = encoding or settings.DEFAULT_CHARSET
File "/Users/lolimay/Library/Python/3.8/lib/python/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/Users/lolimay/Library/Python/3.8/lib/python/site-packages/django/conf/__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_CHARSET, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
>>>

说人话就是需要先调用 settings.configure() 配置设置项,需要像下面这样用:

1
2
3
4
5
6
7
8
9
10
11
12
I python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.conf import settings
>>> from django.http import QueryDict
>>> settings.configure()
>>> dict = QueryDict("周恩来")
>>> question = list(dict.keys())[0]
>>> print(question)
周恩来
>>>