从django中的数据库打印数据

时间:2014-08-16 07:27:19

标签: python mysql database django dynamic-data-display

我想在django中显示带有外键的两个连接表的数据,我已经设置了所有内容,但它没有显示任何内容,这里是我的文件:

models.py:

from django.db import models
from django.utils.encoding import smart_unicode

class Course (models.Model):
Course_name=models.CharField(max_length=120,null=False,blank=False)
course_id=models.AutoField(primary_key=True)
course_idtomatch=models.IntegerField(max_length=2)

def __unicode__(self):
    return smart_unicode(self.Course_name)

class Subjectsnames(models.Model):
subject_name=models.CharField(max_length=50)
course=models.ForeignKey(Course)


def List item__unicode__(self):
    return smart_unicode(self.subject_name)

views.py:

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from .models import Subjectsnames
from .models import Course


def Subjects(request):
Subject = Subjectsnames.objects.get(id=id)

subjects_data = {
    "subjects_names":Subject
}

print subjects_data
return render_to_response("code.html",subjects_data,context_instance=RequestContext(request))

code.html:

<html>

{% for name in Subjectsnames %}
Name:{{name.subject_name}}
Id:{{name.id}}
{% endfor %}

</html>

我在我的数据库中有这些数据:

Subjectsnames:

+----+-----------------+-----------+
| id | subject_name    | course_id |
+----+-----------------+-----------+
|  2 | Organic         |         1 |
|  3 | inorganic       |         1 |
|  4 | thermodynacmics |         2 |
|  5 | vectors         |         2 |
|  6 | trigo           |         3 |
|  7 | algebra         |         3 |
|  8 | relational      |         3 |
|  9 | c++             |         4 |
| 10 | c#              |         4 |
+----+-----------------+-----------+

课程:

+-------------+-----------+------------------+
| Course_name | course_id | course_idtomatch |
+-------------+-----------+------------------+
| chemistry   |         1 |                1 |
| pyhics      |         2 |                2 |
| maths       |         3 |                3 |
| computers   |         4 |                4 |
+-------------+-----------+------------------+

忽略course_idtomatch

据我所知,我可能在code.html中的for循环中做错了。 如果有人知道请帮我解决,请提前致谢。

我更新了我的views.py之类的内容:

from django.conf import settings
settings.configure()
from django.shortcuts import render, render_to_response, RequestContext

from django.db import models
from .models import Course
from .models import Subjectsnames

def Subjects(request):
Subject = Subjectsnames.objects.get(id=id)

subjects_data = {
    "subjects_names":Subject
}

print subjects_data
return render_to_response("code.html",subjects_data,context_instance=RequestContext(request))


print Subject

但现在当我在终端上运行python views.py时出现如下错误:

Traceback (most recent call last):
File "views.py", line 7, in <module>
from .models import Course
ValueError: Attempted relative import in non-package

1 个答案:

答案 0 :(得分:0)

试试这个

 Name:{{subjects_names.subject_name}}
 Id:{{subjects_names.id}}

由于您只渲染一个对象而不是查询集,因此您不需要forloop

我希望,这行中的id定义在某处:

Subject = Subjectsnames.objects.get(id=id)