I can't see my gabarit on my templates

Hello,
i’m learning django, but i have some issues. i can’t see my gabarit variable on my template. could you please help me to understand what’s the matter.

Here’s my views

from django.shortcuts import get_object_or_404
from django.shortcuts import render
# from .forms import SignUp
from .models import Article

# Create your views here.


# We return blog homepage
def index(request):
    return render(request, "blog/index.html")


def getarticle(request):
    article = get_object_or_404(Article, pk=1)
    return render(request, "blog/index.html", context={"article_N2": article})

here’s my models

from django.contrib.auth.models import User
from django.db import models


# Create your models here.
User

class Category(models.Model):
    name = models.CharField(max_length=36)
    slug = models.SlugField()

class Article(models.Model):
    # CASCADE we delete article if author is deleted
    author = models.ForeignKey(User, on_delete=models.PROTECT, null=True)
    category = models.ManyToManyField(Category)
    # title with limit
    titre = models.CharField(max_length=100)
    # We have title in field
    slug = models.SlugField()

Here’s my template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>blog sareham</title>
</head>
<body>

    {{ article_N2.titre }}

    {{"Bonjour"}}

</body>
</html>

Please help me to see where i made error because i’ve any error message but i can’t see my data on my template.

Kind regards

A complementary information :

when i launch my server, i can see the word “Bonjour”, but i can’t see the title as you can see the picture :
website

Thanks for your help

You would see these results if you’re executing the index view.

What url are you issuing that is giving you these results?

What is the content of your urls.py file?

Hello KenWhitsell,

here is my URL contents for the blog application :


# variable qui va contenir les liens du blog
from django.urls import path
from .views import index
from .views import getarticle

urlpatterns = [
    #  blog homepage
    path('', index, name="blog-index"),

]

when i call this url on my browser i have this :
image_2022-09-05_230946992

Here’s below, the main url of my website

"""sareham URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from .views import index
from .views import signup

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
    path('blog/', include("blog.urls")),
]

So you are calling your view named index. You’re never executing your view named getarticle.

Hello KenWhitesell,
i’ve created a new path to call my view getarticle but the result is the same

# link to blog
from django.urls import path
from .views import index
from .views import getarticle

urlpatterns = [
    #  blog homepage
    path('', index, name="blog-index"),
    path('', getarticle),
]

You haven’t created a new path - you’ve got the same path defined for both of your views.

I suggest you review the docs at URL dispatcher | Django documentation | Django, particularly the section on How Django processes a request.

Thanks KenWhitesell i’m going to read this part of documentation.
Kind regards

Hello ,
really thanks for your help, i can now display my data in my html page.

i didn’t understand that i can call a lot of data in my context and give just one url to show my page.
Here is the new code :

1. My view :

from django.shortcuts import get_object_or_404
from django.shortcuts import render
# from .forms import SignUp
from .models import Article


# Create your views here.
def getarticle():
    article = get_object_or_404(Article, pk=1)
    return article


# We return blog homepage
def index(request):
    article = getarticle()
    article2 = get_object_or_404(Article, pk=1)
    return render(request, "blog/index.html",
                  context={"article": article,
                           "surname": "Francis",
                           })

2. My URL

# link to blog
from django.urls import path
from .views import index, getarticle


urlpatterns = [
    #  blog homepage
    path('', index, name="blog-index"),
]

3. My template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>blog sareham</title>
</head>
<body>


    {{ article.titre }}</br>
    {{ surname }}


</body>
</html>

the result
website