请帮我写正确的URL以便在主页上呈现上下文。 感谢在此获得的友好帮助,我得到了以下代码:
型号:
import datetime
from datetime import datetime, timedelta
from django.utils.translation import ugettext as _
from django.db import models
class DayOfWeekSchedule(models.Model):
“""Dynamic days of week"""
def days_of_week(self):
days = {
'1': _('Monday’),
'2': _('Tuesday’),
'3': _('Wednesday'),
'4': _('Thursday’),
'5': _('Friday'),
'6': _('Saturday'),
'7': _('Sunday')}
DOW_CHOICES = []
today = datetime.today()
for i in range(7):
day_number = (today + timedelta(days=i)).isoweekday()
day = days[str(day_number)]
DOW_CHOICES.append(day_number, day)
context = dict(DOW_CHOICES)
return context
视图:
import datetime
from datetime import *
from django.shortcuts import render
from django.views.generic.detail import DetailView
from .models import DayOfWeekSchedule
class DayOfWeekSchedules(DetailView):
model = DayOfWeekSchedule
template_name = 'schedule.html'
def get_context_data(self, **kwargs):
context = super(DayOfWeekSchedules, self).get_context_data(**kwargs)
context_a = self.object.my_dict()
return render(request, self.template_name, context)
URL:
path('<int:pk>/', DayOfWeekSchedules.as_view(), name='schedule’),
我得到http://127.0.0.1:8000/错误404。我想在127.0.0.1:8000而不是在127.0.0.1:8000/1/上看到我的页面。
我也非常感谢相关的阅读建议。