我在將 {context} 從 Django 端點(diǎn)傳輸?shù)?HTML 時(shí)遇到問(wèn)題。我向項(xiàng)目添加了第二個(gè)應(yīng)用程序(第一個(gè)是“任務(wù)”,一切正常,第二個(gè)是“注釋”,問(wèn)題出在這個(gè))。我不知道為什么,但是來(lái)自 context 的信息沒(méi)有傳輸?shù)?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%}當(dāng)我訪問(wèn) http://127.0.0.1:8000/notes_list/ 時(shí),我只看到“List:”和空白頁(yè)(沒(méi)有注釋列表)。模型數(shù)據(jù)庫(kù)是正確的 - print("notes",notes) 在控制臺(tái)中打印所有行,因此一切正常。這是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貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
改變
context = { notes: "notes", }
到
context = { "notes": notes, }
同時(shí)在 中的 {%
之后和 %}
之前添加空格template
- 1 回答
- 0 關(guān)注
- 124 瀏覽
添加回答
舉報(bào)
0/150
提交
取消