我正在学习Django,我想用日历和任务构建一个应用程序,其中每个任务都有一个单独的链接get_absolute_url
。我已经创建了一个活动列表和每个链接的url链接,但是当我选择其中一个时,没有任何反应,甚至没有错误消息。
请在下面找到我的代码。
from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager,self).get_queryset().order_by('hour')
class Activity(models.Model):
STATUS_CHOICES = (
('30 min', '30 min'),
('1:00 h', '1:00 h'),
('1:30 h', '1:30 h'),
('2:00 h', '2:00 h'),
('2:30 h', '2:30 h'),
('3:00 h', '3:00 h'),
('3:30 h', '3:30 h'),
('4:00 h', '4:00 h'),
('4:30 h', '4:30 h'),
('5:00 h', '5:00 h'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='hour')
body = models.TextField()
hour = models.DateTimeField(default=timezone.now())
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
objects = models.Manager()
duration = models.CharField(max_length=10, choices=STATUS_CHOICES, default='30 min')
published = PublishedManager()
class Meta:
ordering = ('-hour',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('core_tm:activity_detail',
args=[self.slug])
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.activity_list, name='activity_list'),
url(r'^(?P<activity>[-\w]+)/$',
views.activity_detail, name='activity_detail')
from django.shortcuts import render, get_object_or_404
from .models import Activity
def activity_list(request):
activities = Activity.published.all()
return render(request, 'core_tm/activity/list.html', {'activities' : activities})
def activity_detail(request, activity):
activity = get_object_or_404(Activity, slug=activity)
return render(request, 'core_tm/activity/detail.html', {'activity' : activity})
列表模板:
{% extends "core_tm/base.html" %}
{% block title %}<h1>Lista aktywności</h1>{% endblock %}
{% block content %}
{% for activnes in activities %}
<h2>
<a href="{{ activnes.get_absolute_url }}">
{{ activnes.title }}
</a>
</h2>
<p class="date">
Data rozpoczęcia : {{ activnes.hour }}
Czas trwania : {{ activnes.duration }}
</p>
{{ activnes.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
详细信息模板:
{% extends "core_tm/base.html" %}
{% block title %}{{ activity.title }}{% endblock %}
{%block content %}
<h1>{{ activity.title }}</h1>
<p class="date">
Data rozpoczęcia : {{ activity.hour }}
Czas trwania : {{ activity.duration }}
</p>
{{ activity.body|linebreaks }}
{% endblock %}
答案 0 :(得分:0)
您需要终止您的activity_list网址:
url(r'^$', views.activity_list, name='activity_list'),
否则,这将简单地匹配所有路径,包括那些应该指向您的活动的路径。