0

I'm trying to do a GET request from angular passing through a parameter to django's view.py.
The url that I'm getting back is /contact/api/getName?name=John and neither of them is hitting api_get_client_info in views.py because the string and values inside api_get_client_info are being printed. But I am getting a "success" message after the request.

HTML:

      <select ng-change="selectName(name)" ng-model="name">
        <option value="" disabled>Select from List</option>
        <option ng-repeat="name in nameList" value="{{name.Name}}"> {{name.Name}} </option>
      </select>

$http.get request in angular controller:

        $scope.selectName= function(name){
    Method 1: 
            $http.get("/contact/api/getName", {params: {"personName": name}})
            .success(function(response) {
                console.log("success");
            })
            .error(function(response){
                console.log("failed");
            })

    Method 2:
           $http({
             method:'GET',
             url: '/contact/api/getName?=' + name 
           })
           .success(function(data) {
             console.log("success");
           })
           .error(function(data){
             console.log("failed");
           })
       }

Django url.py

urlpattern = [
  url(r'^api/getName(?P<personName>[a-zA-Z0-9_]+)/$', views.api_get_person_info),
  url(r'', views.main, name='contact',
]

Django views.py

def api_get_person_info(request):
    print("here")
    person = request.GET.get("personName")
    print(person)

1 Answer 1

1

I presume you mean the statements inside api_get_person_info are not being printed.

This is because your URL doesn't match. Your pattern expects the personName within the URL itself: ie api/getName/John/, but you are passing the parameter in the query string, api/getName?personName=John. You should make the pattern just r'^api/getName$'.

The reason why you are getting a 200 is because the second pattern, for contact, does match; you haven't terminated the regex, so it matches everything. That pattern should be r'^$'.

Sign up to request clarification or add additional context in comments.

1 Comment

awesome! I got it to work by changing url to url(r'^api/getClientInfo', views.api_get_client_info),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.