如何在Django的第二个应用程序中检索第一个应用程序模型数据。
我在Trible应用中创建模型scheme_details_model
,并且必须在主题scheme_title
的字段中显示home1.html
。
# this is model in trible app
class scheme_details_model(models.Model):
scheme_id = models.CharField(max_length=200, blank=False, null=True)
scheme_title = models.CharField(max_length=200, default='')
publish_on = models.CharField(max_length=200, default='')
detail_description = models.CharField(max_length=200, blank=False,
null=True)
validity_upto = models.CharField(max_length=50, null=True, blank=True)
pdf_doc_type = models.FileField(upload_to="driver_file/",
validators=[validate_file_extension],
null=True)
source_url = models.CharField(max_length=100, null=True, blank=True)
created_date = models.DateTimeField(default=datetime.now, blank=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
db_table = "scheme_details_table"
# view function in theme app
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from DigitalMine.tribal.models import scheme_details_model
@login_required
def home(request):
book = scheme_details_model.objects.all()
data = {}
data['object_list'] = book
print(data)
return render(request, "home1.html")
将“ scheme_title”字段显示为重定向到另一个页面的链接
答案 0 :(得分:1)
我认为您忘记了将数据传递到模板文件。更改此代码:
return render(request, "home1.html")
收件人:
return render(request, "home1.html", data)
然后您可以像这样将变量访问到html文件中:
{% for scheme in object_list %}
<a href="#">{{ scheme.scheme_title }}</a>
{% endfor %}