Django - 基数为10的int()的文字无效

时间:2014-03-12 23:33:57

标签: python django django-forms django-views

我有以下代码

模型:

class Item(models.Model):
    name = models.CharField(max_length=50)
    price = models.IntegerField()

的观点:

def index(request):
    itemlist=Item.objects.all()
    return render(request,"items/index.html",{"itemlist":itemlist})

def details(request,item_id):
    item = get_object_or_404(Item,pk=item_id)
    return render(request,"items/details.html",{"item":item})

def new(request):
    if request.method == "POST":
        if "create" in request.POST:
            newitemform = NewItemForm(request.POST)
            if newitemform.is_valid():
                cd = newitemform.cleaned_data
                Item.objects.create(name=cd["name"],price=cd["price"])
                return HttpResponseRedirect(reverse("main:index"))

    else:
        newitemform = NewItemForm()

    return render(request,"tags/new.html",{"newitemform":newitemform})

网址:

urlpatterns = patterns("",
        url(r'^$',views.index,name="index"),
        url(r'^(?P<item_id>\w+)/$',views.details,name="details"),
        url(r'^new/$',views.new,name="new")
        )

形式:

class NewItemForm(forms.Form):
    name=forms.CharField(max_length=70)                     
    price=forms.IntegerField(min_value=0)

当我从'index'视图调用'new'视图时,我不断收到错误:

/ main / new /的ValueError int()的基数为10的无效文字:'new'

我做错了什么?

1 个答案:

答案 0 :(得分:2)

你可以发布整个错误和追溯......但是猜测

url(r'^(?P<item_id>\w+)/$',views.details,name="details"), #<- is the problem
url(r'^new/$',views.new,name="new")

因为"new"匹配"\w+" ...您可以通过交换这两行来解决此问题

url(r'^new/$',views.new,name="new")
url(r'^(?P<item_id>\w+)/$',views.details,name="details"), #<- is the problem