有没有办法将Django与Next.js集成?

时间:2019-01-18 11:21:51

标签: django reactjs next.js

我已经通过具有访问build/index.html的功能将Reactjs与Django集成在一起。以下代码显示了我的操作方法。

config / urls.py

urlpatterns = [
    ...
    re_path(r'search/', react_views.ReactAppView.as_view()),
]

PROJECT_NAME / views.py

class ReactAppView(View):

    def get(self, request):
        try:
            with open(os.path.join(str(settings.BASE_DIR), 'frontend', 'build', 'index.html')) as file:
                return HttpResponse(file.read())
        except:
            return HttpResponse(
                """
                Build your React app.
                """,
                status=501,
            )

ReactAppView函数访问index.html,该yarn build在React端由index.html创建。基本上,我只将React用于搜索视图,而不是搜索视图,而是先使用jQuery开发的,然后使用jQuery。

由于我发现我需要Next.js才能在React中使用服务器端渲染(SSR),所以我一直在尝试将React应用程序迁移到Next.js。但是,Next.js没有pages/index.js。它只是有// Type: SoftArtisans.OfficeWriter.ExcelWriter.Cell // Assembly: SoftArtisans.OfficeWriter.ExcelWriter, Version=7.6.0.2814, Culture=neutral, PublicKeyToken=f593502af6ee46ae // MVID: 86DFA65A-7B7D-4BDD-9BD7-A23DDE47DC04 // Assembly location: \Bin\SoftArtisans.OfficeWriter.ExcelWriter.dll SoftArtisans.OfficeWriter.ExcelWriter.Cell.Formula this.sheet.Cells[TheRow, 11].Formula = "=IFERROR( CHOOSE(L7,\"A1\",\"R2\",\"S3\",\"I4 R\",\"P R\",\"Snse ve\",\"Suspensr\",\"Deleted\",\"InMark\",\"Peng\",\"veyed\",\"veyed\"),\" \")"; this.sheet.Cells[TheRow, 12].Value = item["id"];

我已经非常努力地找出如何将Django与Next.js集成在一起,但是我找不到任何有用的资源。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您似乎想提供静态文件(即React或Next.js捆绑包)。 Django提供了有关如何执行此操作的指南(通过django.contrib.staticfiles

最简单的示例(直接来自文档)是:

  • 设置STATIC_FILES文件夹:

    STATIC_URL = '/static/'

  • index.html文件放在此处,并将其引用为/static/index.html

有关staticfiles的更多信息,请参考官方文档:https://docs.djangoproject.com/en/2.1/howto/static-files/

在Next.js端,您需要遵循https://nextjs.org/#static-exporting上的示例,或者手动创建一个index.html,其中包括您正在使用的所有next.js捆绑软件。

相关问题