这是我的HTML页面(在访问路径/
时提供):
<form ng-submit="ctrl.loginUser()" name="myLoginForm">
<div class="form-group">
<label>Username</label>
<input type="text" name="uname" class="form-control" ng-model="ctrl.loginuser.username" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="pwd" class="form-control" ng-model="ctrl.loginuser.password" required>
</div>
<input type="submit" value="Login">
</form>
<div ng-view></div>
这是在提交表单时调用的内容:
angular.module("BaseApp", [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}])
.config(['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);
}])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
templateURL: 'static/templates/mainPage.html'
})
/*
.otherwise({redirectTo: '/'});
*/
}])
self.loginUser = function(loginuser) {
return $http.post("/custom-api-auth/login", loginuser)
.then(function(response) {
$location.url("/home");
});
};
static/templates/mainPage.html
就是这样:
<html>
<head>
<base href="/" >
</head>
<h5>TESTTTTTTTT</h5>
我尝试过测试并提交表单。该网址更改为/home
,但TESTTTTTTTT
未显示。当我通过地址栏手动将静态网址更改为/static/templates/mainPage.html
然后按回车键时,mainPage.html
确实会显示。当我将templateURL: 'static/templates/mainPage.html'
更改为template: '<h5>This is the default route</h5>'
时,This is the default route
确实正确显示。
static/templates/mainPage.html
怎么没出现?
我使用Django / DRF作为我的后端,这是我的urls.py
:
from django.conf.urls import url
from django.conf.urls import include
from ebdjangoapp import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'stuffs', views.StuffViewSet)
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^api/', include(router.urls)),
# templates
url(r'^$', views.HomePageView.as_view()),
url(r'^home$', views.MainPageView.as_view()),
# login logout
url(r'^custom-api-auth/login$', views.login_view.as_view()),
url(r'^custom-api-auth/logout$', views.logout_view.as_view()),
]
这是我的views.py
:
class HomePageView(TemplateView):
def get(self, request, *args, **kwargs):
return TemplateResponse(request, "home.html")
class MainPageView(TemplateView):
def get(self, request, *args, **kwargs):
return TemplateResponse(request, "mainPage.html")
请注意/home
为mainPage.html
页面提供服务,/
为页面home.html
提供服务(我知道这有点令人困惑)。
答案 0 :(得分:1)
不是你的$ routeProvider代码中的templateUrl,而不是templateURL吗?