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

我在将 {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.html


List:

{%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',

            ],

        },

     },

   ]


一只名叫tom的猫
浏览 54回答 1
1回答

慕勒3428872

改变context&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;notes:&nbsp;"notes", }到context&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;"notes":&nbsp;notes, }同时在 中的&nbsp;{%&nbsp;之后和&nbsp;%}&nbsp;之前添加空格template
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5