简体   繁体   English

Django - URLS映射和调度程序的问题

[英]Django - problems with URLS mapping and dispatcher

I'm learning Django and I would like to build an application with the calendar and tasks, where every task has a separate link via get_absolute_url . 我正在学习Django,我想用日历和任务构建一个应用程序,其中每个任务都有一个单独的链接,通过get_absolute_url I've created a list of activities and url link to each of them, but when I pick one of them there is no reaction, not even an error message. 我已经创建了一个活动列表和每个链接的url链接,但是当我选择其中一个时,没有任何反应,甚至没有错误消息。

Please find my code below. 请在下面找到我的代码。

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})

List template: 列表模板:

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

Detail template: 细节模板:

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

You need to terminate your activity_list URL: 您需要终止您的activity_list网址:

url(r'^$', views.activity_list, name='activity_list'),

Otherwise this will simply match all paths, including those that are supposed to point to your activities. 否则,这将简单地匹配所有路径,包括那些应该指向您的活动的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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