开发一个具有课程,课程和情节模型的项目。在剧集模型中有视频。但是问题是当我使用generic.TemplateView呈现模板时,模板可以工作,但是当我使用generic.DetailView时,错误提示“找不到页面(404)”。
无法理解该错误。
urls.py
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
type ParamsOne struct {
Username string `json:"username"`
}
type ParamsTwo struct {
Username string `json:"username"`
}
func first(c *gin.Context) {
c.Set("variable", "foo")
c.Next()
}
func second(c *gin.Context) {
v := c.MustGet("variable")
log.Printf("in middleware %s", v)
c.Next()
}
func main() {
r := gin.New()
r.Use(first)
r.Use(second)
r.POST("/", func(c *gin.Context) {
var f ParamsOne
if err := c.ShouldBindBodyWith(&f, binding.JSON); err != nil {
log.Printf("%+v", err)
}
log.Printf("%+v", f)
var ff ParamsTwo
if err := c.ShouldBindBodyWith(&ff, binding.JSON); err != nil {
log.Printf("%+v", err)
}
log.Printf("%+v", ff)
v := c.MustGet("variable")
log.Printf("in route %s", v)
c.IndentedJSON(http.StatusOK, f)
})
r.Run(":4000")
}
views.py,当 TemplateView
example$ ./example
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] POST / --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :4000
2020/07/05 13:11:06 in middleware foo
2020/07/05 13:11:06 {Username:somename}
2020/07/05 13:11:06 {Username:somename}
2020/07/05 13:11:06 in route foo
做得好。
DetailView 时为views.py
from django.urls import path
from .views import (
CourseDetailsView,
LessonEpisodeVideoView,
)
urlpatterns = [
path('course_detail/<slug:slug>/', CourseDetailsView.as_view(), name='course-detail'),
path('course/detail/video/<slug:slug>/', LessonEpisodeVideoView.as_view(), name='episode-video'),
]
但是将 TemplaeView 更改为 DetailView 时,出现了错误!
在HTML超链接标记中:
class LessonEpisodeVideoView(TemplateView, LoginRequiredMixin):
model = Courses
template_name = 'courses/course_details.html'