Giter Site home page Giter Site logo

sorokinvld / djnext Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yourlabs/djnext

0.0 1.0 0.0 676 KB

NextJS Template backend for Isomorphic UI development in Django !MIRROR of git.yourlabs.org/oss/djnext!

JavaScript 30.62% Python 69.38%

djnext's Introduction

Django-NextJS

Isomorphic UI Development with Decorator pattern for Django with:

  • NextJS out of the box experience for perfect frontend development,
  • Django support for NextJS template rendering for perfect backend development,

Run the example project

Run this commands as non root:

git clone https://git.yourlabs.org/oss/djnext
cd djnext  # all commands must be executed from repo root
pip install --user --editable .[dev]
yarn install
djnext djnext  # required for yarn dev to run
yarn dev  # run localhost:3000
djnext dev  # run localhost:8000

The purpose of the example project is to show a create form and a list page with a hardcoded menu. For complete CRUD views, autogenerated menus and so on, use CRUDLFA+.

Install in your project

Install NextJS

In your repo root, install NextJS which means adding these dependencies:

yarn add nextjs react react-dom

And this commands to your package.json:

"scripts": {
  "dev": "next",
  "build": "next build",
  "start": "next start"
}

Then you can run yarn dev.

That was the first step of the NextJS tutorial.

Install DjNext

  • Install djnext with pip for example: pip install --user djnext
  • Then, add urls for /_next: path('_next', include('djnext.urls')),
  • Then add djnext to INSTALLED_APPS.
  • Run manage.py djnext

It will automatically setup the template backend if you haven't yourself. This allows picking up React templates in JS from django.

While you run yarn dev, you also need to run the djnext management command to maintain the NextJS pages directory with the static/pages directory of every INSTALLED_APPS.

This will watch static/pages directories of all apps in INSTALLED_APPS, and build the pages/ directory, so that yarn dev will find it.

If you want to setup the template backend yourself and override options you can do as such:

TEMPLATES = [
    {
        'BACKEND': 'djnext.backend.Backend',
        'OPTIONS': {
            'NEXTJS_DSN': 'http://localhost:3000',  # default: from env var
            'context_processors': [
                'djnext_example.artist.context_processors.menu',
            ]
        },
    }
]

The above adds a context processor that will recieve the request as first argument if the django-threadlocals middleware is installed, otherwise None. In return, the context processor function must return a dict that will be merged to the template context before being serialized and sent to the NextJS server.

Tutorial

NextJS template engine

Example project lives in src/djnext_example, see src/djnext_example/artist/urls.py:

CreateView.as_view(
    model=Artist,
    fields=['name'],
    success_url=reverse_lazy('artist_list'),
    template_name='create.js',
)

The template backend queries the NextJS render server, because the djnext command exposed components/*js and pages/*js from your INSTALLED_APPS to NextJS.

Note

For development, you need both yarn dev and manage.py djnext.

Troubbleshooting

Try rm -rf node_modules .next pages && yarn install && manage.py djnext & yarn dev to start the NextJS server from a fresh install.

Authors

djnext's People

Contributors

jpic avatar tbinetruy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.