嵌套的Urls抛出404错误

时间:2017-07-27 11:33:20

标签: python django

我已经建立了一个指南网站。 每个指南都由步骤组成。 每个步骤都由任务组成。

要进行下一步,您需要完成任务,然后再完成下一步,直到没有更多步骤。

示例视图和网址:

/1应该向您展示guide1

/1/3应该显示guide1的第3步

/1/3/5应该在guide1

中显示第3步的task5

我希望网址如下:/1/3/6(guide1 / step3 / task6)

当我去/1时,我开始工作了。然后它显示了指南的视图。 但是,当我转到/1/11/1/1时,我收到404错误,指出该指南不存在。

我需要帮助。这是我的代码:

适用于应用的Urls.py

urlpatterns = [
    url(r'(?P<guide_id>[0-9]+)/$', views.taskoftheday, name="taskoftheday"),
    url(r'(?P<guide_id>[0-9]+)/(?P<step.sequence_num>[0-9]+)/$', views.taskoftheday_step, name="taskoftheday_step"),
    url(r'(?P<guide_id>[0-9]+)/(?P<step.sequence_num>[0-9]+)/(?P<task.sequence_num>[0-9]+)/$', views.taskoftheday_task, name="taskoftheday_task"),
]

项目的urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('registration.urls')),
    url(r'^taskoftheday/', include('taskoftheday.urls')),
    url(r'^', include('taskoftheday.urls')),
    url(r'^analytics/', include('analytics.urls')),
    url(r'^progress/', include('progress.urls')),
    url(r'^goals/', include('goals.urls')),
    url(r'^registration/', include('registration.urls')),
    url(r'thanks/', views.thanks),
    url(r'thanks_again/', views.thanks_again),
    url(r'^$', views.landing_page),
]

models.py

class Guide(models.Model):
    name = models.CharField(max_length=200)
    guide_category = models.CharField(max_length=70)
    guide_why = models.TextField()
    guide_how = models.TextField()
    is_complete = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

class Step(models.Model):
    guide = models.ForeignKey(Guide, on_delete=models.CASCADE)
    sequence_number = models.PositiveIntegerField(default=1)
    name = models.CharField(max_length=10)
    is_complete = models.BooleanField(default=False)

    class Meta:
        unique_together = ("guide", "sequence_number")

    def __unicode__(self):
        return self.name

class Task(models.Model):
    step = models.ForeignKey(Step, on_delete=models.CASCADE)
    sequence_number = models.PositiveIntegerField(default=1)
    name = models.CharField(max_length=10)
    task_img = models.ImageField()
    task_task = models.TextField()
    task_description = models.TextField()
    is_complete = models.BooleanField(default=False)

    class Meta:
        unique_together = ("step", "sequence_number")

    def __unicode__(self):
        return self.name

views.py

def taskoftheday(request, guide_id):
    try:
        guide = Guide.objects.get(pk=guide_id)
    except Guide.DoesNotExist:
        raise Http404("Guide does not exist")
    return render(request, 'taskoftheday/taskoftheday.html', {'guide': guide})

def taskoftheday_step(request, guide_id, step_id):
    try:
        guide = Guide.objects.get(pk=guide_id)
        step = Step.objects.get(guide=guide, sequence_number=step_id)
    except Step.DoesNotExist:
        raise Http404("Step does not exist")
    return render(request, 'taskoftheday/taskoftheday_step.html', {'step': step})

def taskoftheday_task(request, guide_id, step_id, task_id):
    try:
        guide = Guide.objects.get(pk=guide_id)
        step = Step.objects.get(guide=guide, sequence_number=step_id)
        task = Task.objects.get(step=step, sequence_number=task_id)
    except Task.DoesNotExist:
        raise Http404("Task does not exist")
    return render(request, 'taskoftheday/taskoftheday_task.html', {'task': task})

0 个答案:

没有答案