Sunday, September 24, 2017

Re: capturing url pattern from html forms

I found the problem - 

the key of the object that I was passing in the view's item_detail function's return render line was in quotes.  After removing the quotes, the problem went away.  I wonder why the quotes didn't post an issue before altering the url pattern. 


On Sunday, September 24, 2017 at 1:02:27 PM UTC-4, Mel DeJesus wrote:
When I try to go to index.html, I get: 

Exception Type:NoReverseMatch
Exception Value:
Reverse for 'item_detail' with arguments '(1, 1)' not found. 2 pattern(s) tried: [u'item\\.(?P<format>[a-z0-9]+)/?$', 'item/$']

My Views: 

def index(request):
items = Item.objects.exclude(amount=0)
return render(request, 'inventory/index.html',{
'items': items,
})
def item_detail(request):
try:
id=request.GET['id']
item = Item.objects.get(id=id)
except Item.DoesNotExist:
raise Http404("This item doesn't exist")
return render(request, 'inventory/item_detail.html',{
'item': item,
})

index.html

{%extends "base.html" %}

{%block content%}

<h3> Find An Item </h3>

<form id="form" form action = "item/" method="get">
Item Name:<br>
<input id="entry" type="text" name="id"><br>
<br><br>
<input type="submit" value="Submit">
</form>

<h2 id="output"></h2>

{%endblock%}

On Saturday, September 23, 2017 at 4:39:48 PM UTC-4, James Schneider wrote:


On Sep 23, 2017 1:27 PM, "Mel DeJesus" <meldej...@gmail.com> wrote:
Unfortunately, I didn't show my entire URLpatterns list, and the ^item/$  seems to interfere with the ^$ of the previous:  Any suggestions for a work around? thanks again. 

from django.conf.urls import include, url
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
from durham_app import views

urlpatterns = [
# Examples:
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^item/$', views.item_detail, name='item_detail'),
url(r'^items/', views.ItemList.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)


There isn't really a reason that any of those URL patterns would interfere with each other. I'd change the last one to r'items/$', but otherwise they look fine.

What do you mean by 'interfere'?

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/51722c88-2624-4a73-b7cb-7410f7884715%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment