亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我無法將“上下文”轉移到從端點 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 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號