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)
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'^$'
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.