django - 与 angularjs 一起使用 mezzanine

标签 django angularjs mezzanine

我正在建立一个简单的网站,我想为 UI 集成 angularjs。然而,似乎 CMS 接管了一切并提供了一切,包括我想通过 angularjs 提供的任何东西。

我的 urls.py 文件:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    (r'^users/', include('apps.site_users.urls')),
    url('^$', 'mezzanine.pages.views.page', {'slug': '/'}, name='home'),
    url('', include('social.apps.django_app.urls', namespace='social')),
    ('^', include('mezzanine.urls')),
)

我对 angularjs 进行了所有必要的更改,因为没有 CMS,一切都加载得很好,但这意味着我无法提供 CMS 中的其他页面。关于需要做什么的任何想法?

最佳答案

您可以在HTML5 Mode中轻松设置Mezzanine以与Angular一起使用通过在 Angular 上设置应用程序的路由并通过 Django 设置基本 URL,确保任何未捕获的 URL 方案都重定向到“home”URL:

在 Django 上:

# urls.py
urlpatterns = patterns("",
    # Change the admin prefix here to use an alternate URL for the
    # admin interface, which would be marginally more secure.
    ("^admin/", include(admin.site.urls)),

    # If you'd like more granular control over the patterns in
    # ``mezzanine.urls``, go right ahead and take the parts you want
    # from it, and use them directly below instead of using
    # ``mezzanine.urls``.
    ("^", include("mezzanine.urls")),

    # AngularJS HTML5 mode (ie, remove the /#/ from URLs):
    # We need to redirect any uncaught URL schemes to the default home view.
    # http://scotch.io/quick-tips/js/angular/pretty-urls-in-angularjs-removing-the-hashtag
    url(r'^.*$', TemplateView.as_view(template_name='index.html'), name='home'),

    # etc...
)

在 Angular 上:

// app.js
app.config(function ($routeProvider, $locationProvider) {
  $routeProvider
    .when('/', {
      templateUrl: '/static/app/views/home.html',
    })
    .when('/profile/:profileId', {
      templateUrl: '/static/app/views/profile.html',
      controller: 'ProfileCtrl'
    })
    .when('/results', {
      templateUrl: '/static/app/views/results.html',
      controller: 'ResultsCtrl'
    })
    .otherwise({
      redirectTo: '/'
    });

    $locationProvider.html5Mode(true);
  });
});

在 HTML 上:

<!-- index.html -->
<!doctype html>
<html class="no-js" lang="es" ng-app="myApp">
  <head>
    <base href="/">
    <!-- etc -->
  </head>
  <!-- etc -->
</html>

相关博客文章here .

关于django - 与 angularjs 一起使用 mezzanine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20383232/

相关文章:

python - 如何将夹层安装为 Django 应用程序?

python - django 自动填充管理中的 manytomany 字段

django 如何断言 url 模式解析为正确的基于类的 View 函数

javascript - AngularJS 与 JavaScript 压缩器冲突

javascript - 是否可以定义一个解析对象并使其在所有路由中可用?

angularjs - ngSanitize 模块注入(inject)错误

python - 多维度获取django最新条目

python - django-south:先有鸡+有初始数据

python - 使用 ManyToManyField 时出现重复字段

django - 在 Django Mezzanine 中,如何对可以在页面树中的不同位置添加哪些自定义定义页面实现限制?