0

我正在创建一个带有帖子和标签的小 Django 应用程序。但是我的 urls-models-views 有一些问题。我不明白为什么在我的应用程序中我得到这个错误,int() 以 10 为底的无效文字:'python'。这里的“python”是一个标签。我的文件:models.py:

class Category(models.Model):
    name = models.CharField(max_length=55)

    def __unicode__(self):
        return u"%s" %(self.name)



class Post(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, unique=True)
    excerpt = models.TextField(blank=True, help_text="A small teaser of\
                your content")
    content = MarkupField()
    date_created = models.DateTimeField(auto_now_add=True)
    is_published = models.BooleanField(default=True)
    objects = models.Manager()
    published_objects = PublishedManager()
    tags = TaggableManager()
    category = models.ForeignKey(Category)


    class Meta:
        ordering = ("date_created",)

    def __unicode__(self):
        return u"%s %s %s %s %s" %(self.title, self.date_created, self.is_published, self.category, self.tags)

    def get_absolute_url(self):
        return "/posts/%s/" % self.id

视图.py:

from django.conf import settings
from django.views.generic import ListView, RedirectView
from blog.models import Post
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse

def blogg(request):
    posts = Post.objects.get(title="generic views")
    ctx = {"posts":posts}
    return render_to_response("index.html",ctx, context_instance=RequestContext(request))

def post_detail(request, post_id):

    post = Post.objects.get(id=post_id)


    return render_to_response("post_detail.html",
        {"post":post}, context_instance=RequestContext(request))


def tagdetail(request, tag):
    posts = Post.objects.filter(tags=tag)
    return render_to_response("tag_detail.html",
        {"posts":posts, "tags":tag}, context_instance=RequestContext(request))

网址.py:

urlpatterns = patterns('',
    url(r'^$', "blog.views.blogg"),
    url(r'^posts/(?P<post_title>\d+)/$',"blog.views.post_detail"),
    url(r'^posts/tag/(?P<tag>\w+)$',"blog.views.tagdetail"),
)

错误:

ValueError at /posts/tag/python

invalid literal for int() with base 10: 'python'

Request Method:     GET
Request URL:    
Django Version:     1.4.3
Exception Type:     ValueError
Exception Value:    

invalid literal for int() with base 10: 'python'

Exception Location:     /home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 537
Python Executable:  /home/ada/virtualenv/bin/python
Python Version:     2.7.3
Python Path:    

['/home/ada/mainproject',
 '/home/ada/virtualenv/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/home/ada/virtualenv/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
 '/home/ada/virtualenv/src/django-taggit',
 '/home/ada/virtualenv/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/home/ada/virtualenv/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
 '/home/ada/virtualenv/lib/python2.7',
 '/home/ada/virtualenv/lib/python2.7/plat-linux2',
 '/home/ada/virtualenv/lib/python2.7/lib-tk',
 '/home/ada/virtualenv/lib/python2.7/lib-old',
 '/home/ada/virtualenv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/home/ada/virtualenv/local/lib/python2.7/site-packages',
 '/home/ada/virtualenv/local/lib/python2.7/site-packages/PIL',
 '/home/ada/virtualenv/lib/python2.7/site-packages',
 '/home/ada/virtualenv/lib/python2.7/site-packages/PIL']

Can yo please tell me why I get this error:invalid literal for int() with base 10: 'python'
Thanks for help.
4

3 回答 3

1

在 django 中,filter(tags=tag)是一个快捷方式tags__pk=tag,在你的情况下,你有一个标签名称,所以你应该使用filter(tags__name=tag)

于 2013-02-08T06:30:13.343 回答
0

tadgetails在您的视图中更新以下行

posts = Post.objects.filter(tags=tag)

作为

posts = Post.objects.filter(tags__name=tag)
于 2013-02-08T06:30:06.617 回答
0

对于这些问题,“base 10”后面是一个字,有一个可能的原因是数据库发生了变化,不是在外键列成为键之前,而是在数据没有被修改之前,解决办法是要么数据库数据或删除所有数据,或 /admin/ 手动修改外键数据。希望能帮到你

于 2017-04-30T09:56:59.227 回答