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

在Django中添加路径创建多视图时遭遇ERR_CONNECTION_REFUSED错误

Troubleshooting ERR_CONNECTION_REFUSED in Your Django Films App

Hey there, let's break down why you're hitting that connection refused error after adding more views and path-based URLs. I spotted a few key issues to fix first:

1. Critical Variable Name Mismatch in the results View

Looking at your views.py, the results function has a straightforward but impactful mistake—you're using question_id in the string formatting, but the function's parameter is named film_id. This will throw a NameError when the view runs, which can crash the development server entirely, leading to the connection refused error.

Fix the results view like this:

def results(request, film_id):
    response = "You're looking at the results of film %s."
    return HttpResponse(response % film_id)  # Changed question_id to film_id

2. Verify Root URL Configuration

Make sure your project's main urls.py (not the films/urls.py file) includes the films app's URLs. If you haven't added this line, Django won't route requests to your app's new paths, and in some cases, this can cause server instability.

Add this to your project's root urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('films/', include('films.urls')),  # This line is crucial!
]

3. Restart the Django Development Server

After making changes to views.py or urls.py, the development server doesn't always auto-reload reliably (especially if you've made multiple quick changes). Stop the server with Ctrl+C and restart it using python manage.py runserver to ensure all your updates are applied.

4. Double-Check Path Syntax

Your use of path() instead of the older url() regex style is totally valid for Django 2.0+ (which the official tutorial uses). Your films/urls.py path definitions look correct once the view error is fixed, so no changes needed here.

Testing After Fixes

Once you've made these adjustments and restarted the server:

  • Visit http://localhost:8000/films/ to confirm the index still loads
  • Try http://localhost:8000/films/5/, http://localhost:8000/films/5/results/, and http://localhost:8000/films/5/vote/—all should load without connection errors now.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:12:16