django在我的网址末尾添加%EF%BB%BF

时间:2016-10-30 15:13:48

标签: html django utf-8 byte-order-mark

我正在开发一个博客django app。 我有帖子列表和帖子详细信息页面。 当我点击列表中的帖子标题时,应该转到帖子详细信息页面。

views.py

from django.shortcuts import render, get_list_or_404
from .models import Post


def list_of_post(request):
    post = Post.objects.all()
    template = 'blog/post/list_of_post.html'
    context = {'post': post}
    return render(request, template, context)


def post_detail(request, slug):
    post = get_list_or_404(Post, slug=slug)
    template = 'blog/post/post_detail.html'
    context = {'post': post}
    return render(request, template, context)

urls.py

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

urlpatterns = [
    url(r'^$', views.list_of_post, name='list_of_post'),
    url(r'^(?P<slug>[-\w]+)/$', views.post_detail, name='post_detail')
]

models.py

from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User


class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published')
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250)
    content = models.TextField()
    seo_title = models.CharField(max_length=250)
    seo_description = models.CharField(max_length=160)
    author = models.ForeignKey(User, related_name='blog_posts')
    published = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=9, choices=STATUS_CHOICES, default='draft')

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.slug])

    def __str__(self):
        return self.title

list_of_post.html

{% extends 'blog/base.html' %}
{% block title %}List of blog post{% endblock %}
{% block content %}
    {% for posts in post %}
        <h2><a href="{{ posts.get_absolute_url }}">{{ posts.title }}</a></h2>
        <p>Written by {{ posts.author }} on {{ posts.published }}</p>
        <hr>
        {{ posts.content|truncatewords:40|linebreaks }}
    {% endfor %}
{% endblock %}

由于某种原因,网址不起作用。而是显示404看起来像这样:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/blog/practice-title/%EF%BB%BF
Using the URLconf defined in cms.urls, Django tried these URL patterns,in this order:
^admin/
^blog/ ^$ [name='list_of_post']
^blog/ ^(?P<slug>[-\w]+)/$ [name='post_detail']
The current URL, blog/practice-title/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

和想法?

0 个答案:

没有答案