I have the following code
models:
class Item(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField()
views:
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})
urls:
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")
)
forms:
class NewItemForm(forms.Form):
name=forms.CharField(max_length=70)
price=forms.IntegerField(min_value=0)
and when I call the 'new' view from the 'index' view I keep getting the error:
ValueError at /main/new/ invalid literal for int() with base 10: 'new'
What am I doing wrong?
you could post the whole error and traceback ... but at a guess
url(r'^(?P<item_id>\w+)/$',views.details,name="details"), #<- is the problem
url(r'^new/$',views.new,name="new")
since "new"
matches "\\w+"
... you can resolve this simply by swapping those two lines
url(r'^new/$',views.new,name="new")
url(r'^(?P<item_id>\w+)/$',views.details,name="details"), #<- is the problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.