Django 博客应用在本地服务器只能看到管理页面

0 投票
1 回答
1760 浏览
提问于 2025-04-18 14:21

自从我更新到Django 1.6.5后,按照简单的步骤创建博客的例子就不再正常工作了。刚开始创建Django项目时,我可以访问127.0.0.1:8000/,一切运行得很好。但是,当我进一步创建博客应用,并在settings.py文件中把'blog'添加到INSTALLED_APPS后,我就无法再访问127.0.0.1:8000了,也无法访问127.0.0.1:8000/blog。此时我在访问127.0.0.1:8000/blog时收到了“页面未找到(404)”的错误:

    Request Method:     GET
       Request URL:     http://127.0.0.1:8000/blog

       Using the URLconf defined in mysite2.urls, Django tried these URL patterns, in this order:

       ^blog/
       ^admin/

       The current URL, blog, didn't match any of these.

但我确实创建了博客地址,错误信息中也提到了这个,^blog/。我将展示一些我认为对找出问题最有帮助的html和py文件。

settings.py文件位于C:\mysite2\mysite2

# 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.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=ja7o^#pm2hd!swt67%1h!)!et(i$91v1flks9ms-&8@r17((5'

# 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',
    'blog',         ## ***add for blog app
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

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

ROOT_URLCONF = 'mysite2.urls'

WSGI_APPLICATION = 'mysite2.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/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.6/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.6/howto/static-files/

STATIC_URL = '/static/'

modles.py文件位于C:\mysite2\mysite

from django.db import models
# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length = 140)
    body = models.TextField()
    date = models.DateTimeField()

    def __unicode__(self):
        return self.title

urs.py文件位于C:\mysite2\mysite2

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite2.views.home', name='home'),   ## ** i tried uncommenting but no help
    url(r'^blog/', include('blog.urls')),  ## once i add this on i can not go to http://127.0.0.1:8000/

    url(r'^admin/', include(admin.site.urls)),
)

admin.py文件位于C:\mysite2\blog

from django.contrib import admin
# Register your models here.
from  blog.models import Post
admin.site.register(Post)

urls.py文件位于C:\mysite2\blog

from django.conf.urls import patterns, include, url
from django.views.generic import ListView, DetailView
from blog.models import Post

urlpatterns = patterns('',
                        #url(r'^$', app.views.show_homepage),  ##http://stackoverflow.com/questions/11832178/404-error-in-django-when-visiting-runserver-returns-no-errors-though/11832382#11832382


                        url('r^$', ListView.as_view(
                            queryset=Post.objects.all().order_by("-date")[:10],
                            template_name="blog.html")),

)

base.html文件位于C:\mysite2\blog\templates

<h1>Django Tutorial Blog</h1>
{% block content %}
{% endblock %}

blog.html文件位于C:\mysite2\blog\templates

{% extends "base.html" %}    <!-- think a way to mesh in html and python django (this is sytax to do it   {%  %} -->
{% block content %}

{% for post in object_list %}
<h3>{{post.title}}</h3>

<div class = "post_meta">
    on {{ post.date }}
</div>

<div class = "post_body">
    {{ post.body|safe|linebreaks}}  <!-- if linebreaks automaticaly will read it-->
</div>

{% endfor %}
{% endblock %}

顺便说一下,虽然我无法访问127.0.0.1:8000/blog或127.0.0.1:8000,但我可以访问127.0.0.1:8000\admin并登录,更新博客等等。如果有人能告诉我问题出在哪里,那将是个美好的一天,因为我已经在这个问题上纠结了一段时间。

谢谢,tom

1 个回答

1

你写错字了:

                    url('r^$', ListView.as_view(

这里的 r 应该放在 ' 前面。把这一行改成这样:

                    url(r'^$', ListView.as_view(

撰写回答