Issue
Reverse for 'blogpost' with arguments '('',)' not found. 1 pattern(s) tried: ['blog/(?P[0-9]+)\Z']
I face this problem in every project. So please any devloper solve this problem
my urls.py
from django.urls import path
from .import views
urlpatterns = [
path('', views.index, name='Blog_home'),
path('<int:pk>', views.blogpost, name='blogpost'),
]
my views.py
from django.shortcuts import render
from blog.models import Post
# Create your views here.
def index(request):
post = Post.objects.all()
context = {'post':post}
return render(request, 'blog/bloghome.html', context)
def blogpost(request, pk):
blog_post = Post.objects.get(id=pk)
context = {'blog_post':blog_post}
return render(request, 'blog/blogpost.html', context)
my models.py
from django.db import models
# Create your models here.
class Post(models.Model):
post_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
content = models.TextField()
author = models.CharField(max_length=50)
date_and_time = models.DateTimeField(blank=True)
def __str__(self):
return self.title
Template: bloghome.html
{% extends 'basic.html' %}
{% block title %} Blog {% endblock title %}
{% block blogactive %} active {% endblock blogactive %}
{% block style %}
.overlay-image{
position: absolute;
height: auto;
width: 100%;
background-position: center;
background-size: cover;
opacity: 0.5;
}
{% endblock style %}
{% block body %}
<div class="container">
{% for post in post %}
<div class="row">
<div class="col-md-7 py-4">
<div class="row g-0 border rounded overflow-hidden flex-md-row shadow-sm h-md-250 position-relative">
<!-- <div class="col-auto m-auto img-fluid">
<img src="#">
</div> -->
<div class="col p-4 blog d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-primary">Code</strong>
<h3 class="mb-0">{{post.title}}</h3>
<div class="mb-1 text-muted">{{post.date_and_time}}</div>
<p class="card-text mb-auto">{{post.content | truncatechars:150}}</p>
<div class="my-2">
<a href="{% url 'blogpost' blog_post.id %}" class="btn btn-primary">Continue reading</a>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock body %}
Template: blogpost.html
{% extends 'basic.html' %}
{% block title %} Blog {% endblock title %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-8">
<h2>{{post.title}}</h2>
</div>
</div>
</div>
{% endblock body %}
Error
NoReverseMatch at /blog/
Reverse for 'blogpost' with arguments '('',)' not found. 1 pattern(s) tried: ['blog/(?P<pk>[0-9]+)\\Z']
Error at Template: bloghome.html
<a href="{% url 'blogpost' blog_post.id %}" class="btn btn-primary">Continue reading</a>
Please any devloper solve this problem.
Solution
- views.py
from django.shortcuts import render, get_object_or_404
from blog.models import Post
def index(request):
posts = Post.objects.all()
context = {'posts': posts}
return render(request, 'blog/bloghome.html', context)
def blogpost(request, pk):
blog_post = get_object_or_404(Post, pk=pk)
context = {'blog_post': blog_post}
return render(request, 'blog/blogpost.html', context)
- bloghome.html
{% extends 'basic.html' %}
{% block title %} Blog {% endblock title %}
{% block blogactive %} active {% endblock blogactive %}
{% block style %}
.overlay-image{
position: absolute;
height: auto;
width: 100%;
background-position: center;
background-size: cover;
opacity: 0.5;
}
{% endblock style %}
{% block body %}
<div class="container">
{% for post in posts %}
<div class="row">
<div class="col-md-7 py-4">
<div class="row g-0 border rounded overflow-hidden flex-md-row shadow-sm h-md-250 position-relative">
<!-- <div class="col-auto m-auto img-fluid">
<img src="#">
</div> -->
<div class="col p-4 blog d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-primary">Code</strong>
<h3 class="mb-0">{{post.title}}</h3>
<div class="mb-1 text-muted">{{post.date_and_time}}</div>
<p class="card-text mb-auto">{{post.content | truncatechars:150}}</p>
<div class="my-2">
<a href="{% url 'blogpost' post.pk %}" class="btn btn-primary">Continue reading</a>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock body %}
Answered By - Waket Zheng
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.