【发布时间】:2018-11-07 19:55:16
【问题描述】:
我对全栈开发还很陌生,所以这实际上可能是一个简单的问题:
我希望能够访问我的 Django 后端的 models.py 文件中的一个简单方法:
def testMethodFromModelPY(request, path=''):
data = {'returnedData': 'whatever'}
return data
我在urls.py中添加了方法的路径:
from django.urls import path
from DjangoBackEnd import models
urlpatterns = [
# some other paths here that frontend is able to access #
path(r'api/v1/', models.testMethodFromModelPY, name='testMethodFromModelPY'),
]
现在,在 Angular 前端中,我在 app 文件夹中创建了一个名为 config 的组件并添加到 config.service.ts
export interface Test {
returnedData: string;
}
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
testMethodInConfigServiceTS() {
return this.http.get('/api/v1/testMethodFromModelPY/');
}
}
我在 app.module.ts 中包含这个 Service 并在 config.component.ts 中调用 testMethodInConfigServiceTS 函数:
testMethodInConfigComponentTS() {
this.configService.testMethodInConfigServiceTS()
.subscribe(
(data: Test) => this.test = { ...data }, // success path
error => this.error = error // error path
);
}
在 config.component.html 中,我尝试使用以下方法访问此方法:
<button (click)="testMethodInConfigComponentTS()">teststuffhere</button>
但是当我点击一个按钮时,我得到了错误消息:
GET http://127.0.0.1:8000/api/v1/testMethodFromModelPY/ 404 (Not Found)
非常感谢!
【问题讨论】:
标签: django angular httprequest httpclient angular7