2
回答
问一个调用django 中文章标签列表的问题.为什么与文章有关的标签多于1个就调用错误了?
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

models.py

class JobTag(models.Model):
    name = models.CharField(verbose_name=u'信息标签', max_length=16)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'标签'
        verbose_name_plural = u'信息标签'


class dede_archives(models.Model):
    #id=models.IntegerField(primary_key=True)
    typeid = models.ForeignKey(dede_arctype, verbose_name=u'分类信息')
    title = models.CharField(max_length='60')
    body = models.TextField()
    jobtag = models.ManyToManyField(JobTag, verbose_name='标签', blank=True, null=True)

 

 

views.py

def jobtaglist(request, tag):
    #jobtags = JobTag.objects.filter(name=tag)
    jobtags = get_object_or_404(JobTag, name=tag)
    taglist = jobtags.dede_archives_set.all()
    print jobtags
    paginator = Paginator(taglist, 2)
    page = request.GET.get('page')

    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
    return render_to_response('job/tags.html', {'user':request.user, 'contacts':contacts})

 

模板里这样调用:

<DIV class=itembox>

{% if contacts %}
{% for tags in contacts %}

<DIV class=box1>
<SPAN class=date>{{ tags.pubdate|date:"Y-m-d H:i" }}</SPAN>
<a href="{% url 'job_arc'  tags.id %}" target="_blank">{{ tags.title }}</A>
</DIV>
{% endfor %}
{% endif %}</DIV>

 

 

但是这样调用的时候就出错了:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/tags/%E4%BD%A0%E5%A5%BD

Django Version: 1.5.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'jobinfo')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "E:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "E:\python\yxszp\jobinfo\views.py" in jobtaglist
  185.     jobtags = get_object_or_404(JobTag, name=tag)
File "E:\Python27\lib\site-packages\django\shortcuts\__init__.py" in get_object_or_404
  113.         return queryset.get(*args, **kwargs)
File "E:\Python27\lib\site-packages\django\db\models\query.py" in get
  407.             (self.model._meta.object_name, num))

Exception Type: MultipleObjectsReturned at /tags/你好
Exception Value: get() returned more than one JobTag -- it returned 6!

 

我知道这个只让返回一个文章.如果系统里只有一个标签.那可以访问

如果多于一个标签.就会出现这样的错误:

 

 

这个调用视图应该怎么写啊?多谢

 

举报
gq2018
发帖于3年前 2回/272阅
共有2个答案 最后回答: 3年前
异常信息:

Exception Value: get() returned more than one JobTag--it returned6!

出错:jobtags=get_object_or_404(JobTag, name=tag)

换成  jobtags=get_list_or_404(JobTag, name=tag)   试试

引用来自“明天以后”的评论

异常信息:

Exception Value: get() returned more than one JobTag--it returned6!

出错:jobtags=get_object_or_404(JobTag, name=tag)

换成  jobtags=get_list_or_404(JobTag, name=tag)   试试

这样可以得到列表了。但是代码这样:

#文章标签列表
def wxtagslist(request, tags):
    wxtags = get_list_or_404(WXTag, name=tags)
    taglist = wxtags.zbwxinfo_set.all()
    paginator = Paginator(taglist, 2)
    page = request.GET.get('page')
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
    return render_to_response('wx/tags.html', {'user':request.user, 'contacts':contacts})

又出现这样的错误:

 

AttributeError at /wxtag/321/

'list' object has no attribute 'zbwxinfo_set'
Request Method: GET
Request URL: http://127.0.0.1:8000/wxtag/321/
Django Version: 1.5.5
Exception Type: AttributeError
Exception Value:
'list' object has no attribute 'zbwxinfo_set'
Exception Location: E:\python\zbvip1228\vipbbs\views.py in wxtagslist, line 163
Python Executable: E:\Python27\python.exe

 

还是没法根据这个得到标签列表?

顶部