我在將 {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
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報
0/150
提交
取消