为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我无法将“上下文”转移到从端点 Django 应用程序到 HTML 页面?

为什么我无法将“上下文”转移到从端点 Django 应用程序到 HTML 页面?

一只名叫tom的猫 2023-12-19 20:40:02
我在将 {context} 从 Django 端点传输到 HTML 时遇到问题。我向项目添加了第二个应用程序(第一个是“任务”,一切正常,第二个是“注释”,问题出在这个)。我不知道为什么,但是来自 context 的信息没有传输到 HTML。Python:notes/views.py@login_required(login_url='/user_login/')def notes_list(request, *args, **kwargs):if request.method == "POST":    name = request.POST.get("name")    description = request.POST.get("description")    new_note = Notes.objects.create(        name=name,        description=description,        user=request.user        )    new_note.save()    return redirect("/notes_list/")notes = Notes.objects.all().filter(user=request.user)print("notes", notes)context = {    notes: "notes",}return render(request, 'notes_list.html', context)HTML:模板/notes_list.htmlList:{%for note in notes%}<b>Title:</b>    <textarea  class="form-control" rows="1" cols="1"  name='name' >{{note.name}}</textarea><b>Note:</b>    <textarea  class="form-control" rows="3" cols="1"  name='description' >{{note.description}}</textarea>{%endfor%}当我访问 http://127.0.0.1:8000/notes_list/ 时,我只看到“List:”和空白页(没有注释列表)。模型数据库是正确的 - print("notes",notes) 在控制台中打印所有行,因此一切正常。这是settings.py 文件:    TEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',        'DIRS': [os.path.join(BASE_DIR, 'templates')],        'APP_DIRS': True,        'OPTIONS': {            'context_processors': [                'django.template.context_processors.debug',                'django.template.context_processors.request',                'django.contrib.auth.context_processors.auth',                'django.contrib.messages.context_processors.messages',            ],        },     },   ]
查看完整描述

1 回答

?
慕勒3428872

TA贡献1848条经验 获得超6个赞

改变

context = {
    notes: "notes",
}

context = {
    "notes": notes,
}

同时在 中的 {% 之后和 %} 之前添加空格template


查看完整回答
反对 回复 2023-12-19
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信