如何以动态方式使用与“wait_for”一起使用的 Discord.py“check”参数/函数?

我收到以下行的错误:username = login_form.cleaned_data('username'),因为在某个地方它似乎是一个字典,但我无法理解为什么。谁能告诉我,问题是什么?


views.py


def index(request):

    return render(request, 'web/base.html')



def register_view(request):

    register_form = UserCreationForm()

    if request.method == "POST":

        register_form = UserCreationForm(request.POST)

        if register_form.is_valid():

            register_form.save()

            username = register_form.cleaned_data("username")

            password = register_form.cleaned_data("password1")

            user = authenticate(request, username=username, password=password)

            if user:

                login(request, user)

    return render(request, 'web/register.html', {'register_form': register_form})



def login_view(request):

    login_form = AuthenticationForm()


    if request.method == 'POST':

        login_form = AuthenticationForm(data=request.POST)

        if login_form.is_valid():

            username = login_form.cleaned_data('username')

            password = login_form.cleaned_data('password')

            user = authenticate(request, username=username, password=password)

            if user:

                login(request, user)


    return render(request, 'web/login.html', {'login_form': login_form})



def logout_view(request):

    logout(request)

    return redirect(reverse('web:index'))


浮云间
浏览 130回答 1
1回答

茅侃侃

我能够通过将我的问题+答案存储在 JSON 文件中来解决我自己的问题,对于“检查”功能,我创建了一个全局变量“triv”,它在调用之前使用 json 文件中的答案在测验功能中进行更新wait_for 和 check 函数。它运作得很好。triv = []async def play_trivia(new_msg, ctx, bot):&nbsp; &nbsp; with open("trivia_questions.json", 'r') as f:&nbsp; &nbsp; &nbsp; &nbsp; trivia = json.load(f)&nbsp; &nbsp; data = trivia['items']&nbsp; &nbsp; random.shuffle(data)&nbsp; &nbsp; for item in data:&nbsp; &nbsp; &nbsp; &nbsp; flag = 0&nbsp; &nbsp; &nbsp; &nbsp; for key in item:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q = item['q']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = item['a']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if flag < 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await new_msg.edit(content=q)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global triv&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triv = a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg = await bot.wait_for('message', check=check)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if msg:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await msg.add_reaction('✅')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await ctx.send(str(msg.author.name) + "wins this one")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await asyncio.sleep(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await ctx.send(q)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; triv = a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg = await bot.wait_for('message', check=check)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if msg:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await msg.add_reaction('✅')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await ctx.send(str(msg.author.name) + "wins this one!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await asyncio.sleep(2)def check(message):&nbsp; &nbsp; content = message.content.lower()&nbsp; &nbsp; return any(t in content for t in triv)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python