Почему я не могу перенести «контекст» из конечной точки Django приложения на страницу HTML? - PullRequest
0 голосов
/ 13 апреля 2020

У меня проблема с переносом {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: 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%}

Когда я go до http://127.0.0.1: 8000 / notes_list / Я вижу только «Список:» и пустую страницу (без списка заметок). Модель БД верна - print («заметки», 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 Ответ

0 голосов
/ 13 апреля 2020

Изменить

context = {
    notes: "notes",
}

На

context = {
    "notes": notes,
}

Также добавить пробел после {% и до %} в template

...