【问题标题】:Django: Page not found (404) and not getting data from models in templatesDjango:找不到页面(404)并且没有从模板中的模型获取数据
【发布时间】:2015-02-22 16:20:49
【问题描述】:

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

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

我的项目是这样设置的:

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

我的乐队/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)

我的乐队/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))

我的乐队/urls.py 文件:

from django.conf.urls import *

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

我的 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')),
)

我的 band.html 文件:

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

  </body>
</html>

我的 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,
)

【问题讨论】:

    标签: django views models


    【解决方案1】:

    您将在浏览器中访问 URL "/band/",但尚未为该 URL 定义 urlpattern:您已定义 "/""/band/&lt;id&gt;/"。要么转到根页面,要么找到现有乐队对象的 ID 并转到该页面。

    几乎可以肯定,你最好学习官方的 Django 教程,而不是跟随一个随机的 YouTube 视频。

    【讨论】:

    • 谢谢。我明白我现在的问题是什么。我从未在 url 中定义 /band/。是的,此时我已经阅读了一些不同的教程,但是我对 django 真的很陌生,所以很多东西都是一只耳朵,另一只耳朵,但我开始通过观看多篇文章来掌握事情的窍门人们一遍又一遍地这样做。
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2012-01-19
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2021-12-16
    • 2017-09-14
    相关资源
    最近更新 更多