Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

python - DayArchiveview is not detected in urls.py(Django)

I created several date-based views in Django, and while views for year and month function as expected, the view to display days is not detected. For instance, if I try to get http://127.0.0.1:8000/blog/archive/2021/, or http://127.0.0.1:8000/blog/archive/2021/01 the views will be displayed, http://127.0.0.1:8000/blog/archive/2021/01/07 will fail. I understand that the problem must be with urls.py configuration, but I just cannot find it in documentation. I also tried passing day_format='%d' to as_view method, without any results.

urls.py

from django.urls import path, re_path

from blog import views

app_name = 'blog'

urlpatterns = [
    # Example: /blog/
    path('', views.PostListView.as_view(), name='index'),

    # Example: /blog/post/ (same as /blog/)
    path('post/', views.PostListView.as_view(), name='post_list'),

    # Example: /blog/post/django-example/
    re_path(r'^post/(?P<slug>[-w]+)/$', views.PostDetailView.as_view(), name='post_detail'),

    # Example: /blog/archive/
    path('archive/', views.PostArchiveView.as_view(), name='post_archive'),

    # Example: /blog/archive/2019/
    path('archive/<int:year>/', views.PostYearArchiveView.as_view(), name='post_year_archive'),

    # Example: /blog/archive/2019/nov/
    path('archive/<int:year>/<int:month>/', views.PostMonthArchiveView.as_view(month_format = '%m'), name='post_month_archive'),

    # Example: /blog/archive/2019/nov/10/
    path('archive/<int:year>/<int:month>/<int:day>/', views.PostDayArchiveView.as_view(), name='post_day_archive'),

    # Example: /blog/archive/today/
    path('archive/today/', views.PostTodayArchiveView.as_view(), name='post_today_archive'),
]

views.py

from django.shortcuts import render

# Create your views here.
from django.views.generic import ListView, DetailView, ArchiveIndexView, YearArchiveView, MonthArchiveView, 
    DayArchiveView, TodayArchiveView

from blog.models import Post


class PostListView(ListView):
    model = Post
    template_name = 'blog/post_all.html'
    context_object_name = 'posts'
    paginate_by = 2


class PostDetailView(DetailView):
    model = Post


class PostArchiveView(ArchiveIndexView):
    model = Post
    date_field = 'modify_dt'


class PostYearArchiveView(YearArchiveView):
    model = Post
    date_field = 'modify_dt'
    make_object_list = True


class PostMonthArchiveView(MonthArchiveView):
    model = Post
    date_field = 'modify_dt'


class PostDayArchiveView(DayArchiveView):
    model = Post
    date_field = 'modify_dt'
    day_format = '%d'


class PostTodayArchiveView(TodayArchiveView):
    model = Post
    date_field = 'modify_dt'

post_archive_day.html

<h1>Post Archives for {{ day|date:"N d, Y" }}</h1>

<div>
    <ul>
        {% for post in object_list %}
        <li>
            {{ post.modify_dt|date:"Y-m-d" }}&nbsp;&nbsp;&nbsp;
            <a href="{{ post.get_absolute_url }}"><strong>{{ post.title }}</strong></a>
        </li>
        {% endfor %}
    </ul>
</div>

error enter image description here

question from:https://stackoverflow.com/questions/65650225/dayarchiveview-is-not-detected-in-urls-pydjango

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

URL you are constructing is not in right format, it should be in format for example

/2021/jan/30/

as default month_format = '%b'

%b | Month, textual, 3 letters, lowercase.  'jan'

But instead of %b for month you are using %m


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...