簡體   English   中英

我無法創建post_detail和post_list

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

創建我的list_posts和detail_post時出錯! 我在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)

和我的代碼在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)

和我的代碼在我的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'),

和我在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 %}">

我的錯誤是:

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]+)$/']

我的模板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 %}

請回答我的問題和錯誤的圖片==> 在此處輸入圖片描述

您的路徑需要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'),

但是在您的模板中,僅通過了年,月和日,沒有任何問題:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM