如何从 django-cookiecutter 上的新应用程序呈现模板

How to render a template from a new app on django-cookiecutter

我在使用 Django-cookiecutter 从应用程序呈现模板时遇到问题!

我正在为我的项目使用 Django-cookiecutter,我正在尝试在我的项目中创建一个新的博客应用程序,我已经按照本教程完成了所有工作:Creating a blog part

但我卡在了我试图从名为 algo_explained 的新应用程序中呈现模板的部分。

我尝试在示例项目中关注用户应用程序,但没有成功。

Here's the link to my project on github

这是我目前的情况:

应用浏览量

explain_algorithms/explain_algorithms/algo_explained/views.py

from django.shortcuts import render
from explain_algorithms.algo_explained.models import Post, Comment
from explain_algorithms.algo_explained.forms import CommentForm

#blog_index will display a list of all your posts.

def blog_index(request):
    posts = Post.objects.all().order_by("-created_on")
    context = {
        "posts" : posts,
    }
    return render(request, "blog_index.html", context)

特定于应用程序 URL

explain_algorithms/explain_algorithms/algo_explained/urls.py

from django.urls import path
from . import views

app_name = "algo_explained"
urlpatterns = [
       path("blog", views.blog_index, name="blog_index"),
]

主要项目URL

explain_algorithms/config/urls.py

我有管理员和所有其他路线我只是想分享重要的东西!

urlpatterns = [
path("users/", include("explain_algorithms.users.urls", namespace="users")),
path("accounts/", include("allauth.urls")),

# Your stuff: custom urls includes go here
path("algo_explained/", include("explain_algorithms.algo_explained.urls", namespace = 
"algo_explained")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我在 templates/algo_explained/blog_index.html

里面有模板

这是错误: enter image description here

如有任何意见,我将不胜感激!

您是否将您的应用程序添加到设置文件?

您应该只将 appname 添加到您的设置文件中..

好的,经过几天的努力,我终于解决了这个问题。我将分享我是如何解决这个问题的,这可能会对其他人有所帮助。

我在 cookiecutter-Django 中配置我的新应用没有任何问题。 Follow this link if you need to figure out how to configure it the right way

此外,按照用户应用程序在您的主 urls.py 中配置您的 urs。

我需要做的更改是更明确地说明我的模板在项目中的位置。例如,这是我的:

def blog_index(request):
posts = Post.objects.all().order_by("-created_on")
context = {
    "posts" : posts,
}
return render(request, "blog_index.html", context)`

我将其更改为:

def blog_index(request):
posts = Post.objects.all().order_by("-created_on")
context = {
    "posts" : posts,
}
**return render(request, "algo_explained/blog_index.html", context)**

Look into these and see if you can solve your issue: