我遇到URl映射问题。我希望帖子的标题位于网址中,并且在尝试执行此操作时出现NoReverseMatch错误。
这是app / urls.py文件
from django.conf.urls import url
from .views import PostsListView, PostDetailView
app_name = 'era'
urlpatterns = [
url(r'^$', PostsListView.as_view(), name='posts_list'),
url(r'^post/(?P<title>[\w-]+)/$', PostDetailView.as_view,
name='post_details')
]
应用程序/ views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
class PostsListView(ListView):
model = Post
class PostDetailView(DetailView):
model = Post
context_object_name = 'post_details'
template_name = 'era/post_details.html'
最后是era / post_list.html
{% extends 'era/base.html' %}
{% block content %}
{% for post in post_list %}
<div class="post-block" style="width: 27%;float: left; margin: 3%; background-color: rgba(169, 169,169,0.5); padding: 2%; border-radius: 15px">
<h3>{{ post.title }}</h3>
<br>
{{ post.image_title }}
<br>
{{ post.short_description }}
<br><br>
<a href="{% url 'era:post_details' title=post.title %}" class="btn btn-primary" style="float: right">Read more</a>
</div>
{% endfor %}
{% endblock %}
非常感谢您的帮助!