简体   繁体   English

我无法创建post_detail和post_list

[英]i can't create a post_detail and post_list

i am a error to create my list_posts and detail_post ! 创建我的list_posts和detail_post时出错! my code at list_posts : 我在list_posts上的代码:

def blog(request):
list_posts = Post.objects.filter(status = 'published').order_by('-published_at')[:]
context = {
    'list_posts':list_posts
}
return render(request, 'mysite/doc_2.html', context)

and my code at detail_post: 和我的代码在detail_post:

edef detail(request, year, month, day,slug):
post = get_object_or_404(Post, status='published', 
                                published_at__year=year, 
                                published_at__month=month, 
                                published_at__day=day,
                                slug=slug)
context = {
    'post':post
}
return render(request, 'mysite/ws-post.html', context)

and my code in my app_name.urls: 和我的代码在我的app_name.urls中:

re_path(r'^(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})/(?P<slug>[-\w]+)$/',views.detail,name='detail'),

and my code in url's my html list_posts is : 和我在url的我的HTML list_posts中的代码是:

<a class="l-ws-more" href="{% url 'blog:detail' year=post.published_at.year month=post.published_at.month day=post.published_at.day %}">

and my error is : 我的错误是:

NoReverseMatch at /blog/
Reverse for 'detail' with keyword arguments '{'year': '', 'month': '', 
'day': ''}' not found. 1 pattern(s) tried: ['blog/(?P<year>\\d{4})-(? 
P<month>\\d{1,2})-(?P<day>\\d{1,2})/(?P<slug>[-\\w]+)$/']

my template list_posts : 我的模板list_posts:

{% if list_posts %}
          {% for posts in list_posts %}
            <div class="doc-item-2">
            <div class="doc-intro-2 dark_unhover">
              <figure class="effect-oscar">
                <img src="{{ posts.banner.url }}" alt="{{ posts.title }}">
                <figcaption>
                  <h2>Warm <span>Oscar</span></h2>
                </figcaption>
              </figure>
              <div class="rate-star">
                <span class="fa fa-star star-checked"></span>
                <span class="fa fa-star star-checked"></span>
                <span class="fa fa-star star-checked"></span>
                <span class="fa fa-star"></span>
                <span class="fa fa-star"></span>
              </div>
            </div>
            <div class="doc-content-2">
              <h3>{{ posts.title }}</h3>
              <div class="info_blue">
                <div class="date_1">
                  <p>ساعت</p>
                  <span>{{ posts.published_at|jalali_time }}</span>
                </div>
                <div class="date_2">
                  <p>تاریخ</p>
                  <!-- <span>{{ posts.published_at|date:"Y D M" }}</span> -->
                  <span>{{ posts.published_at|jalali_date }}</span>
                </div>
              </div>
              <p class="dec">{{ posts.summary }}</p>
              <a class="l-ws-more" href="{% url 'blog:detail' year=post.published_at.year month=post.published_at.month day=post.published_at.day %}">
              ادامه مطلب</a>
            </div>
            </div>
            {% endfor %}
            {% else %}
            <h2>no post!</h2>
            {% endif %}

please answer my question and error's pictures ==> enter image description here 请回答我的问题和错误的图片==> 在此处输入图片描述

Your path expects 4 arguments, including a slug at the end: 您的路径需要4个参数,最后包括一个参数:

re_path(r'^(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})/
 (?P<slug>[-\w]+) # slug
$/',views.detail,name='detail'),

But in your template it is only passed year, month, and day, no slug: 但是在您的模板中,仅通过了年,月和日,没有任何问题:

href="{% url 'blog:detail' 
         year=post.published_at.year 
         month=post.published_at.month 
         day=post.published_at.day 
         # slug missing here!: slug=post.slug
         %}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoReverseMatch at / Reverse for &#39;post_detail&#39; 未找到 - NoReverseMatch at / Reverse for 'post_detail' not found 评论未显示在post_detail视图中 - Comments not showing in post_detail view post_detail不显示图像 - post_detail doesnt display image 未找到“post_detail”的反转。 “post_detail”不是有效的视图函数或模式名称 - Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name 如何在 django 中没有 post_detail 页面的 django 评论表单中获取“post id”(与评论模型相关的外键字段) - how can i get the “post id”(foreign key field connected with comment model) to the django comments form without post_detail page in django NoReverseMatch at /post/new Reverse for &#39;post_detail&#39; 未找到。 “post_detail”不是有效的视图函数或模式名称 - NoReverseMatch at /post/new Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name post_detail()视图缺少必需的位置参数: - post_detail() view missing required positional arguments: post_list() 缺少 1 个必需的位置参数:“请求” - post_list() missing 1 required positional argument: 'request' 未找到带有参数 &#39;(&#39;&#39;,)&#39; 的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;post/(?P<slug> [-\\\\w]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<slug>[-\\w]+)/$'] 反向使用关键字 arguments '{'pk': '', 'article_id': ''}' not found - Reverse for 'post_detail' with keyword arguments '{'pk': '', 'article_id': ''}' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM