python3 django1.8 mysql5.5给出页面未找到(404),而sqlite工作正常

时间:2016-10-24 04:51:04

标签: python django

MySQL的:

DATABASES = {
    'default': {
        'NAME': 'ch01',
        'ENGINE': 'mysql.connector.django',
        'HOST':'172.17.100.18',
        'USER': 'test',
        'PASSWORD': '123456',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

源码:

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

博客/ urls.py:

from django.conf.urls import url, include
from . import views

urlpatterns = [
    #url(r'^$', views.post_list, name='post_list'),
    url(r'^$', views.PostListView.as_view(), name='post_list'),
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',
        views.post_detail,
        name='post_detail'),
]

当我使用第二个数据库设置运行服务器项目工作正常,但是当我使用mysql运行第一个设置文件时,django会为所有网址返回404错误。

查看博客应用程序:

from .models import Post
import logging

def post_list(request):
    object_list = Post.published.all()
    paginator = Paginator(object_list, 3) # 3 posts in each page
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer deliver the first page
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range deliver last page of results
        posts = paginator.page(paginator.num_pages)
    #logging.console(page)
    return render(request, 'blog/post/list.html', {'posts': posts})

class PostListView(ListView):
    queryset = Post.published.all()
    context_object_name = 'posts'
    paginate_by = 4
    template_name = 'blog/post/list.html'

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post, status='published',
                             publish__year=year, publish__month=month,
                             publish__day=day)
    return render(request, 'blog/post/detail.html', {'post': post})

mysql中的数据:

+----+-------+-------+------+---------------------+---------------------+---------------------+-----------+-----------+
| id | title | slug  | body | publish             | created             | updated             | status    | author_id |
+----+-------+-------+------+---------------------+---------------------+---------------------+-----------+-----------+
|  1 | Test1 | test1 | ??   | 2016-10-24 03:46:04 | 2016-10-24 03:46:16 | 2016-10-24 03:46:16 | published |         1 |
+----+-------+-------+------+---------------------+---------------------+---------------------+-----------+-----------+

错误信息:

找不到页面(404) 请求方法:GET 请求网址:http://172.17.100.19/blog/2016/10/24/test1/ 提出者:blog.views.post_detail

没有帖子与给定的查询匹配。

您看到此错误,因为您的Django设置文件中有DEBUG = True。将其更改为False,Django将显示标准的404页面。

models.py

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse


class PublishedManager(models.Manager):

    def get_queryset(self):
        return super(PublishedManager, self).get_queryset()\
                .filter(status='published')

class Post(models.Model):
    STATUS_CHOICES = (
            ('draft', 'Draft'),
            ('published', 'Published'),
            )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date="publish")
    author = models.ForeignKey(User, related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="draft")

    objects = models.Manager()
    published = PublishedManager()


    class Meta:
        ordering = ('-publish', )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.publish.year,
            self.publish.strftime('%m'),
            self.publish.strftime('%d'),
            self.slug])

1 个答案:

答案 0 :(得分:1)

NSString *myString = YOUR_ARRAY.uppercaseString; [myNSMutableArray addObject:myString]; ,一切都好。

USE_TZ = False根据settings.py 中指定的时区存储时间时,即如果USE_TZ = True以UTC格式存储,并且在显示模板时它将在本地区。

由于时区设置,错误404的原因是网址中的日期与正确的日期不匹配。要避免这种情况,您可以设置USE_TZ = False

参阅: Difference between USE_TZ = False and USE_TZ = True