Skip to main content
 首页 » 编程设计

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

2024年10月01日35xing901022

我正在学习 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 看起来像这样: 查看.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}) 

网址看起来是这样的: 网址.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 如下所示: 主页.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 %}