显示其他属性值,如果在Django模板中已知的话 [英] displaying other attribute values if one is known in django template
问题描述
我在django中有这个应用,我正在尝试制作。这是index.html页面
< html>
< head>
< title>索引页< / title>
< / head>
< body>
< h1>选择学生的名称< / h1>
< form action ={%url'detail'%}method =postenctype =multipart / form-data>
{%csrf_token%}
< select name =namedrop>
{%为student_list%中的姓名}
< option value = {{name.stuname}}> {{name.stuname}}< / option>
{%endfor%}
< / select>
< input type =submitname =submit>
< / form>
< / body>
< / html>
,这是我们选择名称并点击提交时所指向的detail.html页面按钮...
<!DOCTYPE html>
< html>
< head>
< title>学生详情< / title>
< / head>
< body>
< p> hello {{name}}< / p>
< style type =text / css>
p
{
颜色:蓝色;
}
< / style>
< / body>
< / html>
但只显示hello neha,如果选择的名字是neha...但是我希望它从表格学生中打印出这个学生的所有细节......这是models.py ...
from django.db导入模型
课程(models.Model):
cid = models.IntegerField(unique = True,default = 0)
cname = models.CharField(max_length = 50)
def __unicode __(self):
return self.cname
$ b $ class Meta:
db_table =course
class student(models.Model):
stuid = models.IntegerField(default = 0)
stuname = models.CharField(max_length = 50)
cid = models.ForeignKey(course,to_field ='cid',on_delete = models.CASCADE)
def __unicode __(self):
return self.stuname
class Meta:
db_table =student
class subject(models.Model):
sid = models.IntegerField(default = 0)
sub = models .CharField (max_length = 50)
cid = models.ForeignKey(course,to_field ='cid',on_delete = models.CASCADE)
class Meta:
db_table =subject
def __unicode __(self):
return self.sub
这是views。 py
来自.models从django.http导入学生
从django.shortcuts导入Http404
import render
from django.template import loader
from django.http import HttpResponse
$ b def index(request):
student_list = student.objects.all( )
template = loader.get_template('studinfo / index.html')
context = {'student_list':student_list,}
return HttpResponse(template.render(context,request))
def detail(request):
if request.method =='POST':
name = request.POST ['namedrop']
idd = student.objects.all
return render(request,'studinfo / detail.html',{'name':name})
我也希望它通过details.html页面显示neha所有的主题。如何做到这一点?
在您的索引模板中,您在表单中传递名称,并使用它在视图中将其传递给详细模板。您需要获取学生
对象并将其传递给详细信息模板,以便您可以访问其所有字段。您可以从表单中传递id开始,而不是名称:
{%for student in student_list%}
< option value = {{student.id}}> {{student.stuname}}< / option>
{%endfor%}
然后在视图中你可以做这样的事情可能希望添加更多的理智检查,例如确保学生存在):
def detail(request):
如果request.method =='POST':
student_id = request.POST ['namedrop']
student_instance = student.objects.get(pk = student_id)
idd = student.objects .all
return render(request,'studinfo / detail.html',{'student':student_instance})
现在在详细模板中,您可以访问 student
对象,并可以使用它的所有属性(例如 {{student.stuname因为学生和科目之间没有直接的联系,所以你必须经过课程:
{%for subject in student.cid.subject_set.all%}
{{subject}}
{%endfor% }
i have this app in django that i'm trying to make. this is the index.html page
<html>
<head>
<title>The index page</title>
</head>
<body>
<h1>Choose the name of student</h1>
<form action= "{% url 'detail' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<select name="namedrop">
{% for name in student_list %}
<option value={{name.stuname}}>{{name.stuname}}</option>
{% endfor %}
</select>
<input type="submit" name="submit">
</form>
</body>
</html>
and this is the detail.html page to which it is directed when we select a name and click submit button...
<!DOCTYPE html>
<html>
<head>
<title>Details of student </title>
</head>
<body>
<p>hello {{name}}</p>
<style type="text/css">
p
{
color: blue;
}
</style>
</body>
</html>
but it only shows "hello neha" if the name selected is "neha"... but i want it to print all the details of this student from the table student...this is models.py...
from django.db import models
class course(models.Model):
cid=models.IntegerField(unique=True,default=0)
cname=models.CharField(max_length=50)
def __unicode__(self):
return self.cname
class Meta:
db_table= "course"
class student(models.Model):
stuid=models.IntegerField(default=0)
stuname=models.CharField(max_length=50)
cid=models.ForeignKey(course,to_field='cid',on_delete=models.CASCADE)
def __unicode__(self):
return self.stuname
class Meta:
db_table= "student"
class subject(models.Model):
sid=models.IntegerField(default=0)
sub=models.CharField(max_length=50)
cid=models.ForeignKey(course,to_field='cid',on_delete=models.CASCADE)
class Meta:
db_table= "subject"
def __unicode__(self):
return self.sub
this is views.py
from .models import student
from django.http import Http404
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
def index(request):
student_list=student.objects.all()
template = loader.get_template('studinfo/index.html')
context= { 'student_list' : student_list, }
return HttpResponse(template.render(context, request))
def detail(request):
if request.method=='POST':
name=request.POST['namedrop']
idd=student.objects.all
return render(request, 'studinfo/detail.html', {'name':name})
also i want it to display all the subjects that "neha" has through the details.html page...how do i do that?
In your index template you are passing the name in the form, and just use it in the view to pass it along to the detail template. You need to get the student
object and pass it to the detail template, so you have accesss to all its fields. You could start by passing the id in the form, instead of the name:
{% for student in student_list %}
<option value={{ student.id }}>{{ student.stuname }}</option>
{% endfor %}
Then in the view you can do something like this (you would probably want to add more sanity checks, like making sure the student exists):
def detail(request):
if request.method=='POST':
student_id = request.POST['namedrop']
student_instance = student.objects.get(pk=student_id)
idd=student.objects.all
return render(request, 'studinfo/detail.html', {'student': student_instance})
Now in the detail template you have access to the student
object and can use all its attributes (e.g. {{ student.stuname }}
).
Since there is no direct connection between students and subjects, you have to go through the course:
{% for subject in student.cid.subject_set.all %}
{{ subject }}
{% endfor %}
这篇关于显示其他属性值,如果在Django模板中已知的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!