capturing url pattern from html forms

47 views
Skip to first unread message

Mel DeJesus

unread,
Sep 23, 2017, 2:48:37 PM9/23/17
to Django users

Hi - 

If the number '1' is submitted with the form below, the following url is created:  http://localhost:8000/item/?id=1

But I continually get a page not found.  How can I style the regex in the urlpatterns so that this url registers?  Thanks. 

<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>

I'm attempting to capture here: 

urlpatterns = [
# Examples:
url(r'^admin/', include(admin.site.urls)),
# url(r'^item\/\?id=(?P<id>\d+)/', views.item_detail, name='item_detail'),
url(r'^item/(?P<id>\d+)/', views.item_detail, name='item_detail'),
# JSON files






Daniel Roseman

unread,
Sep 23, 2017, 3:41:16 PM9/23/17
to Django users
GET parameters are not captured in URL patterns. Just accept `^item/$` and get the data inside the view via `request.GET['id']`.
-- 
DR. 

Mel DeJesus

unread,
Sep 23, 2017, 4:12:51 PM9/23/17
to Django users
Awesome, thanks!

Following your suggestion, I went to views and I assigned the request.GET['id'] to a variable and was able to use it! 

Mel DeJesus

unread,
Sep 23, 2017, 4:26:51 PM9/23/17
to Django users
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)





On Saturday, September 23, 2017 at 3:41:16 PM UTC-4, Daniel Roseman wrote:

James Schneider

unread,
Sep 23, 2017, 4:39:48 PM9/23/17
to django...@googlegroups.com


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

Mel DeJesus

unread,
Sep 24, 2017, 1:02:27 PM9/24/17
to Django users
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%}

Mel DeJesus

unread,
Sep 24, 2017, 1:16:54 PM9/24/17
to Django users
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. 
Reply all
Reply to author
Forward
0 new messages