簡體   English   中英

如何從 django-cookiecutter 上的新應用程序呈現模板

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

我在使用 Django-cookiecutter 從應用程序渲染模板時遇到問題!

我正在為我的項目使用 Django-cookiecutter,我正在嘗試在我的項目中創建一個新的博客應用程序,並且我已經按照本教程完成了所有工作: 創建博客部分

但我被困在我試圖從名為 algo_explained 的新應用程序中渲染模板的部分。

我嘗試在示例項目中關注用戶應用程序,但沒有運氣。

這是我在 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)

我確實有模板內的模板/algo_explained/blog_index.html

這是錯誤:在此處輸入圖像描述

我將不勝感激任何意見!

您是否將應用程序添加到設置文件中?

您應該只將 appname 添加到您的設置文件中。

好吧,經過幾天的努力,我能夠解決這個問題。 我將分享我如何解決這個問題,這可能對其他人有所幫助。

我在 cookiecutter-Django 中配置我的新應用程序沒有任何問題。 如果您需要弄清楚如何以正確的方式配置它,請點擊此鏈接

此外,請按照用戶應用程序在您的主 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)**

看看這些,看看你是否能解決你的問題:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM