1

models.py

    from django.db import models

    class Blog(models.Model):
        time = models.DateTimeField(auto_now_add = True)
        title = models.CharField(max_length = 100)
        slug = models.SlugField()
        perex = models.TextField()
        content = models.TextField()
        @models.permalink
        def get_absolute_url(self):
            return ('blog', [self.slug])

        def __unicode__(self):
            return self.title

        class Meta:
            ordering = ['-time']

ビュー.py

    from django.shortcuts import render_to_response, get_object_or_404
    from blog.models import Blog

    def blog_entries(request):
        blogs = Blog.objects.all()[0:3]
        title = "Blogs"
        return render_to_response('blog/blog.djhtml', {'blogs': blogs, 'title': title,})

    def blog_single_entry(request, slug):
        blog = get_object_or_404(Blog, slug=slug)
        title = blog.title
        return render_to_response('blog/single.djhtml', {'blog': blog, 'title': title,})

url.py

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

    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    admin.autodiscover()

    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'kablog.views.home', name='home'),
        # url(r'^kablog/', include('kablog.foo.urls')),

        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

        # Uncomment the next line to enable the admin:
         url(r'^admin/', include(admin.site.urls)),
         url(r'^blog/', 'blog.views.blog_entries', name='blog'),
         url(r'^blog/(?P<slug>[-\w]+)/', 'blog.views.blog_single_entry', name='single_blog'),
    )

テンプレート

    {% extends 'base.djhtml' %}
    {% block title %}| {{title}}{% endblock %}
    {% block content %}
    <div class="hero-unit">
      <h1>Welcome to my Blog</h1>
      <p>Where knowledge is always free</p>
      <p>
        <a class="btn btn-primary btn-large">
          Read More
        </a>
      </p>
    </div>
    <div class="row">
        {% for blog in blogs %}
            <div class="span4">
                <h2>{{blog}}<small>{{blog.time|date:"M D d Y"}}</small></h2>
                <p>{{blog.perex|safe}}</p>
                <a class="btn" href="{{ blog.get_absolute_url }}">
                  Read More
                </a>
            </div>
        {% endfor %}
    </div>
    {% endblock %}

blog.get_absolute_url はスラッグを返さず、また、「blog/my-first-blog」をブラウズしようとしても、ブラウザは single_blog ではなくホーム ブログを表示するだけで、「ブラウジングしても 404 エラーを返さない」ブログ/dgdsghdsfhdsfhds"

4

2 に答える 2

0

私もそれを試しましたが、うまくいかないので、他のアプローチを試します

class Blog(models.Model):
    [......]

    @property
    def get_blog_url(self):
        return reverse('blog', args=[self.slug])

<a class="btn" href="{{ blog.get_blog_url }}">
    Read More
</a>
于 2013-03-20T08:54:24.787 に答える
0

@models.permalink代わりに作る必要があります@permalink

于 2016-08-31T20:28:06.657 に答える