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 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 :
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")),
]
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),
]