Code Monkey home page Code Monkey logo

django_nextjs_build's Introduction

Django + Next.js Project Building

We are going to combine Django and Next.js servers, use Django to accept web requests and use Next.js as an internal service that generates the HTML.

Sources:

Architecture

Development Environment

When you run the project with ./manage.py runserver, you are using Django as a proxy for all the requests between you and Next.js.

Production Environment

In production, you should proxy some Next.js requests (requests that require no Django manipulation) through your webserver to reduce unnecessary loads on the Django server.

Installation

Dependencies Package

Required version

  • Django 4.0.0
  • django-nextjs 2.4.0
  • channels 3.0.4
git clone [email protected]:ycpranchu/django-nextjs-build.git
cd django-nextjs-build
pip install -r requirements.txt

Create Django project and app

Django project name django-nextjs-blog

django-admin startproject 'django-nextjs-blog'

Django app name django_app

cd 'django-nextjs-blog'
python manage.py startapp 'django_app'

md static
md templates

Create Nextjs project

Nextjs project name nextjs_app

npx create-next-app
npm run dev

The Next.js is up on port 3000.

Django Setting

Under Project

setting.py

  1. Add apps and requirements to INSTALLED_APPS.
INSTALLED_APPS = [
    "django_app",
    "channels",
    "nextjs_app",
    ...
]
  1. Set the debug mode and allowed hosts.
DEBUG = True

ALLOWED_HOSTS = ['*']
  1. Set the templates directory path.
import os
TEMPLATES = [
    {
        ...
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        ...
    },
]
  1. Use ASGI instead of WSGI.
# WSGI_APPLICATION = "django_nextjs_blog.wsgi.application"
ASGI_APPLICATION = "django_nextjs_blog.asgi.application"
  1. Set the time zone.
LANGUAGE_CODE = "zh-hant"

TIME_ZONE = "UTC"
  1. Set the static directory path.
STATIC_URL = "static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
  1. Initialized the database.
python manage.py makemigrations
python manage.py migrate

urls.py

Include the django-nextjs URLs inside project urls.py

# django_nextjs_blog/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("django_app.urls")),
]

asgi.py

Set Django as ASGI server.

import os

from django.core.asgi import get_asgi_application
from django.urls import re_path, path

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_nextjs_blog.settings")
django_asgi_app = get_asgi_application()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django_nextjs.proxy import NextJSProxyHttpConsumer, NextJSProxyWebsocketConsumer

from django.conf import settings

# put your custom routes here if you need
http_routes = [re_path(r"", django_asgi_app)]
websocket_routers = []

if settings.DEBUG:
    http_routes.insert(0, re_path(r"^(?:_next|__next|next).*", NextJSProxyHttpConsumer.as_asgi()))
    websocket_routers.insert(0, path("_next/webpack-hmr", NextJSProxyWebsocketConsumer.as_asgi()))


application = ProtocolTypeRouter(
    {
        # Django's ASGI application to handle traditional HTTP and websocket requests.
        "http": URLRouter(http_routes),
        "websocket": AuthMiddlewareStack(URLRouter(websocket_routers)),
        # ...
    }
)

Under App

views.py

from django.http import HttpResponse
from django_nextjs.render import render_nextjs_page

async def index(request):
    return await render_nextjs_page(request)

urls.py

create urls.py under app's folder

# django_app/urls.py

from django.urls import path
from .views import index
urlpatterns = [
    path("", index, name="index"),
]

Make it work

Nextjs frontend

# terminal_1
npm run dev

Django server

# terminal_2
python manage.py runserver

Now you can see Nextjs running on Django server port 8000.

django_nextjs_build's People

Contributors

ycpranchu avatar

Stargazers

 avatar Mohamad Faishol avatar

Watchers

 avatar

Forkers

zichdan

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.