简体   繁体   English

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

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

I am having trouble with rendering a template from an app using Django-cookiecutter!我在使用 Django-cookiecutter 从应用程序渲染模板时遇到问题!

I am using Django-cookiecutter for my project and I am trying to create a new blog app in my project and I have kinda done everything following this tutorial: Creating a blog part我正在为我的项目使用 Django-cookiecutter,我正在尝试在我的项目中创建一个新的博客应用程序,并且我已经按照本教程完成了所有工作: 创建博客部分

but I am stuck at the part where I am trying to render the template from my new app called algo_explained.但我被困在我试图从名为 algo_explained 的新应用程序中渲染模板的部分。

I tried following the user app inside the sample project but no luck.我尝试在示例项目中关注用户应用程序,但没有运气。

Here's the link to my project on github这是我在 github 上的项目的链接

Here's what I have so far:这是我到目前为止所拥有的:

App views应用程序视图

explain_algorithms/explain_algorithms/algo_explained/views.py 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)

app-specific URL特定应用 URL

explain_algorithms/explain_algorithms/algo_explained/urls.py 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"),
]

main project URL主要项目URL

explain_algorithms/config/urls.py explain_algorithms/config/urls.py

I do have admin and all other routes I just wanted to share what's important!我确实有管理员和所有其他路线,我只是想分享什么是重要的!

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)

and I do have the templates inside templates/algo_explained/blog_index.html我确实有模板内的模板/algo_explained/blog_index.html

this is the error: enter image description here这是错误:在此处输入图像描述

I would appreciate any input!!我将不胜感激任何意见!

did you added your application to settings files?您是否将应用程序添加到设置文件中?

You should just add appname to your setting files..您应该只将 appname 添加到您的设置文件中。

Ok people after days of struggle I was able to solve this problem.好吧,经过几天的努力,我能够解决这个问题。 I am going to share how I solved this which might help someone else.我将分享我如何解决这个问题,这可能对其他人有所帮助。

I didn't have any problem configuring my new app in cookiecutter-Django.我在 cookiecutter-Django 中配置我的新应用程序没有任何问题。 Follow this link if you need to figure out how to configure it the right way 如果您需要弄清楚如何以正确的方式配置它,请点击此链接

in addition, follow the User app to configure your urs in your main urls.py.此外,请按照用户应用程序在您的主 urls.py 中配置您的 urs。

Changes that I needed to make was to be more explicit about where my template was inside the project.我需要做的更改是更明确地说明我的模板在项目中的位置。 For example, this is what I had:例如,这就是我所拥有的:

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

and I changed it to:我将其更改为:

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: 看看这些,看看你是否能解决你的问题:

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

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