找不到带有关键字参数'{'slug':''}''的'detail'。尝试了1个模式:['posts /(?P <slug> [-\\ w] +)/ $']

时间:2019-09-29 15:19:53

标签: django python-3.x django-urls slug

当我尝试访问显示所有帖子的主页时,出现此错误

它告诉我模板中此行中的错误:

=“ https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js”完整性=“ sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf / nJGzIxFDsf4x0xIM + B07jRMous” =“

base.html:

html模板:

{对象中i中%的%}                 

            <div class="card" style="width: 18rem;">
              <div class="card-body">
                  {% if i.cover %}
                 <img src="{{ i.cover.url}}" class="img-fluid" >
                  {% endif %}
                <h4 class="card-title">{{i.title}} <small style="font-size: small;"> {{i.timestamp | timesince}}</small></h4>
                <p class="card-text">{{i.content | linebreaks | truncatechars:120}}</p>
               <a href="{{i.get_absolute_url}}" class="btn btn-outline-dark">view</a> <a href="{{i.get_absolute_url}}edit" class="btn btn-outline-info">edit</a>
              </div>
            </div> <br>
            {% cycle '' '' '</td></tr><tr>' %}
            {%endfor%}

urls.py:

urlpatterns = [

url(r'^create/$', views.posts_create),
url(r'^$', views.posts_list, name="list"),
url(r'^(?P<slug>[-\w]+)/$', views.posts_detail, name="detail"),
url(r'^(?P<ID>\d+)/edit/$', views.posts_update, name="update"),
url(r'^(?P<ID>\d+)/delete/$', views.posts_delete),

]

models.py:

class Post (models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
slug = models.SlugField(unique=True, default='-')
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
cover = models.ImageField(null=True, blank=True, upload_to="media/")

def __str__(self):
    return self.title

def get_absolute_url(self):
    return reverse("posts:detail", kwargs={'slug':self.slug})

views.py:

def posts_detail(request, slug):
object = get_object_or_404(Post,slug=slug)
context = {'title': object.title, 'object': object}
return render(request, 'post_details.html', context)

0 个答案:

没有答案
相关问题