Detail View of a Object

hello everyone, i am trying to understand the logic of detail view and made a dummy project, i made it before with a tutorial but now it is not working no matter i’ve done. Here is my code:

views.py

from django.shortcuts import render
from .forms import SeçimForm
from .models import Seçim

# Create your views here.

def index(request):
    adam = Seçim.objects.all().order_by("-id")
    if request.method == "POST":
        form = SeçimForm(request.POST, request.FILES)
        if form.is_valid():
            yeni_seçim = form.save(commit=False)
            yeni_seçim.user = request.user
            yeni_seçim.save()
    else:
        form = SeçimForm(data=request.GET)
    return render(request, 'selectapp/index.html',{
        'form':form,
        'adam':adam
    })


def detail(request, item_id):
    seçim = Seçim.objects.get(pk=item_id)
    context = {
        "Seçim":seçim,
    }
    return render(request, 'selectapp/detail.html', context)


urls.py:

from django.urls import path
from selectapp import views

app_name = 'selectapp'

urlpatterns =[
    path('',views.index, name='index'),
    path('<int:item_id>/', views.detail, name='detail'),
]

models.py:

from django.db import models
from django.urls import reverse
from django.utils.text import slugify

# Create your models here.

class Seçim(models.Model):

    def __str__(self):
        return self.user

    user = models.CharField(max_length=50, blank=False)
    adam = models.CharField(max_length=50, blank=False)

    def get_absolute_url(self):
        return reverse("selectapp:detail", kwargs={"pk":self.pk})

forms.py:

from django import forms
from .models import Seçim

demo_seçimler = (
    ("Taha","Taha"),
    ("Safa","Safa"),
    ("Deniz","Deniz"),
    ("Furkan","Furkan"),
    ("Eray","Eray"),
    ("Selim","Selim"),
    ("Sezer","Sezer"),
)

class SeçimForm(forms.ModelForm):
    adam = forms.MultipleChoiceField(label="adamlar", choices = demo_seçimler, required= False)
    class Meta:
        model=Seçim
        fields = ('adam',)

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>document</title>
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <style>
        #id_adam{
            width: 50%;
        }
    </style>
</head>
<body>
    <form method="POST"> 
        {% csrf_token %} 
        {{ form.as_p }} 
        <input type="submit" value="Submit"> 
    </form> 

    <h4>Kişiler</h4>
    
    {% for kişi in adam %}
    <div class="card">
       <a href="{% url 'selectapp:detail' Seçim.id %}"> {{ kişi.adam }} </a>
    </div>
        </br>
    {% endfor %}

    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#id_adam').select2();
        });
    </script>

</body>
</html>

it is keep giving:

NoReverseMatch at /
Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<item_id>[0-9]+)/\\Z']

thank you :slight_smile:

I think that you mispelled this variable name.

There is no Seçim in your context, so i think that you wanted to refer to the kişi.id variable instead:

    {% for kişi in adam %}
    <div class="card">
      <!-->The only line that changed is the below one</!-->
       <a href="{% url 'selectapp:detail' kişi.id %}"> {{ kişi.adam }} </a>
    </div>
        </br>
    {% endfor %}

1 Like

thank you so much i can’t believe i didn’t see that. i was about to give up :slight_smile: