我正在学习Django,并创建了一个应用程序“ myapp”,每个组件都有不同的组件:
模型看起来像这样:
Model.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})
Urls看起来像这样:
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'),
]
用于渲染视图的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 %}