gpt4 book ai didi

python - DetailView 模板不显示其数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:43 27 4
gpt4 key购买 nike

在应用程序中,我有两个模型,名为“类(class)”和“步骤”。每个步骤都属于一个类(class),每个类(class)又包含许多步骤。但是,我在为步骤创建详细 View 时遇到问题。例如,当我转到 url ' http://127.0.0.1:8000/courses/1 时' 它应该显示 course.objects.get(pk=1) 的步骤。但是我返回的只是类(class)页面,即 http://127.0.0.1:8000/courses '。

型号

from django.db import models

# Create your models here.
class Course(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length= 255)
description = models.TextField()

def __str__(self):
return self.title

class Step(models.Model):
title = models.CharField(max_length = 255)
description = models.TextField()
order = models.IntegerField()
course = models.ForeignKey(Course)

def __str__(self):
return self.title

网址

from django.conf.urls import url
from . import views


urlpatterns = [
url(r'^$', views.course_list),
url(r'(?P<pk>\d+)/$', views.course_detail)
]

查看

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from .models import Course, Step

def course_list(request):
courses = Course.objects.all()
return render(request, 'courses/course_list.html', {'courses': courses})

def course_detail(request, pk):
course = Course.objects.get(pk=pk)
return render(request, 'courses/course_detail.html', {'course': course})

course_detail.html

{% extends 'layout.html' %}
{% block title %}{{course.title}}{% endblock %}
{% block content %}
<article>
<h2>{{ course.title }} %</h2>
{{course.description}}
<section>
{% for step in course.step_set.all %}
<h3>{{ step.title }}</h3>
{{step.description}}
{% endfor %}
</section>
</article>
{% endblock %}

主要网址

from django.conf.urls import url, include
from django.contrib import admin
from courses.views import course_list
from django.conf import settings
from django.conf.urls.static import static
from . import views

urlpatterns = [
url(r'^courses/', course_list),
url(r'^admin/', admin.site.urls),
url(r'^$', views.hello_world),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我似乎无法意识到我哪里出了问题

最佳答案

我认为您需要这些 url 映射..!这应该可以解决问题。

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^applicationName/$', views.ListView.as_view(), name='courses'),
url(r'^applicationName/(?P<pk>\d+)$', views.CourseDetailView.as_view(), name='course-detail'),
]

关于python - DetailView 模板不显示其数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44167420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com