Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从视图中的模型访问未返回的字符域?_Python_Django_Python 3.x_Django Models_Django Views - Fatal编程技术网

Python 如何从视图中的模型访问未返回的字符域?

Python 如何从视图中的模型访问未返回的字符域?,python,django,python-3.x,django-models,django-views,Python,Django,Python 3.x,Django Models,Django Views,在开始之前,我想澄清一下,我是django的初学者,所以我正在学习 这是我的模特 class MyNote(models.Model): title_of_note = models.CharField(max_length=200) date_created = models.DateTimeField('Created') details_note = models.TextField(max_length=2500, defau

在开始之前,我想澄清一下,我是django的初学者,所以我正在学习

这是我的模特

    class MyNote(models.Model):
         title_of_note = models.CharField(max_length=200)
         date_created = models.DateTimeField('Created')
         details_note = models.TextField(max_length=2500, default="")
         def __str__(self):
             return self.title_of_note
这是我的观点

def index(request):
    notes_list = MyNote.objects.order_by('date_created')
    context  = {'notes_list' : notes_list}
    return render(request, 'note/index.html', context)

def detail(request, note_id):
    note = get_object_or_404(MyNote, pk=note_id)
    return render(request, 'note/detail.html', {'request' : note})
我的目标是有一个主页/便笺/在这里我可以通过便笺的
标题从我所有的便笺中进行选择。在我选择了一个注释并单击其中一个(/note/1/)的链接后,它会将注释的
标题显示为标题,在标题下方,我可以看到我的详细信息
详细信息\u注释
。 到目前为止,我一直设法以笔记的标题作为链接,按创建日期排序。它可以正常工作,但我不知道如何将标题下的详细信息添加到/note/1/页面。据我所知,我可以在my models.py中的退货中添加
详细信息\u note
。但我不知道如何真正做到这一点,我知道我不能只做
返回self.title\u of\u note,self.details\u note

如何访问my views.py中的
详细信息\u note

我真的没有主意了,希望能得到一些帮助。这是我在这里的第一个问题,如果我做错了什么,我很抱歉

我的索引模板

<body bgcolor="black">
<p><font size="5", color="white">You are at the main page of my notes.</p>
{% if notes_list %}
    <ul>
    {% for note in notes_list %}
        <li><font size="3", color="white"><a href="{% url 'note:detail' note.id %}">{{ note.title_of_note }}</a></font></li>
    {% endfor %}
    </ul>
{% else %}
    <p><font size="5", color="white">There was a problem. Probably no notes</font></p>
{% endif %}
</body>

你在我笔记的主页上

{%if notes_list%}
    {注释列表%中注释的百分比}
  • {%endfor%}
{%else%} 有个问题。可能没有笔记

{%endif%}
这是我的详细信息模板

<body bgcolor="black">
    <h1><font size="5", color="white">{{ note.title_of_note }}</font></h1>
    <p><font size="3", color="pink">{{ note.details_note }}</font></p>
</body>

{{note.title}
{{note.details{u note}}


您需要将注释传递给模板

def detail(request, note_id):
    note = get_object_or_404(MyNote, pk=note_id)
    context = {'note': note}
    return render(request, 'note/detail.html', context)
在模板中,您可以从注释中获得如下信息:
{{note.title\u of_note}}
{{note.details\u note}}


在您的代码中,您正在使用
note
实例覆盖
request
上下文变量,不要这样做,因为Django将添加一个
request
上下文变量,以便在模板中使用。

您需要将注释传递给模板

def detail(request, note_id):
    note = get_object_or_404(MyNote, pk=note_id)
    context = {'note': note}
    return render(request, 'note/detail.html', context)
在模板中,您可以从注释中获得如下信息:
{{note.title\u of_note}}
{{note.details\u note}}


在您的代码中,您正在使用
note
实例覆盖
request
上下文变量,不要这样做,因为Django将添加一个
request
上下文变量,以便在模板中使用。

出于好奇,您是否也可以将模板添加到问题中?我没有想到这一点。现在就做。出于好奇,你能把你的模板也添加到问题中吗?我没想到。现在就做,我很感激。它完全符合我的要求。非常感谢,我非常感谢。它完全符合我的要求。非常感谢你
def detail(request, note_id):
    note = get_object_or_404(MyNote, pk=note_id)
    context = {'note': note}
    return render(request, 'note/detail.html', context)