简体   繁体   中英

Django: Display model values in table

I am a newbie to django and need some help. 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. 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"? 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.

{% 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. 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.

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.

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/

So would a solution be something like the following to 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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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