Home > Blockchain >  why link not displaying content after click on the link?
why link not displaying content after click on the link?

Time:07-25

it is displaying content in home.html. but it is not displaying the content(T1.name and T1.address) in index_detail.html after click on T1.name. Django version 4.0.6. The code is below please have a look into it. please solve the issue. Thank you for your time.

Settings.py

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR=os.path.join(BASE_DIR,"templates")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-d4@4fwcpkk$6a8ylqjmyhp-9"t^aizetygwkxzw9s_wu5$&2'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'g6app',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'g6.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
    'context_processors': [
        'django.template.context_processors.debug',
        'django.template.context_processors.request',
        'django.contrib.auth.context_processors.auth',
        'django.contrib.messages.context_processors.messages',
    ],
    },
    },
]

urls.py

from django.contrib import admin
from django.urls import path
from g6app.views import V1,V2

urlpatterns = [
path('admin/', admin.site.urls),
path("",V1.as_view(),name="v11"),
path("index/<int:pk>",V2.as_view(),name="index_detail"),
]
  admin.py

from django.contrib import admin
from . models import T1
# Register your models here.
admin.site.register(T1)

  models.py

from django.db import models
from django.urls import reverse


# Create your models here.

    class T1(models.Model):
        name=models.CharField(max_length=100)
        address=models.TextField()
    

        def __str__(self):
            return self.name

        def get_absolute_url(self):
            return reverse("index_detail",args=[self.pk])
  

views.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import T1
# Create your views here.

class V1(ListView):
    model=T1
    context_object_name="con1"
    template_name="home.html"


class V2(DetailView):
    model=T1
    context_object_name="con2"
    template_name="index_detail.html"


  home.html

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
    <h1>home page</h1>
 {% block context %}
  {% for T1 in con1 %}
    <h4><a href="{% url 'index_detail' T1.pk %}">{{T1.name}}</a></h4>
    <h4>{{T1.address}}</h4>

  {% endfor %}
 {% endblock context %}
</body>
</html>

  index_detail.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
<body>

<h1>detail view page</h1>
{% block context %}
<h4>{{T1.name}}</h4>]
<h4>{{T1.address}}</h4>

{% endblock context %}
</body>
</html>

CodePudding user response:

Try to write con2.name and con2.address. Because you are in the index_detail page an you mentioned con2 in view not a T1

CodePudding user response:

The data is passed as object to the context, as as con2, so you render this with:

{% block context %}
<h4>{{ con2.name }}</h4>
<h4>{{ con2.address }}</h4>
{% endblock context %}

The variable name of home.html is thus not somehow reused: after rendering the template, the context is no longer available. If the user clicks on another link, then it will visit that view, and thus restart the rendering process with a new context.

  • Related