django 我总是遇到这个问题,Reverse for 'detail' with arguments '('',)' not found.已尝试1种模式:['post/(?P < post_id>[0-9]+)/\\Z']

ndasle7k  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(340)

我是Malek我正在用Django写博客
但我正面临着这个问题的url和pk
未找到带参数“('',)”的“detail”的反转。已尝试1种模式:['post/(?P <post_id>[0-9]+)/\Z']
我希望有人能帮我解决这个问题
enter image description here
下面是我的每个文件的django代码
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'

我试了很多方法,但没有一个对我有用

mo49yndu

mo49yndu1#

对于Blog对象使用b(我建议使用blog,但无论如何),这意味着您使用b.pk访问post id,因此:

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

相关问题