Search code examples
pythondjangowebdjango-viewsdjango-urls

I always face this problem Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<post_id>[0-9]+)/\\Z']


I am Malek I am working on a blog using django

but I am facing this problem with the url and pk

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

I hope someone can help me solve this problem

enter image description here

here are my django codes for each file

views.py

from django.shortcuts import render, get_object_or_404
from .models import AboutUs, Name, Skills, Portfolio, Navlink, Contact, Blog
from django.http import HttpResponse
# Create your views here.


def index(request):
    if request.method == "POST":
        contact = Contact()
        user_name = request.POST.get('name')
        email = request.POST.get('email')
        subject = request.POST.get('subject')

        contact.name = user_name
        contact.email = email
        contact.message = subject
        contact.save()
        return render(request, 'sent.html', context={'link': Navlink.objects.first()})
        
    return render(request, 'index.html', context= {
        'about_us': AboutUs.objects.first(),
        'name': Name.objects.first(),
        'skills': Skills.objects.all(),
        'image' : Portfolio.objects.all(),
        'link': Navlink.objects.all(),
        'blog': Blog.objects.all(),
        })



def post_details(request, id):
    post = get_object_or_404(Blog, pk= id)
    details = {
        'post_id' : post,
        'title' : 'title',
    }
    return render(request, 'post.html', details)

def home(request):
    return render(request, 'index.html')


def posts(request):
    return render(request, 'posts.html', context={'posts': Blog.objects.all()})
"""def post(request):
    return render(request, 'product.html')"""

urls.py

from django.urls import path
from . import views

urlpatterns = [
    #path('', views.home, name='home'),
    path('', views.index, name='home'),
    path('', views.home, name='home'),
    path('post/<int:post_id>/', views.post_details, name='detail'),
    path('posts/', views.posts, name='posts'),
]

models.py

from django.db import models
from django import forms

# Create your models here.


class AboutUs(models.Model):
    content = models.TextField()

    def __str__(self):
        return self.content[:50] + '...'
    
#-----------------------------------------------------------------

class Name(models.Model):
    name = models.CharField(max_length=15)

    def __str__(self):
        return self.name
    
#-----------------------------------------------------------------

class Skills(models.Model):
    levels = [
        ('Beginner', 'Beginner'),
        ('Good', 'Good'),
        ('Skillful', 'Skillful'),
        ('Professional', 'Professional'),
        ('Expert', 'Expert'),
    ]

    icon = models.ImageField(upload_to='photos/%y/%m/%d')
    skill = models.CharField(max_length=15, null=False, blank=False)
    level = models.CharField(max_length=30, null=False, blank=False, choices= levels)

    def __str__(self):
        return self.skill

    class Meta:
        verbose_name = 'Skill'

#-----------------------------------------------------------------

class Portfolio(models.Model):
    categories = [
        ('Web Sites', 'Web Sites'),
        ('Photographs', 'Photographs'),
        ('3D', '3D'),
        ('Brands', 'Brands'),
        ('Photoshop', 'Photoshop'),

    ]

    name = models.CharField(max_length=50, null=False, blank=False)
    image = models.ImageField(upload_to='photos/%y/%m/%d')
    category = models.CharField(max_length=50, choices= categories, default='3D')

    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name = 'Portfolio Image'


#-----------------------------------------------------------------


class Navlink(models.Model):
    linkname = models.CharField(max_length=15)
    link = models.CharField(max_length=50)


#-----------------------------------------------------------------


class Contact(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    message = models.TextField()
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = 'Message'
    

#-----------------------------------------------------------------


class Blog(models.Model):
    authers = [
        ('Malek Anas', 'Malek Anas'),
    ]

    id = models.AutoField(primary_key= True)
    blog_img = models.ImageField(upload_to='photos/%y/%m/%d')
    blog_title = models.CharField(max_length=100)
    blog_content = models.TextField()
    blog_auther = models.CharField(max_length=50, choices= authers)
    blog_date = models.DateField(auto_now=True)
    def __str__(self):
        return self.blog_title
    
    class Meta:
        verbose_name = 'Post'

admin.py

from django.contrib import admin
from .models import AboutUs, Name, Skills, Portfolio, Navlink, Contact, Blog
# Register your models here.

class AboutUsAdmin(admin.ModelAdmin):
    list_display = ('content',)

admin.site.register(AboutUs, AboutUsAdmin)


#-----------------------------------------------


class NameAdmin(admin.ModelAdmin):
    list_display = ('name',)

admin.site.register(Name, NameAdmin)


#-----------------------------------------------


class SkillsAdmin(admin.ModelAdmin):
    list_display = ('skill',)
    search_fields = ('skill',)

admin.site.register(Skills, SkillsAdmin)


#-----------------------------------------------


class PortfolioAdmin(admin.ModelAdmin):
    list_display = ('name',)
    search_fields = ('name',)
    list_filter = ('category',)

admin.site.register(Portfolio, PortfolioAdmin)


#-----------------------------------------------


class NavlinkAdmin(admin.ModelAdmin):
    list_display = ('linkname',)
    search_fields = ('linkname',)

admin.site.register(Navlink, NavlinkAdmin)


#-----------------------------------------------


class ContactAdmin(admin.ModelAdmin):
    list_display = ('name',)
    search_fields = ('name',)

admin.site.register(Contact, ContactAdmin)


#-----------------------------------------------


class BlogAdmin(admin.ModelAdmin):
    list_display = ('blog_title',)
    search_fields = ('blog_title', 'blog_auther')

admin.site.register(Blog, BlogAdmin)


# Admin Customization
admin.site.site_header = 'Alsayed Design Administration'
admin.site.site_title = 'Alsayed Design'

I tried many ways but no one of them worked with me


Solution

  • You use b for the Blog object (I would advise using blog but anyway), that means you access the post id with b.pk, so:

    <a href="{% url 'detail' b.pk %}">…</a>