Search code examples
pythondjangodjango-templatescookiecutter-django

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!

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

but I am stuck at the part where I am trying to render the template from my new app called algo_explained.

I tried following the user app inside the sample project but no luck.

Here's the link to my project on github

Here's what I have so far:

App views

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

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

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

this is the error: enter image description here

I would appreciate any input!!


Solution

  • 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. 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.

    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: