You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中Ajax调用提示URL不存在问题求助

Troubleshooting "Url could not be found" Error in Your Ajax Calls

Let's walk through the common issues that could be causing this 404 error and how to fix them step by step:

1. Verify the Actual Requested URL

First, check what URL your browser is actually sending. Open your browser's Developer Tools (F12), switch to the Network tab, trigger the Ajax call, and look for the failed request. Compare the URL in the request with what your Django URLs are configured to handle.

For example, if you call getBodyHeights(12), the request URL should be /master/heights/12/ (with a trailing slash). If it's missing the trailing slash, that's likely the problem.

2. Fix Trailing Slash Mismatches

Your master app's URL patterns include a trailing slash in the regex (e.g., r'^heights/(\d+)/$'), meaning the request URL must end with a slash to match. You have three ways to fix this:

  • Update your JS to include the trailing slash:
    url: '/master/heights/' + seats_id + '/',
    
  • Modify Django regex to allow optional trailing slashes:
    url(r'^heights/(\d+)/?$', views_data.body_heights, name='heights'),
    
  • Confirm APPEND_SLASH = True in your Django settings (this is the default). If it's set to False, Django won't auto-redirect URLs without slashes to the version with slashes.

3. Validate Parameter Format

Your Django regex uses (\d+), which only matches numeric values. If seats_id, body_id, or facelift_id is non-numeric (like a string or empty value), the regex won't match, leading to a 404. Add a quick check in your JS functions:

function getBodyHeights(seats_id) {
    const parsedId = parseInt(seats_id);
    if (!Number.isInteger(parsedId)) {
        console.error('Invalid seats ID:', seats_id);
        return;
    }
    // Rest of your code
}

4. Stop Hardcoding URLs (Use Django Reverse Resolution)

Hardcoding URLs in JS is error-prone if your Django URL config changes. Instead, generate valid URLs using Django's url template tag and pass them to your JS.

In your template, add a script block to define URL templates:

<script>
const ajaxUrls = {
    heights: "{% url 'heights' 0 %}".replace('0', ''),
    facelifts: "{% url 'facelifts' 0 %}".replace('0', ''),
    seats: "{% url 'seats' 0 %}".replace('0', '')
};
</script>

Then update your JS functions to use these dynamic URLs:

function getBodyHeights(seats_id) {
    // ...
    $.ajax({
        type: 'GET',
        url: ajaxUrls.heights + seats_id + '/',
        // ...
    })
}

5. Check View Function Spelling & Existence

Double-check that views_data in your master app's urls.py actually contains the functions body_heights, facelifts, and seats with the exact correct names. A tiny typo (like body_height instead of body_heights) would cause a 404.

6. Use Django's DEBUG Mode for Clues

If DEBUG = True in your settings, Django's 404 page will show a list of all URL patterns it tried to match against your request. This will tell you exactly which part of your URL isn't aligning with the regex.


内容的提问来源于stack exchange,提问作者Boky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:17:22