【发布时间】:2020-03-27 23:05:08
【问题描述】:
我正在使用 Django 开发博客网站。
我有一些博客,我正在从我的数据库中将它们加载到卡片中。我的卡片有一个 阅读更多 按钮(转到新的 HTML 页面并动态获取特定博客的内容)。
我没有为每个博客创建一个新的 html 页面,而是使用单个 html 页面通过点击卡片上的阅读更多按钮来显示博客的内容。
但我收到以下错误:
未找到带有参数 '('',)' 的 'post_detail' 的反向。尝试了 1 种模式:['(?P[-a-zA-Z0-9_]+)\/$']
请求方法:GET
请求网址:http://127.0.0.1:8000/blogs/
Django 版本:2.2.6
异常类型:NoReverseMatch
异常值:
异常位置:C:\django\lib\site-packages\django\urls\resolvers.py in_reverse_with_prefix,第 673 行**
非常感谢
models.py
class blog(models.Model):
STATUS_CHOICES=(
("scholarship","Scholarship"),
("examination","Examination"),
("career","Career"),
("fellowship","Fellowship")
)
blog_image=models.ImageField(upload_to='blog_media',default="")
blog_title=models.CharField(max_length=300)
slug = models.SlugField( max_length=200, unique=True)
blog_type = models.CharField( max_length=50, choices=STATUS_CHOICES, default="scholarship" )
blog_author_name=models.CharField(max_length=200)
blog_content=models.CharField(max_length=5000)
publish_date=models.DateField()
urls.py:
urlpatterns = [
path('',views.index,name='home'),
path('blogs/',views.blogs,name='blogs'),
path('about/',views.about,name='about'),
path('admissions/',views.admissions,name='admissions'),
path( '<slug:slug>/', views.PostDetail.as_view(), name='post_detail' ),]
views.py
class PostDetail(DetailView):
#my model name is blog
model = blog
#this is the html page on which I want to show the single blog data
template_name = 'buddyscholarship_html/post_detail.html'
在 CardView 中以 html 格式从 Django 加载动态数据的代码:
{% for i in data %}
</div>
<div class="card-body">
<h5 class="card-title">{{i.blog_title}}</h5>
<p class="card-text">{{i.blog_type}}</p>
<p class="card-text">By:{{i.blog_author_name}}</p>
<p class="card-text">{{i.blog_content|truncatechars:100}}</p>
<h5 class="card-title">{{i.publish_date}}</h5>
<!-----read more button for each and every blog coming from the database---------->
<a href="{% url 'post_detail' blog.slug %}" class="btn btn-primary">Read More →</a></div>
</div>
</div>
{% endfor %}
【问题讨论】:
标签: django django-templates django-views