Coder Social home page Coder Social logo

Error Page Sync about django-nextjs HOT 11 CLOSED

querateam avatar querateam commented on July 26, 2024
Error Page Sync

from django-nextjs.

Comments (11)

danialkeimasi avatar danialkeimasi commented on July 26, 2024

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?

from django-nextjs.

danialkeimasi avatar danialkeimasi commented on July 26, 2024

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.

from django-nextjs.

lawrenceuchenye avatar lawrenceuchenye commented on July 26, 2024

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

from django-nextjs.

danialkeimasi avatar danialkeimasi commented on July 26, 2024

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"),
]

from django-nextjs.

lawrenceuchenye avatar lawrenceuchenye commented on July 26, 2024

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"

from django-nextjs.

danialkeimasi avatar danialkeimasi commented on July 26, 2024

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.

from django-nextjs.

danialkeimasi avatar danialkeimasi commented on July 26, 2024

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

from django-nextjs.

lawrenceuchenye avatar lawrenceuchenye commented on July 26, 2024

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

from django-nextjs.

lawrenceuchenye avatar lawrenceuchenye commented on July 26, 2024

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

from django-nextjs.

lawrenceuchenye avatar lawrenceuchenye commented on July 26, 2024

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

from django-nextjs.

lawrenceuchenye avatar lawrenceuchenye commented on July 26, 2024

Also thank you @danialkeimasi very much for the help

from django-nextjs.

Related Issues (20)

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.