代码概述:令牌是相同的,但是在加密和解密之间,加密的对象被存储到模块级字典中 - 尽管如此,加密令牌不会改变。
为什么这不起作用?我想加密对象背后有一些东西使它独一无二,但我认为它所需要的只是解密工作的正确密钥。
这是最少的相关代码:
import sys
from cryptography.fernet import Fernet
import json
import os
key = Fernet.generate_key()
f = Fernet(key)
with open("storage.json", "a+") as file:
if os.stat("storage.json").st_size == 0:
file.write("{}")
file.seek(0)
storage = json.load(file)
def write(data):
with open("storage.json", "w") as file:
json.dump(data, file)
def encrypt(pw):
token = f.encrypt(bytes(pw, "utf-8"))
return token
def decrypt(token):
return f.decrypt(token)
if len(sys.argv) == 1:
to_encrypt = input("A key to encrypt: ")
storage[to_encrypt] = encrypt(to_encrypt).decode("utf-8")
print("encrypted:", storage[to_encrypt])
# print("storage:", storage)
try:
write(storage)
except Exception …Run Code Online (Sandbox Code Playgroud) 根据Django docs的说法,ChoiceField接受两个元组的迭代,"或者是一个返回这样一个可迭代的可调用的",用作字段的选择.
我ChoiceFields在我的表格中定义了:
class PairRequestForm(forms.Form):
favorite_choices = forms.ChoiceField(choices=[], widget=RadioSelect, required=False)
Run Code Online (Sandbox Code Playgroud)
这是我试图传递一组自定义选项的视图:
class PairRequestView(FormView):
form_class = PairRequestForm
def get_initial(self):
requester_obj = Profile.objects.get(user__username=self.request.user)
accepter_obj = Profile.objects.get(user__username=self.kwargs.get("username"))
# `get_favorites()` is the object's method which returns a tuple.
favorites_set = requester_obj.get_favorites()
initial = super(PairRequestView, self).get_initial()
initial['favorite_choices'] = favorites_set
return initial
Run Code Online (Sandbox Code Playgroud)
在我的内部models.py,这里是上面使用的返回元组的方法:
def get_favorites(self):
return (('a', self.fave1), ('b', self.fave2), ('c', self.fave3))
Run Code Online (Sandbox Code Playgroud)
根据我的理解,如果我想预先填充表单,我会通过覆盖传递数据get_initial().我尝试favorite_choices用可调用的方式设置表单的初始数据.可赎回的人favorites_set.
使用当前代码,我收到错误 'tuple' object is not callable
我如何用自己的选择预先填充RadioSelect ChoiceField?
编辑:我也尝试过设置 initial['favorite_choices'].choices = …
django django-templates django-models django-forms django-views
我还没有找到有关基于多个文本文件的问答的文档,同时单独引用文本文件。
示例:我已经file1.txt通过了file20.txt。
file1.txt是从 2023 年 4 月开始,file5.txt是从 2023 年 3 月开始。
给定两个文件,我希望 chatGPT 读取这两个文件并回答比较问题,例如:
“从 3 月份的文件到 4 月份的文件,关于 ___ 的情绪有何变化/进展?”
“两个文件在 ___ 的讨论方面有何不同?”
“每个文件中 ___ 被提及多少次?”
这是非工作代码,说明了我想要实现的目标:
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
from langchain.llms import OpenAI
chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type="stuff")
query = "How has sentiment regarding medical devices changed/progressed from March to April?"
docs = [March_file, April_file]
chain({"input_documents": docs, "question": query}, return_only_outputs=True)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题:
分块 - langchain 的 QA 假设将您的所有个人文件分块为许多单独的、但仍然连续的块/ documents。然而,我的任务的性质需要分离文件,因为需要以某种方式通过不同的日期引用它们(并且仍然需要分块,因为文件很大)。
对特定文件的引用 - …
我记得玩过一些设置,我相信它改变了 dump.rdb 的位置。现在,dump.rdb 自动神奇地出现在我的项目的根目录中。
它属于哪里,我如何将它归还到那里?此外,在生产环境中,此位置如何更改?
我发现了一个类似的问题,它有点过时了。我想知道不使用另一个库是否可能。
目前,forms.ValidationError将触发form_invalid仅返回带有错误和状态代码的 JSON 响应。
我有一个 ajax 表单,想知道在 ajax 表单提交时是否可以在表单字段上进行通常的 django 字段验证。
我的表单触发错误:
class PublicToggleForm(ModelForm):
class Meta:
model = Profile
fields = [
"public",
]
def clean_public(self):
public_toggle = self.cleaned_data.get("public")
if public_toggle is True:
raise forms.ValidationError("ERROR")
return public_toggle
Run Code Online (Sandbox Code Playgroud)
ajax对应的View的mixin:
from django.http import JsonResponse
class AjaxFormMixin(object):
def form_invalid(self, form):
response = super(AjaxFormMixin, self).form_invalid(form)
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
else:
return response
def form_valid(self, form):
response = super(AjaxFormMixin, self).form_valid(form)
if self.request.is_ajax():
print(form.cleaned_data)
print("VALID")
data = {
'message': …Run Code Online (Sandbox Code Playgroud) 在我的设置中,我使用 django-environ 来设置密钥:
import environ
env = environ.Env()
SECRET_KEY = env.read_env('SECRET_KEY')
Run Code Online (Sandbox Code Playgroud)
在我的项目的根目录中,我确实有一个带有实际密钥的 .env 文件:
.env:
SECRET_KEY=qgw6s66n3e$27mmddfua*8yq6n%gz(!mx8e=@zbixk50-h020
Run Code Online (Sandbox Code Playgroud)
错误将在collectstatic. 这是回溯:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 194, in fetch_command
settings.INSTALLED_APPS
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", …Run Code Online (Sandbox Code Playgroud) 我对Heroku的Docker容器注册表CLI的理解是,它是Docker CLI的包装。
当我建立本地我会使用,例如:docker build -f Dockerfile.example --build-arg SECRET_KEY=abc。我将ARG SECRET_KEY在Dockerfile.example中进行设置。
但是,如果我想推送到heroku的docker container-registry,我发现我需要ENV SECRET_KEY=abc在Dockerfile.example中声明,然后运行命令heroku container:push example --recursive。
为什么会这样呢?是什么使它们与众不同?难道不是对ENV的不良安全做法进行硬编码?Heroku是否提供解决方法?
我在使用 Celery 运行任务时遇到了段错误。查找问题后,似乎其他人正在通过启动 celery 来解决类似的问题--pool=threads。
当我尝试通过时--pool=threads我得到ModuleNotFoundError: No module named 'threads'
thread我不相信这与会引发错误的模块相同No module named 'thread'。
如何开始使用线程以及它有什么作用?
Celery 站点中有关的文档--pool=threads非常稀疏。搜索“--pool”除了与台球相关的内容外不会返回任何实质性内容
我有两个单独的基于类的视图,并希望保持其功能,并让两个CBV指向同一个模板(我试图将两个单独的表单放入一个页面).
更具体地说,我试图将这个电子邮件视图和这个密码更改视图子类化/组合,以便我可以让它们指向同一个模板,因为我最终希望两个表单在同一页面上.
我试图通过将它们子类化到我自己的视图中来做到这一点:
class MyEmailUpdateView(LoginRequiredMixin, EmailView):
template_name = 'account/account_settings'
success_url = reverse_lazy('settings')
def form_valid(self, form):
return super(SettingsUpdateView, self).form_valid(form)
class MyPasswordUpdateView(LoginRequiredMixin, PasswordChangeView):
template_name = 'account/account_settings'
success_url = reverse_lazy('settings')
def form_valid(self, form):
return super(SettingsUpdateView, self).form_valid(form)
Run Code Online (Sandbox Code Playgroud)
但我现在发现由于错误,一个接一个地说,除非我手动将它传入(success_url,方法等),否则父类中的任何内容实际上都没有转移到我的自定义类.即便如此,我正在子类化的原始类中的代码也指向其他地方.
那么,当组合这两个视图时,我是否需要将所有原始代码复制到我的自定义子类视图中?
这是实现它的正确方法吗?我如何结合这两个视图?我最终想找到一种方法,在我自己的应用程序中将它们的两个表单放在一个页面上.是否有可能使用库提供的模板更简单的方法来实现这一目标?
试图让这项工作让我头晕目眩:
我有一个有序的词典:
OrderedDict([('key', {'keyword': {'blue', 'yellow'}), ('key1', {'keyword': {'lock', 'door'})])
Run Code Online (Sandbox Code Playgroud)
我有一个清单potential_matches:[red, blue, one]
我想将这些潜在的匹配命名为两个列表之一:
correct = []或incorrect = []
如果潜在匹配是dict中某个键的关键字,则它会进入correct,否则它会进入incorrect.
这个例子的结果应该是:
correct = [blue],incorrect = [red, one]
这是我尝试过的:
correct = []
incorrect = []
for word in potential_matches:
for key, value in ordered_dict.items():
if word in value["keyword"] and word not in correct:
correct.append(word)
elif word not in value["keyword"] and word not in correct and word not in incorrect:
incorrect.append(word) …Run Code Online (Sandbox Code Playgroud) python ×5
django ×4
django-forms ×2
celery ×1
cryptography ×1
dictionary ×1
django-views ×1
docker ×1
encryption ×1
for-loop ×1
heroku ×1
javascript ×1
json ×1
langchain ×1
nested ×1
openai-api ×1
py-langchain ×1
python-3.x ×1
redis ×1