简体   繁体   中英

i can't create a post_detail and post_list

i am a error to create my list_posts and detail_post ! my code at 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:

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:

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 :

<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 :

{% 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:

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
         %}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM