Django应用Localhost无法显示视图,出现页面未找到错误求助
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'surls.py—did you import theindexanddocumentviews correctly? You should have a line like:from .views import index, documentAlso, verify your
views.pyhas these functions properly defined. For example, thedocumentview needs to accept theslugparameter and fetch the correspondingDocumentobject: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
YourDocumentmodel has a uniqueslugfield, 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 thissavemethod 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 accessinghttp://localhost:8000/directly throws an error, simplify yourindexview 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 anincludein the project-levelurls.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 likehttp://localhost:8000/boards/1/but there's noBoardwithpk=1in your database, Django'sget_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 yourviews.pyhas all the required functions with the right parameters. For example,board_topicsshould 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
pkparameter, you'll get a 404 or server error.
内容的提问来源于stack exchange,提问作者tonde

