QueraTeam / django-nextjs

Next.js integration for Django projects
MIT License
341 stars 17 forks source link

Error Page Sync #24

Closed lawrenceuchenye closed 1 year ago

lawrenceuchenye commented 1 year ago

Hi do you have a way to sync the 404 page in nextjs with the django backend error pages

views.py from django_nextjs.render import render_nextjs_page_sync def error_404(request, exception): return render_nextjs_page_sync(request)

project/project/urls.py handler404 = "apps.views.error_404"

danialkeimasi commented 1 year ago

Hi. Currently, we don't have the necessary settings to support this. However, I believe it can be achieved with the following code in each view:

# views.py

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

async def jobs(request):
    response = await render_nextjs_page(request)

    if response.status_code == 404:
        raise Http404

    return response

I propose adding a setting for this purpose. Perhaps we can call it RAISE_ON_404 or something similar. Do you think this would be beneficial?

danialkeimasi commented 1 year ago

I should say that we can't raise Http404 in render_* function since it is not expected by normal Django's render function. Instead we should create some kind of view that can be configurable.

lawrenceuchenye commented 1 year ago

@danialkeimasi that seem better tho the issue is how to go about it.

danialkeimasi commented 1 year ago

For now, you can write a re-usable view (with adding *args, **kwargs to it) in your own code and re-use it for all pages like this:

# reusable_nextjs_page.py

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

async def nextjs_page(request, *args, **kwargs):
    response = await render_nextjs_page(request)

    if response.status_code == 404:
        raise Http404

    return response

and use it like this:

# urls.py
from django.urls import path
from reusable_nextjs_page import nextjs_page

urlpatterns = [
    path("/companies", nextjs_page, name="companies"),
    path("/companies/<str:slug>", nextjs_page, name="company"),
]
lawrenceuchenye commented 1 year ago

Hi being bug huntering for a while so being off i tried the import for render_nextjs_page but it's not working i can't import the package named "render_nextjs_page"

danialkeimasi commented 1 year ago

This isn't an actual import statement from a external package. It's just pseudo-code to demonstrate how you can create a reusable view in your own codebase.

danialkeimasi commented 1 year ago

I have been thinking about it, since creating a reusable view is simple, I suggest we skip it to keep the API less complex.

lawrenceuchenye commented 1 year ago

Ok wrote the code without the async but it still didn't trigger the custom nextjs 404 page

code snippet def uni_nextjs_page_view(request, *args, **kwargs): res = render_nextjs_page_sync(request) print("STATUS CODE:{}".format(res.status_code)) if res.status_code == 404: raise Http404 return res

lawrenceuchenye commented 1 year ago

def uni_nextjs_page_view(request, *args, **kwargs): res = render_nextjs_page_sync(request) print("STATUS CODE:{}".format(res.status_code)) if res.status_code == 404: raise Http404 return res

lawrenceuchenye commented 1 year ago

I have being able to make the error pages show in production but now the css is not showing

lawrenceuchenye commented 1 year ago

Also thank you @danialkeimasi very much for the help