slug url和更多页面之后

时间:2013-01-16 17:49:36

标签: django slug

我不明白如何在slug字段之后生成url,在django前输入stop另一个页面以保存slug字段值或应该转到views.py的值

我有models.py

from django.db import models
from django.db.models import permalink

class Blog(models.Model):
    title =     models.CharField(max_length=100, unique=True)
    slug =      models.SlugField(max_length=100, unique=True)
    body =      models.TextField()
    posted =    models.DateField(db_index=True, auto_now_add=True)
    category =  models.ForeignKey('blog.Category')

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
    return ('view_blog_post', None, {'slug': self.slug})

views.py

def index(request):
        return render_to_response('index.html', {'categories':Category.objects.all(), 'posts': Blog.objects.all()[:5]})

def view_post(request, slug):
        return render_to_response('view_post.html', {'post':get_object_or_404(Blog, slug=slug)})

def view_details(request, slug):
            return render_to_response('view_details.html', {'post':get_object_or_404(Blog, slug=slug)})

urls.py

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

urlpatterns = patterns('',

    url(r'^$', 'blog.views.index', name='index'),
    url(r'^blog/view/(?P<slug>[^\.]+)', 'blog.views.view_post', name='view_blog_post'),
    url(r'^blog/view/(?P<slug>[^\.]+)'/details, 'blog.views.view_details', name='view_blog_details'),
)

的index.html

{% extends 'base.html' %}
{% block titulo %} My blog {% endblock %}

{% block content %} 
<h2>posts</h2>
    {% if posts %}
        <ul>
        {% for post in posts %}
        <li><a href="{{ post.get_absolute_url }}">{{post.title}}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No hay post's</p>
    {% endif%}
{% endblock %}

view_post.html

{% extends 'base.html' %}
{% block head_title %} {{post.title}} {% endblock %}
{% block title %} {{post.title}} {% endblock %}

{% block content %}
    {{post.body}}
{% endblock %}

我不能像

那样制作网址

localhost:8000 / field-slug / details

这个网址不起作用,有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试更改slug模式并将$添加到url的末尾:

urlpatterns = patterns('',
    url(r'^$', 'blog.views.index', name='index'),
    url(r'^blog/view/(?P<slug>[-\w]+)/$', 'blog.views.view_post', name='view_blog_post'),
    url(r'^blog/view/(?P<slug>[-\w]+)/details/$', 'blog.views.view_details', name='view_blog_details'),
)