QueraTeam / django-nextjs

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

Not a problem just a deseparate need for help,please response #17

Closed lawrenceuchenye closed 1 year ago

lawrenceuchenye commented 1 year ago

Hi please how do you use django-nextjs with dynamice routes in nextjs like id/slug in urls

lawrenceuchenye commented 1 year ago

Don't bother i found a fix just use the string as it don't add any python django dynamic url mapping just as is in nextjs

danialkeimasi commented 1 year ago

Hello,

It seems that you're attempting to write a couple of Django paths that handle distinct views for str/id paths. Unfortunately, Next.js file-system based router doesn't support str/id or regex path. Although, you can use Next.js rewrites to manage the routing for those specific paths.

The rewrites feature in Next.js allows you to rewrite incoming requests before they are handled by the server. In this case, you can use a rewrite rule to match requests to /jobs/:jobId (where :jobId is any integer value) and redirect them to a different URL that includes the integer value as a query parameter.

Here's an example of how you can implement this:

# urls.py

urlpatterns = [
    path("/jobs/<str:category>", nextjs_pages.jobs_category, name="jobs_category"),
    path("/jobs/<int:job_id>", nextjs_pages.job, name="job"),
]
// next.config.js

const nextConfig = {
  async rewrites() {
    return [
      {
        source: "/jobs/:jobId(\\d+)",
        destination: "/jobs/job?jobId=:jobId", # or "/jobs/someRandomFolder/:jobId"
      },
    ];
  },
};

module.exports = nextConfig;

In this example, requests to /jobs/:jobId are rewritten to /jobs/job?jobId=:jobId. The :jobId placeholder is replaced with the integer value from the original URL, which is passed as a query parameter to the destination URL.

Note that you'll need to set up a route in your Next.js app to handle requests to /jobs/job, and extract the jobId query parameter.

I hope this helps! Let me know if you have any further questions.

lawrenceuchenye commented 1 year ago

Thank you very much for your response.

On Wed, Mar 22, 2023, 4:22 PM Danial Keimasi @.***> wrote:

Hello,

It seems that you're attempting to generate a couple of Django paths that handle distinct views for str/id paths. Unfortunately, Next.js file-system based router doesn't support str/id or regex path. Although, you can use Next.js rewrites https://nextjs.org/docs/api-reference/next.config.js/rewrites to manage the routing for those specific paths.

The rewrites feature in Next.js allows you to rewrite incoming requests before they are handled by the server. In this case, you can use a rewrite rule to match requests to /jobs/:jobId (where :jobId is any integer value) and redirect them to a different URL that includes the integer value as a query parameter.

Here's an example of how you can implement this:

If you need 2 Django paths like this:

urls.py

urlpatterns = [ path("/jobs/", nextjs_pages.jobs_category, name="jobs_category"), path("/jobs/", nextjs_pages.job, name="job"), ]

// next.config.js

const nextConfig = { async rewrites() { return [ { source: "/jobs/:jobId(\d+)", destination: "/jobs/job?jobId=:jobId", # or "/jobs/someRandomFolder/:jobId" }, ]; }, };

module.exports = nextConfig;

In this example, requests to /jobs/:jobId are rewritten to /jobs/job?jobId=:jobId. The :jobId placeholder is replaced with the integer value from the original URL, which is passed as a query parameter to the destination URL. Note that you'll need to set up a route in your Next.js app to handle requests to /jobs/job, and extract the jobId query parameter from the request URL.

I hope this helps! Let me know if you have any further questions.

— Reply to this email directly, view it on GitHub https://github.com/QueraTeam/django-nextjs/issues/17#issuecomment-1479769804, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATJ4B6VDQCSCM7CTU4ISGQDW5MKJTANCNFSM6AAAAAAWCQK7SY . You are receiving this because you modified the open/close state.Message ID: @.***>