URL Parameter is not found, 404 error

26 views
Skip to first unread message

Malcolm MacKinnon

unread,
Sep 21, 2009, 3:28:57 PM9/21/09
to django...@googlegroups.com
Hi,

I'm new to Django. I keep getting a 404 error when I try to pass a "prod" parameter to a url. I think it is just this last parameter that is giving me trouble.

Here is my error:

Page not found (404)

Request Method:GET
Request URL:http://localhost/order_closeouts/imgs/WMS%20DIVA%20PANT%20BLK/

Here's my url:

(r'^order_closeouts/(?P<foo>\w{4})/(?P<prod>)w+/$',  'onhand.views.order_closeouts',  ),

Here is my view:

def order_closeouts(request,  foo=None,  prod=None):
    prod=prod
    ok='notok'
    message1=''
    u=request.user.username
    custs=Cust.objects.all()
    c_num=User.objects.get(username=u)
    custnum=c_num.first_name
    if not custnum or custnum=='':
        return HttpResponseRedirect('/denied/')
    cus=custnum
    for i in custs:
        if custnum in i.custno:
            ok='ok'
    if ok !='ok':
        return HttpResponseRedirect('/denied/')
    search_i=SearchForm()
    form=InvtForm2()
    forms=CustForm()
    forms_mast=SoMastForm()
    forms_tran=SoTranForm()
    #use date
    day=date.today()
    today=day.isoformat()
    if foo=='nchg':
        message1='You did not order any products yet. Please proceed below.'
    if foo=='chgs':
        message1='Your changes were successful! Please continue.'
    if foo=='edit':
        customers=SomastTemp.objects.get(custno=request.user.first_name)
    else:
        customers=Cust.objects.get(custno=request.user.first_name)
    
    return render_to_response('order_closeouts.html', {'prod':prod, 'foo':foo,'search':search_i,'form':form,'forms':forms, 'today': today, 'user':cus, 'forms_mast':forms_mast,  'forms_tran':forms_tran, 'customers':customers, 'message':message1}, context_instance = RequestContext(request))

Any help or suggestions would be very much appreciated. Thanks!

Malcolm MacKinnon

unread,
Sep 21, 2009, 4:12:48 PM9/21/09
to django...@googlegroups.com

Karen Tracey

unread,
Sep 21, 2009, 8:52:33 PM9/21/09
to django...@googlegroups.com
On Mon, Sep 21, 2009 at 3:28 PM, Malcolm MacKinnon <mmac...@gmail.com> wrote:
Hi,

I'm new to Django.
I keep getting a 404 error when I try to pass a "prod" parameter to a url.
I think it is just this last parameter that is giving me trouble.

Here is my error:
Page not found (404)

Request Method:GET
Request URL:http://localhost/order_closeouts/imgs/WMS%20DIVA%20PANT%20BLK/
Here's my url:

(r'^order_closeouts/(?P<foo>\w{4})/(?P<prod>)w+/$',  'onhand.views.order_closeouts',  ),


First, the closing paren for the "prod" capture needs to be after the pattern of characters you are willing to accept for it, so:

(r'^order_closeouts/(?P<foo>\w{4})/(?P<prod>w+)/$',  'onhand.views.order_closeouts',  ),

But that says you are willing to accept one or more 'w' characters for prod.  I expect you meant the character class \w:

(r'^order_closeouts/(?P<foo>\w{4})/(?P<prod>\w+)/$',  'onhand.views.order_closeouts',  ),

which would accept one or more alphanumerics or the underscore, that is the set [a-zA-Z0-9_].

But that won't quite work for the URL you have specified, as it includes spaces (url-encoded as %20).  If you want to allow spaces as well you'll need to change the pattern to allow that:

(r'^order_closeouts/(?P<foo>\w{4})/(?P<prod>[ \w]+)/$',  'onhand.views.order_closeouts',  ),

Karen

Malcolm MacKinnon

unread,
Sep 21, 2009, 9:31:45 PM9/21/09
to django...@googlegroups.com
Thanks, Karen. With your help, I manged to fix it.

Daniel Roseman

unread,
Sep 22, 2009, 2:47:11 AM9/22/09
to Django users
On Sep 22, 2:31 am, Malcolm MacKinnon <mmack3...@gmail.com> wrote:
> Thanks, Karen. With your help, I manged to fix it.

Note that - quite apart from your original problem - you've got a
serious inefficiency in this view. You iterate through every customer
to find a matching customer number which, once you get more than a
few, is going to take some time and use up tons of memory.

Instead of getting all the customers and looping through, just do
this:

try:
Customer.objects.get(custno=custnum)
except Customer.DoesNotExist:
return HttpResponseRedirect('/denied/')

--
DR.

Malcolm MacKinnon

unread,
Sep 22, 2009, 5:01:06 PM9/22/09
to django...@googlegroups.com
Thanks for your help, Daniel. I made the change.
Reply all
Reply to author
Forward
0 new messages