如何在Django模板中显示模型中的一个字段 [英] How to display one field from a model in Django template

查看:162
本文介绍了如何在Django模板中显示模型中的一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个Django项目来维护,尽管完成了这个教程,当我涉及所有这个项目代码时,我感到很失落,一旦遇到模板,我就完全缺乏理解



我有一个叫做学生的模型,像

 学生(models.model); 
True
studentID = models.CharField(max_length = 6,primary_key = True)
age = models.IntegerField()

以及为简洁起见,省略了其他几个字段。



现在,我有一个HTML文件我想显示当前学生的分数(当前学生被定义为目前正在访问该页面,由学生ID识别)
在views.py中,我通过


获得当前学生

  currentStudent = Student.objects.get(studentID = request.session ['studentID'])
解决方案

Django将您的HTML文件填入空白处。



你告诉它空白处使用双括号 {{somevar}} 或许多内置的在这个 {%sometag%} 中的标签(例如'for')中。需要一些时间学习如何使用此功能,并了解所有选项(文档很棒),但是很容易理解基础知识。以下是一些示例:



views.py

  def示例(请求):
template ='index.html'
context = {'myvar':'hello world'}
返回render_to_response(模板,上下文)

index.html

  ;主体> 
< h1> {{myvar}}< / h1>
< / body>

结果:

 code><身体GT; 
< h1>你好世界< / h1>
< / body>

如您所见,render_to_response需要一个模板名称,然后填写空白。它需要index.html,查看上下文字典,找到 {{myvar}} 的每个外观,并将其替换为 hello world 。说我有一个名为 MyModel 的模型,它有一个字段 MyData ?要显示它,只需使用上下文发送:

  def example(request):
template ='index.html '
context = {'myvar':MyModel.objects.get(pk = 1)}
返回render_to_response(模板,上下文)

index.html

 < body> 
< h1> {{myvar.MyData}}< / h1>
< / body>

第二个例子:



views.py

  def example_fortag(request):
template ='fortag.html'
context = {'mylist ':[1,2,3]}
返回render_to_response(模板,上下文)

fortag.html

 < body> 
{%for mylist%}
< h1> {{item}}< / h1>
{%endfor%}
< / body>

结果如下所示:

 <身体GT; 
< h1> 1< / h1>
< h1> 3< / h1>
< / body>

获取?以下是使用if标签的示例:



views.py:

  

iftag.html:

 <身体GT; 
{%for mylist%}
{%if item / 2 == 0%}
< h1> {{item}}< / h1>
{%endif%}
{%endfor%}
< / body>

结果如下所示:

 <身体GT; 
< h1> 2< / h1>
< h1> 4< / h1>
< h1> 6< / h1>
< h1> 8< / h1>
< / body>

你也可以做复杂的东西。假设您以字典作为变量发送:

  context = {'myvar':{'key':'value'} 

你可以这样做:

  {%for key,value in myvar.items%} 
... etc ...

看到?我正在使用items函数,它是python语言中的一个内置的字典对象(请注意,我不需要使用()调用函数。



有了这一切,我强烈建议您浏览第三部分教程再一次缓慢的拿起来。 Django的模板语言是非常直观和非常强大的,希望这是有帮助的


I've inherited a Django project to maintain, and despite having done the tutorial, I'm feeling kind of lost when I am wading through all this project code and have just a general lack of understanding as soon as I hit templates.

I have a model called Student, defined like

class Student(models.model);
True
studentID = models.CharField(max_length = 6, primary_key = True)
age = models.IntegerField()

along with a few other fields omitted for brevity.

Now, I have a HTML file for the webpage on which I want to display the current student's score (current student is defined as the one currently accessing the page, identified by their student ID) in views.py, I get the current student by

currentStudent = Student.objects.get(studentID = request.session['studentID'])

My question: in my HTML file, how do I display the current student's age in part of a text block?

解决方案

Django takes your html files and fills in the blanks.

you tell it where the blanks are using double brackets {{ somevar }} or one of the many built-in tags (such as 'for') which look like this {% sometag %}. It takes some time learning how to work with this, and learning about all the options you have (the docs are great), but it's very easy to understand the basics. Here are a bunch of examples:

views.py

def example(request):
    template = 'index.html'
    context = {'myvar': 'hello world'}
    return render_to_response(template, context)

index.html

<body>
  <h1> {{ myvar }} </h1>    
</body>

result:

<body>
  <h1> hello world </h1>    
</body>

as you can see, render_to_response takes a template name and then fills in the blanks. It takes index.html, looks at the context dictionary, finds every appearence of {{ myvar }} and replaces that with hello world.

Say I have a model named MyModel which has a field MyData? To display it, simply send it with context:

def example(request):
    template = 'index.html'
    context = {'myvar': MyModel.objects.get(pk=1)}
    return render_to_response(template, context)

index.html

<body>
  <h1> {{ myvar.MyData }} </h1>    
</body>

Second example:

views.py

def example_fortag(request):
    template = 'fortag.html'
    context = {'mylist': [1, 2, 3]}
    return render_to_response(template, context)

fortag.html

<body>
{% for item in mylist %}
    <h1>{{ item }}</h1>
{% endfor %}  
</body>

The result would look like this:

<body>
  <h1>1</h1>
  <h1>2</h1>
  <h1>3</h1> 
</body>

Get it? Here's an example using the if tag:

views.py:

def example_iftag(request):
    template = 'iftag.html'
    context = {'mylist': range(1, 10)}
    return render_to_response(template, context)

iftag.html:

<body>
{% for item in mylist %}
    {% if item / 2 == 0 %} 
        <h1>{{ item }}</h1>
    {% endif %}
{% endfor %}  
</body>

The result would look like this:

<body>
  <h1>2</h1>
  <h1>4</h1>
  <h1>6</h1>
  <h1>8</h1>
</body>

You can also do complicated stuff. Say you send in a dictionary as a variable:

context = {'myvar': {'key': 'value'}}

you can do this:

{% for key, value in myvar.items %}
 ... etc ...

See? I'm using the items function, which is a built-in of the dictionary object in python language (note I don't need to call the function with ()).

With all that in mind, I strongly suggest you go over the third part of the tutorial one more time and pick it up slowly. Django's template language is pretty intuitive and very powerful, hope this is helpful

这篇关于如何在Django模板中显示模型中的一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆