Difficulty in passing positional arguments in url !!!

143 views
Skip to first unread message

livelikehimanshu12

unread,
Apr 21, 2015, 7:25:21 PM4/21/15
to django...@googlegroups.com
Hello friends ,
I am trying to send a simple parameter to view from my template .
But i am getting the error 'Reverse for acceptfriend with arguments ('hpe...@hp.com',) and keyword arguments {} not found. 
Plzz help me !! I am not much comfortable with url concepts !!
----> i need to pass hpe...@hp.com to my view !!

###################################################### url.py ##############################################################
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

admin.autodiscover()


urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    
url(r'^$', 'app.views.home',name='homeurl'),
    url(r'^signup/$','app.views.signup',name='signupurl'),
    url(r'^verifylogin/$','app.views.loginverify',name='verifyloginurl'),
    url(r'^verifysignup/$','app.views.signupverify',name='verifysignupurl'),
    url(r'^login/$','app.views.login',name='loginurl'),
   # url(r'^imgurl/$','app.views.imageviewer',name='imgurl'),
    url(r'^uploadpic/$','app.views.uploadpic',name='uploadpic'),
    url(r'^refreshpage/$','app.views.refreshpage',name='refreshpage'),
    url(r'^acceptfriend/?(\w+)/$', 'app.views.acceptfriend','acceptfriend'),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
 
###################################################### profile.html (my template )##################################################################
..
..
..{% for g in list_reqfromemail %}
                                            <tr>
                                               <td> <a href="{% url 'acceptfriend' g %}">{{ g }} </a></td>
                                                
                                            </tr>
                                        {% endfor %}
#################################################### views.py ################################################################################
def acceptfriend(request,reqfromemail):
    try:
        myemail=request.session['email']
        print("\n\n",request,"\n\n",myemail,"\n\n",reqfromemail)
        return render(request,'success.html',{"message":"congrats","name":myemail})
    except:
        return render(request,'error.html',{"error":"It is not working !!!"})

###########################################################################################################################################
If i remove the  href line from my template ,profile.html then my app runs normally !!
So obviously there is some mistake that i am doing while using href ,or sending url parameters or  in extracting it. Also what is the use of reverse() in django i read the documentation but i am unable to understand it !!

Bill Freeman

unread,
Apr 21, 2015, 7:46:12 PM4/21/15
to django-users
I think that it is the question mark in your url pattern that is causing the problem.  In a real url, question mark separates the path from the query parameters.  Parentheses in url patterns are for capturing parts of the path, and are not associated with query parameters.  Just leave out the ? and it may just work.

(Of course, if you also want this parameter filled in by the submission of a form, you would be better to put the email address in a query paramater, as a form submission would.)

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f300d565-d747-46a4-b685-2c1df8bb2d19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

HIMANSHU RANJAN

unread,
Apr 21, 2015, 8:41:31 PM4/21/15
to django...@googlegroups.com
Thanks for replying Bill :D
Ooops !!
 i removed the question  mark but it is till not working !!  
The problem is that i dont have a form and i think form maynot be suitable to for application !!

MY requirements :-
i have some list of items and clicking on any of it should go to the view and tell me which item was clicked 
i.e.  These are the items which are to be clicked 
      * h...@hp.com
      *a...@gmail.com
      *x...@gmail.com

Now if user clicks a...@gmail.com
      My view should know that a...@gmail.com was clicked 
     i.e. item selected (a...@gmail.com) should be passed to my view and i should be able to perform some calculation with this email  !!!


Any clue as to how to achieve this goal !!
I thought using url regex extraction ,i will be able to pass paramters to my views
  like  
     acceptfriend/a...@gmail.com/  or
    acceptfriend/h...@hp.com/       or
    acceptfriend/x...@gmail.com/     
 
 will match      r'^acceptfriend/(\w+)/$'    and this captured parameter ((\w+)/)  will be passed to my appropriate view !!

Stephen J. Butler

unread,
Apr 21, 2015, 8:46:15 PM4/21/15
to django...@googlegroups.com
\w won't match emails addresses. It doesn't include the "." or "@" characters.
> https://groups.google.com/d/msgid/django-users/CAPJbmvsF%2BKTSwxjGdu%3Dt6qxdgN7B2CvoZqKB2888%3DsT%2BEyMoXQ%40mail.gmail.com.

HIMANSHU RANJAN

unread,
Apr 21, 2015, 8:58:56 PM4/21/15
to django...@googlegroups.com

Hello Stephen ,
Even if I pass simple string like "hello world" instead of email address , it doesn't work !!

I don't know what's the pblm
Plzz help !!

Stephen J. Butler

unread,
Apr 21, 2015, 9:01:53 PM4/21/15
to django...@googlegroups.com
"hello world" wouldn't match \w either. \w is letters, numbers, and
the underscore.

On Tue, Apr 21, 2015 at 3:58 PM, HIMANSHU RANJAN
> https://groups.google.com/d/msgid/django-users/CAPJbmvtGjZuAH%2BvfqQgxWKqVq2DHqqZODBH8ET1Muon5vPRzXw%40mail.gmail.com.

Bill Freeman

unread,
Apr 21, 2015, 9:02:53 PM4/21/15
to django-users
The @ in the email address and the . in the domain name will cause \w+ to not match.  You could either go looser with
   .+
or better
   [^/]+
or you could be explicit with
   [a-zA-Z0-9_@.]+        ( . inside brackets doesn't need to be backslashed)
though that last won't be affected by local, so you could perhaps do
   (?:\w|[@.])+
While you might have a limited set of email addresses, note that . can occur in the username part, as can +, and we often see - in either part.

Probably best to try r'^acceptfriend/([^/]+)/$' next, then decide if you need to be more restrictive.

Vijay Khemlani

unread,
Apr 21, 2015, 11:35:52 PM4/21/15
to django...@googlegroups.com
I think it's better to pass the email as a query parameter instead of in the path

/acceptfriend/?email=hpenvy%40hp.com

Or even better, use a POST request, since it seems the request will touch the DB and have side effects.

Reply all
Reply to author
Forward
0 new messages