mar24n
mar24n

Reputation: 133

Django no reverse match for view details

im stuck on show details for object, no matter i do (following all guides on internet ) still getting reverse not match error

views.py

def val_details(request, id):

    val = Validator.objects.get(id=id)
    print(f'vali: {val}')

    context = dict(val=val)

    return render(request, 'users/val_details.html', context)

print(f'vali: {val}') printing vali: Validator object (14)

html

<button class="btn btn-warning " href="{% url 'val-details' val.id %}">detals</button>

urls.py

path('dashboard/validator/<int:id>/', user_views.val_details, name='val-details'),

models.py

class Validator(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=200, blank=True, null=True)
    address = models.CharField(max_length=200, blank=True, null=True)
    owner = models.CharField(max_length=250, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __int__(self):
        return self.id

error django.urls.exceptions.NoReverseMatch: Reverse for 'val-details' with arguments '('',)' not found. 1 pattern(s) tried: ['users/validator/(?P<id>[0-9]+)/$']

profile view

def profile(request):

    valid = Validator.objects.filter(user=request.user)
    valid_count = valid.count()

    context = {
        'valid': valid,
        'valid_count': valid_count,
    }
    return render(request, 'users/profile.html', context)

and urls.py

from django.urls import path
from users import views as user_views

urlpatterns = [
    path('dashboard/', user_views.profile, name='dashboard'),
    path('dashboard/validator/<int:id>/', user_views.val_details, name='val-details'),

]

Upvotes: 0

Views: 84

Answers (1)

Razenstein
Razenstein

Reputation: 3717

this is the typical error message if in your

<button class="btn btn-warning " href="{% url 'val-details' val.id %}">detals</button>

val.id is either NULL or empty.

Please check where you assign it.

Upvotes: 1

Related Questions