在urls.py中未检测到我的Django DayArchiveView

时间:2018-04-04 13:54:05

标签: python django django-views

我在Django中创建了几个基于日期的视图,虽然年和月的视图按预期运行,但未检测到显示天数的视图。例如,如果我尝试获取<input type="text" class="input" placeholder="Your name goes here" id="setUserNameInput"> <input type="button" class="button" value="Set your username" onclick="setUserName()" /> <input type="button" class="button" value="Display on click" onclick="displayUserName()" /> <br> So you shall be called <span class="displayUserName"></span>! But dont worry, <span class="displayUserName"></span>, it will be all fine./balance/2018/,则会显示视图,而/balance/2018/04/将失败。我知道问题必须是urls.py配置,但我在文档中找不到它。我也尝试将day_format ='%d'传递给as_view方法,没有任何结果。

我的urls.py文件

/balance/2018/04/02

my views.py file

from django.urls import path, re_path
from .views import ArticleYearArchiveView, ArticleMonthArchiveView, ArticleDayArchiveView
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path(r'<int:year>/<int:month:>/<int:day>/', views.ArticleDayArchiveView.as_view(day_format='%d'), name='show_day'),
    path(r'<int:year>/<int:month>/', views.ArticleMonthArchiveView.as_view(month_format='%m'), name='show_month'),
    path(r'<int:year>/', views.ArticleYearArchiveView.as_view(), name='show_year'),
]

错误文字是:

from django.shortcuts import render
from django.views.generic.dates import YearArchiveView, MonthArchiveView, DayArchiveView
from django.http import HttpResponse
from django.db.models import Sum
from .models import Category, Article
import datetime
# Create your views here.

def index(request):

    num_of_articles = Article.objects.all().count()
    num_of_categories = Category.objects.all().count()
    return render(request, 'index.html', context = {
    'num_of_articles':num_of_articles,
    'num_of_categories':num_of_categories})

class ArticleYearArchiveView(YearArchiveView):
    queryset = Article.objects.all()
    date_field = 'financial_day'
    make_object_list = True

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['summation'] = Article.objects.all().filter(financial_day__year=kwargs['year'].year).aggregate(Sum('amount_of_money'))['amount_of_money__sum']
        return context

class ArticleMonthArchiveView(MonthArchiveView):
    queryset = Article.objects.all()
    date_field = 'financial_day'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['summation'] = Article.objects.all().filter(financial_day__year=kwargs['month'].year).filter(financial_day__month=kwargs['month'].month).aggregate(Sum('amount_of_money'))['amount_of_money__sum']
        return context


class ArticleDayArchiveView(DayArchiveView):
    queryset = Article.objects.all()
    date_field = 'financial_day'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['summation'] = Article.objects.all().filter(financial_day__year=kwargs['day'].year).filter(financial_day__month=kwargs['day'].month).filter(financial_day__day=kwargs['day'].day).aggregate(Sum('amount_of_money'))['amount_of_money__sum']
        return context

#Here it'll show ARTICLE DETAILS for a given SLUG url
def show_article(request, article_slug):
    response = "You are looking at the article %s."
    return HttpResponse(response % article_slug)

1 个答案:

答案 0 :(得分:2)

月后你有一个迷路: - <int:month:>。删除它:

path(r'<int:year>/<int:month>/<int:day>/', views.ArticleDayArchiveView.as_view(day_format='%d'), name='show_day'),

另请注意,您正在定义balance/2018/04/02/的URL(带有斜杠),但您将转到http://localhost:8000/balance/2018/04/02(没有斜杠)。一旦你修正了上面的拼写错误,Django应该将你重定向到带有斜杠的URL。