python - 打印 django 数据库中的数据

标签 python mysql database django dynamic-data-display

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

模型.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))

代码.html:

<html>

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

</html>

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

主题名称:

+----+-----------------+-----------+
| 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 |
+----+-----------------+-----------+

类(class):

+-------------+-----------+------------------+
| 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

最佳答案

试试这个

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

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

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

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

关于python - 打印 django 数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25337937/

相关文章:

python - 如何将数据框中每个组中的行转换为列?

mysql - 如何处理数据库中的子类别?

java - 无法从sqlite数据库android中删除行

python - Pandas - 按时间间隔截断数据帧

python - 如何确定数据类中的字段是否具有默认值或是否已显式设置?

python - 使用 Python ftplib ftp.dir() 或 ftp.retrlines ('LIST' ) 仅返回包含字符串的文件

php - 使用mysql查询不使用任何循环打印相应数字的字符

javascript - 使用同一页面上的复选框或单选按钮的 AJAX 过滤 php MySQL 结果

php - 如何在 php 中从 sql 创建自动完成

mysql - CouchDB 与关系数据库的分布式一致性?