为什么我无法将 "context" 从端点 Django 应用程序传输到 HTML 页面?
Why I can not transfer "context" from endpoint Django app to HTML page?
我在将 {context} 从 Django 端点传输到 HTML 时遇到问题。我在项目中添加了第二个应用程序(第一个是 "tasks",一切正常,第二个是 "notes",问题出在这个应用程序上)。我不知道为什么来自 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: templates/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:" 和空白页面(没有笔记列表)。模型 db 是正确的 - 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',
],
},
},
]
改变
context = {
notes: "notes",
}
至
context = {
"notes": notes,
}
还要在 template
中的 {%
之后和 %}
之前添加 space
我在将 {context} 从 Django 端点传输到 HTML 时遇到问题。我在项目中添加了第二个应用程序(第一个是 "tasks",一切正常,第二个是 "notes",问题出在这个应用程序上)。我不知道为什么来自 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: templates/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:" 和空白页面(没有笔记列表)。模型 db 是正确的 - 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',
],
},
},
]
改变
context = {
notes: "notes",
}
至
context = {
"notes": notes,
}
还要在 template
{%
之后和 %}
之前添加 space