简体   繁体   English

Django:在表格中显示模型值

[英]Django: Display model values in table

I am a newbie to django and need some help. 我是django的新手,需要一些帮助。 I have a created a vaguely functioning website. 我创建了一个功能不明确的网站。 I have a model which looks as follows; 我有一个模型,如下所示;

from django.db import models

class InductiveSensors(models.Model):
   Part_No = models.CharField(max_length=250)
   Manufacturer = models.CharField(max_length=250)
   Sn = models.CharField(max_length=250)
   AssuredSn = models.CharField(max_length=250)


def __str__(self):
    return InductiveSensors.Manufacturer

There are a couple of pages. 有几页。 one which gives me a list of all items in the database 一个给我列出数据库中所有项目的列表

{% extends "ESensFind/header.html" %}

{% block content %}
{% for InductiveSensors in object_list %}
<h5>{{ InductiveSensors.Manufacturer}}<a 
href="/Inductive_Search/{{InductiveSensors.id}}"> 
{{InductiveSensors.Part_No}}</a></h5>
{% endfor %}
{% endblock %}

When the {{InductiveSensors.Part_No}} link is clicked it opens up another page. 单击{{InductiveSensors.Part_No}}链接后,它将打开另一个页面。 On which I would like to display this database entry in a table with the information of 我想在上面的表中显示此数据库条目,其中包含以下信息:

   Part_No = models.CharField(max_length=250)
   Manufacturer = models.CharField(max_length=250)
   Sn = models.CharField(max_length=250)
   AssuredSn = models.CharField(max_length=250)

my urls look like: 我的网址看起来像:

from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from Inductive_Search.models import InductiveSensors

urlpatterns = [  
url(r'^$',ListView.as_view(queryset=InductiveSensors.objects.all().order_by
("Manufacturer") #THIS PAGE IS THE LIST OF ALL DATABASE ENTRIES#
[:25],template_name="Inductive_Search/Inductive_Search.html")),
url(r'^(?P<pk>\d+)$', ListView.as_view(model= InductiveSensors, 
template_name = 'Inductive_Search/SensorInfo.html')) #THIS OPENS UP A NEW 
INDEX PAGE AFTER A PARTICULAR DATABASE ENTRY LINK IS CLICKED#

is the 2nd url code correct to work in conjunction with "SensorInfo.html"? 第二个网址代码与“ SensorInfo.html”一起使用是否正确? In "SensorInfo.html" i have this code, which I would think should display even just the manufacturer information in a header but it displays nothing. 在“ SensorInfo.html”中,我有此代码,我认为该代码甚至应仅在标题中显示制造商信息,但不显示任何内容。

{% extends "ESensFind/header.html" %}

{% block content %}
<h5>{{ InductiveSensors.Manufacturer}}</h5>

{% endblock %}

Essentially what I am trying to do is get SensorInfo.html to display the values from my model, relating to that particular index in a table on my webpage. 本质上,我想做的是获取SensorInfo.html以显示模型中与我网页上的表中的特定索引相关的值。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

The "right" solution is to write detail view (by deriving from DetailView ), when in the template you can access the underlying object using get_object () or .object. “正确”的解决方案是编写详细信息视图(通过从DetailView派生),而在模板中,您可以使用get_object ()或.object访问基础对象。

If you want to implement it quick and dirty, you have to access the pk argument and using it get the model instance, and pass it as part of the context data. 如果要快速而又肮脏地实现它,则必须访问pk参数并使用它获取模型实例,并将其作为上下文数据的一部分传递。

I suggest you dig into documentation, the django tutorial is good place to start ( https://docs.djangoproject.com/en/2.0/intro/tutorial01/#writing-your-first-django-app-part-1 ) or read on class based views here https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-display/ 我建议您深入研究文档,django教程是开始的好地方( https://docs.djangoproject.com/en/2.0/intro/tutorial01/#writing-your-first-django-app-part-1 )或在此处阅读基于类的视图https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-display/

So would a solution be something like the following to urls.py? 那么解决方案将类似于urls.py的以下内容吗?

from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from Inductive_Search.models import InductiveSensors

urlpatterns = [
   url(r'^$', 
ListView.as_view(queryset=InductiveSensors.objects.all().order_by
("Manufacturer") 
[:25],template_name="Inductive_Search/Inductive_Search.html")),
url(r'^(?P<pk>\d+)$', DetailView.as_view(model= InductiveSensors, 
template_name = 'Inductive_Search/SensorInfo.html'))
]

and in the template 并在模板中

{% extends "ESensFind/header.html" %}

{% block content %}

    <h5>{{ get_object() }}</h5>

{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM