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

Django应用Localhost无法显示视图,出现页面未找到错误求助

Django应用访问问题排查与解决方案

Hey there! Let's break down and fix the issues with both of your Django 1.11 apps step by step.


第一个:文件存储应用(Localhost访问报错)

You mentioned the runserver console has no errors, but accessing localhost throws an error. Here are the most likely causes and fixes:

  • Missing view imports or invalid view definitions
    First, double-check your app's urls.py—did you import the index and document views correctly? You should have a line like:

    from .views import index, document
    

    Also, verify your views.py has these functions properly defined. For example, the document view needs to accept the slug parameter and fetch the corresponding Document object:

    from django.shortcuts import get_object_or_404, render
    from .models import Document
    
    def document(request, slug):
        doc = get_object_or_404(Document, slug=slug)
        return render(request, 'document_detail.html', {'document': doc})
    

    If the view is missing or has incorrect logic, that'll trigger errors even if the server runs fine.

  • Slug values aren't being generated for your Document instances
    Your Document model has a unique slug field, but there's no logic to auto-generate slugs when you create a new document. If your database entries have empty or invalid slugs, accessing the slug-based route will fail. Add this save method to your model to auto-create slugs:

    from django.utils.text import slugify
    
    class Document(models.Model):
        # ... your existing fields ...
        def save(self, *args, **kwargs):
            # Generate slug from title if it doesn't exist
            if not self.slug:
                base_slug = slugify(self.title)
                self.slug = base_slug
                # Handle duplicate slugs
                counter = 1
                while Document.objects.filter(slug=self.slug).exists():
                    self.slug = f"{base_slug}-{counter}"
                    counter += 1
            super().save(*args, **kwargs)
    

    After adding this, re-save existing Document instances (or create new test ones) to populate the slug field.

  • Index view logic or template issues
    If accessing http://localhost:8000/ directly throws an error, simplify your index view temporarily to test:

    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("Test: Index page works!")
    

    If this loads, the problem is in your original index view's data query or template rendering (like a missing template file, or invalid template syntax).


第二个:论坛类应用(页面未找到404错误)

A 404 here usually boils down to routing or data issues. Let's check the key points:

  • Your project's root urls.py isn't including the app's routes
    This is the most common culprit! You need to tell Django to use your app's URL patterns by adding an include in the project-level urls.py:

    from django.conf.urls import include, url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^', include('your_app_name.urls')),  # Replace 'your_app_name' with your actual app folder name
    ]
    

    Without this line, Django has no idea where to look for your app's /boards/... routes, so every request returns 404.

  • No test data exists for Board instances
    If you're trying to access a route like http://localhost:8000/boards/1/ but there's no Board with pk=1 in your database, Django's get_object_or_404 (assuming you're using it in your views) will return a 404. Head to the Django admin (/admin/) and create a few Board entries first.

  • View functions are missing or misconfigured
    Make sure your views.py has all the required functions with the right parameters. For example, board_topics should look something like this:

    from django.shortcuts import get_object_or_404, render
    from .models import Board
    
    def board_topics(request, pk):
        board = get_object_or_404(Board, pk=pk)
        return render(request, 'board_topics.html', {'board': board})
    

    If the function is missing, or doesn't accept the pk parameter, you'll get a 404 or server error.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:03:20