Comments (11)
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.
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.
@danialkeimasi that seem better tho the issue is how to go about it.
from django-nextjs.
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.
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.
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.
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.
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.
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.
I have being able to make the error pages show in production but now the css is not showing
from django-nextjs.
Also thank you @danialkeimasi very much for the help
from django-nextjs.
Related Issues (20)
- Can we use the Django==1.11.14 version with any code changes as its basic support version is Django==3.2? HOT 1
- _next/webpack-hmr' failed: / Too many redirects HOT 9
- Passing Data HOT 3
- Incorrect Type Annotations
- Request for compression support in django-nextjs HOT 1
- Having issues with NGINX HOT 2
- Not a problem just a deseparate need for help,please response HOT 3
- Does this support dynamic routing such as passing a slug? HOT 1
- Is it possible for me to add the second frontend? HOT 1
- Customizing the HTML Response: Fix hydration error with Nextjs 13+ app router HOT 1
- ASGI Mode Causes Timeout Errors in Next.js Server-Side Rendering HOT 3
- CSS/JS not loading in production HOT 2
- setup with django < 4.2
- Client-side navigation does not work with NextJS 13 + App router
- CookieError - Attempt to set a reserved key 'expires' HOT 1
- it very hard to understand how this app work
- for next.js v12 vercel.svg can not loaded HOT 1
- for next.js v12 thru Django channels, 'admin/' doesn't work HOT 6
- Want to pass allow_redirects parameter to render function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-nextjs.