我正在创建一个带有帖子和标签的小 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.