python - 如何使用 Django 通过链接呈现与一个条目关联的所有数据?

标签 python django django-forms django-templates django-views

我正在学习 Django 并创建了一个应用程序“myapp”,具有不同的组件,如下所示:

模型看起来像这样: 模型.py:

from __future__ import unicode_literals
from django.db import models

class Country(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name


class CountryDetails(models.Model):
    country = models.ForeignKey(Country)
    T_attack = models.PositiveIntegerField(verbose_name="attack")
    year = models.CharField(max_length=254)
    Deaths = models.CharField(max_length=254)
    NEWS_ID = models.CharField(max_length=254, verbose_name="NEWS_ID")


    def __unicode__(self):
        return self.Deaths

View 如下所示: View .py:

from django.shortcuts import render, get_object_or_404
from .models import Country, CountryDetails 

def home(request):
    c_list = Country.objects.all()
    return render(request, 'myapp/home.html', {'c_list':c_list})

def details(request, pk):
    c_details =  get_object_or_404(CountryDetails, pk)
    return render(request, 'myapp/home.html', {'c_details':c_details})

网址看起来是这样的: urls.py:

from django.conf.urls import url
from django.contrib import admin
from . import views 

urlpatterns = [

url(r'^$', views.home, name='home'),
url(r'^myapp/(?P<pk>\d+)/$', views.details, name='details'),

]

渲染 View html 如下所示: home.html:

 {% for c in c_list %}

 <h1><a href="{% url 'details' pk=c.pk %}">{{ c }}</a></h3></br>

 {% endfor %}

简而言之,我创建了一个应用程序,其中包含像这样的各个国家/地区的一些数据

原始数据:

name   T_attack   year  Deaths  News_ID

India    12       2006   12       NDTV
India    110      2009   1        DEAN
PAKISTAN 9        2002   10       XYZT
PAKISTAN 11       2021   11       XXTV
India    12       2023   120      NDNV
India    10       2012   12       DEAN
PAKISTAN 12       2022   12       DLRS
Canada   1        2001   1        DLTV
USA      2        2011   13       NTTV

“myapp”的“Home.html”呈现所有可用国家/地区的列表,如下所示:

INDIA 
PKISTAN
USA
CANADA

我期望当我点击时,让我们假设在印度,它应该在第二个“html”页面上呈现所有可用的详细信息,例如“detailed.html”,与印度相关联,如下所示:

name   T_attack   year  Deaths  News_ID
India    12       2006   12       NDTV
India    110      2009   1        DEAN
India    12       2023   120      NDNV
India    10       2012   12       DEAN

但是当我执行代码时,它显示一些错误,如下所示:

ValueError at /myapp/1/
need more than 1 value to unpack
Request Method: GET
Request URL:    http://127.0.0.1:8000/myapp/1/
Django Version: 1.10
Exception Type: ValueError
Exception Value:    
need more than 1 value to unpack
Exception Location: /home/jai/Desktop/trytable/local/lib/python2.7/site-packages/django/db/models/sql/query.py in build_filter, line 1130
Python Executable:  /home/jai/Desktop/trytable/bin/python
Python Version: 2.7.6
Python Path:    
['/home/jai/Desktop/trytable/test_project',
 '/home/jai/Desktop/trytable/lib/python2.7',
 '/home/jai/Desktop/trytable/lib/python2.7/plat-i386-linux-gnu',
 '/home/jai/Desktop/trytable/lib/python2.7/lib-tk',
 '/home/jai/Desktop/trytable/lib/python2.7/lib-old',
 '/home/jai/Desktop/trytable/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/jai/Desktop/trytable/local/lib/python2.7/site-packages',
 '/home/jai/Desktop/trytable/lib/python2.7/site-packages']
Server time:    Sat, 15 Jul 2017 16:03:33 +0000

请帮帮我。谢谢

最佳答案

c_details =  get_object_or_404(CountryDetails, pk) 

你应该将其替换为

c_details = CountryDetails.objects.filter(country__pk=pk)

在您的网址中,您使用的是国家/地区的pk,因此您需要过滤与某个国家/地区相关的所有行。 filter 查询可以做到这一点。 要在模板中显示详细信息,您可以执行类似的操作。

{% for c in c_details %}

 <h1>{{ c.T_attack }}</h3></br>
 <!-- and so on for the other fields also. -->

{% endfor %}

关于python - 如何使用 Django 通过链接呈现与一个条目关联的所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117235/

相关文章:

python - 如何处理 Python 中的不确定形式

java - 使用 4 个已知 GCP 点将经纬度坐标转换为像素坐标的代码

python - 使用 eclipse 运行 Python Django migrate 时出现 Mysql 错误

python - Nginx+FastCGI 上的 Django 站点出现问题(504 网关超时)

django - 具有过滤查询集的组权限自定义表单

python - 如何防止多余的换行符出现在我的正则表达式匹配中?

Python kludge 将 UCS-2(UTF-16?)读取为 ASCII

python - 使用 Reportlab 生成具有自定义尺寸页面和最佳图像分辨率的 PDF

Django简单验证码无法获取ValidationError异常

python - 分配 django 错误之前引用的局部变量