why i am getting no reverse match error after adding the url in home.html file?

Question:

I am getting the error after adding the URL in the home.html file. I migrated before running the program. I used commands to migrate. i.e., python manage.py migrate. 2. python manage.py makemigrations app_name. Django version I am using is 4.0.6. Thank you

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%22t^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_detail1"),
]


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",kwargs={"pk": 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.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 %}
 {% for T1 in con2%}

  <h4>{{T1.name}}</h4>]
  <h4>{{T1.address}}</h4>

 {% endfor %}
{% endblock context %}
</body>
</html>``
Asked By: Dhani

||

Answers:

In your home.html you are trying to use this link:

<h4><a href="{% url 'index_detail' %}">{{T1.name}}</a></h4>

But in your urls.py you have:

path("index/<int:pk>",V2.as_view(),name="index_detail1"),

you need to change your path in urls.py to be same name as in your home.html.

path("index/<int:pk>",V2.as_view(),name="index_detail"),
Answered By: oruchkin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.