Home > database >  Reverse for 'author' with arguments '('',)' not found. 1 pattern(s) tr
Reverse for 'author' with arguments '('',)' not found. 1 pattern(s) tr

Time:03-16

while running http://127.0.0.1:8000/blogapp/blog/ , in django 4.0, i got error reverse for... not found and

this error occurs when i add (post.author.id) in href which is in template mentioned line 15 if require more information please comment below

    views.py
        
    
def home(request):
   return render(request,'blogapp/home.html')

def authorview(request,pk):
    user=User.objects.get(id=pk)
    return render(request,'blogapp/authorview.html',{'user':user})
    
def blogview(request):
    form=blogform()
    if request.method=='POST':
        form=blogform(request.POST)
        if form.is_valid():
           form.save()
           return redirect('/blogapp/')
        else:
           return  redirect('home')
    else:
        form=blogform()
    context={'form':form}
    return render(request,'blogapp/blog.html',context)
@login_required
def blogretrieve(request):
   q=blogmodel.objects.all().order_by('-published_date')
   context={'q':q}
   return render(request,'blogapp/blogretrieve.html',context)

def signupview(request):
   form=signupform()
   if request.method=='POST':
      form=signupform(request.POST)
      if form.is_valid():
        form.save()
        return redirect('/blogapp/')
   else:
      form=signupform()  
   context={'form':form}
   return render(request,'blogapp/signupform.html',context)

def loginview(request):
   if request.method=='POST':
    username = request.POST.get('username')
    password = request.POST.get('password')
    user = authenticate(request, username=username, password=password)
    if user is not None:
      login(request, user)
      return redirect('/blogapp/blog/')
        
    else:
        return redirect('/blogapp/signup/')
    #context={'username':username,'password':password}
   return render(request,'blogapp/loginview.html')    
def logoutview(request):
   logout(request)
   return render(request,'blogapp/logoutview.html')

templates
In template C:\Users\SHAFQUET NAGHMI\blog\blogapp\templates\blogapp\blogretrieve.html, error at line 15

    {% load static %}
    {% block content %}
    
    <h1>Blog </h1>
    <link rel="stylesheet" href="{% static 'blogapp/blogretrieve.css' %}">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    
    
    {% for post in q %}
    <div >
        <div >
          <h2 >{{post.title}} </h2><br>
          <p >{{post.Newpost}}</p>
          <a href="{% url 'blogapp:author' post.author.id %}">{{post.author}}</a    <!-- line 15 -->
          <small >{{post.created_date}}</small>
    
          
        </div>
      </div>
    {% endfor %}
    
   urls.py   
    

app_name='blogapp'
    urlpatterns=[
        path('',views.home,name='home'),
        path('createblog/',views.blogview,name='blogview'),
        path('blog/',views.blogretrieve,name='blog'),
        path('signup/',views.signupview,name='signup'),
        path('login/',views.loginview,name='login'),
        path('logout/',views.logoutview,name='logout'),
        path('author/<str:pk>/',views.authorview,name='author'),
]

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.deletion import CASCADE

class blogmodel(models.Model):
    author=models.ForeignKey(User,on_delete=models.CASCADE,blank=True,null=True)
    title=models.CharField(max_length=200)
    Newpost=models.TextField(max_length=1000)
    created_date=models.DateTimeField(auto_now_add=True)
    published_date=models.DateTimeField(auto_now=True)
    def __str__(self):
        return f'{self.title},{self.Newpost},{self.published_date},{self.created_date}'

CodePudding user response:

share your models.py and urls.py

CodePudding user response:

The error means that post.author.id is empty in

{% url 'blogapp:author' post.author.id %}

As you do not post the related view it is impossible to tell you why it is empty. You loop through "q" so q should be in your context and holding the posts. So either your list q is empty or one of the contained posts has no author.

  • Related