Django:找不到页面(404)并且没有从模板中的模型获取数据

时间:2021-05-09 22:56:32

I have been following along with a YouTube tutorial and even though I follow along exactly as he does it, I get the 404 error below. I have also been having no luck getting data from models into my templates. Even if the template loads, it will just come through as blank. ( I can upload an example of this as well) It loads static content but not dynamic content from the database. Anyone know why this may be? I have a guess it might be something in my settings.py? Thank you!

我一直在关注YouTube教程,尽管我完全按照他的说法进行操作,但我在下面得到了404错误。我也没有运气从模型中获取数据到我的模板中。即使模板加载,它也只是空白。 (我也可以上传这个例子)它加载静态内容但不加载数据库中的动态内容。谁知道为什么会这样?我猜它可能是我的settings.py中的东西?谢谢!

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/band/

My project is setup like this:

我的项目设置如下:

atmos_v4/
    atmos_v4/
       init__.py
       settings.py
       urls.py
       wsgi.py
    band/
        __init__.py
        admin.py
        models.py
        urls.py
        views.py
    db.sqlite3
    manage.py
    static/
         css/
             ...
         img/
             ...
         js/
             ...
         media/
             ...
    templates/
             band/
                 band.html

My band/models.py file:

我的band / models.py文件:

from django.db import models
import datetime

class Band(models.Model):
    name = models.CharField(max_length=100, unique=True)
    date_added = models.DateTimeField(default=datetime.datetime.now())
    date_founded = models.DateTimeField(null=True, blank=True)
    image = models.CharField(max_length=1000, null=True, blank=True)

My band/view.py file:

我的band / view.py文件:

from django.shortcuts import render_to_response
from django.template import RequestContext
from band.models import Band

def band(request, band_id):
    band = Band.objects.get(pk=band_id)
    return render_to_response('band/band.html', {'band':band}, RequestContext(request))

My band/urls.py file:

我的band / urls.py文件:

from django.conf.urls import *

urlpatterns = patterns('',
                       url(r'^(?P<band_id>\d+)/$', 'band.views.band'),
    )

my atmos_v4/urls.py file:

我的atmos_v4 / urls.py文件:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView 

urlpatterns = patterns('',
    url(r'^$', TemplateView.as_view(template_name="index.html")),
    .
    .
    .
    (r'^band/', include('band.urls')),
)

My band.html file:

我的band.html文件:

<!DOCTYPE html>
<html lang="en">
  <head>
  <title></title>
  </head>
    <h1>{{ band }}</h1>
  <body>

  </body>
</html>

My settings.py file:

我的settings.py文件:

"""
Django settings for atmos_v4 project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y=3ey3sv8lm1j358(2bgthtx0bzy_cjaxug@2npx029nfs@5i%'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'beer',
    'band',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'atmos_v4.urls'

WSGI_APPLICATION = 'atmos_v4.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

from os.path import join

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

STATIC_PATH = os.path.join(BASE_DIR,'static')

STATICFILES_DIRS = (
    STATIC_PATH,
)

1 个解决方案

#1


1  

You're going to the URL "/band/" in your browser, but you haven't defined a urlpattern for that URL: you've defined "/" and "/band/<id>/". Either go to the root page, or find the ID of an existing band object and go to that page.

您将在浏览器中找到URL“/ band /”,但您尚未为该URL定义urlpattern:您已定义“/”和“/ band / /”。转到根页面,或找到现有band对象的ID并转到该页面。

You would almost certainly be better off doing the official Django tutorial, rather than following a random YouTube video.

你几乎肯定会更好地做正式的Django教程,而不是随机播放YouTube视频。

#1


1  

You're going to the URL "/band/" in your browser, but you haven't defined a urlpattern for that URL: you've defined "/" and "/band/<id>/". Either go to the root page, or find the ID of an existing band object and go to that page.

您将在浏览器中找到URL“/ band /”,但您尚未为该URL定义urlpattern:您已定义“/”和“/ band / /”。转到根页面,或找到现有band对象的ID并转到该页面。

You would almost certainly be better off doing the official Django tutorial, rather than following a random YouTube video.

你几乎肯定会更好地做正式的Django教程,而不是随机播放YouTube视频。