KNTY
KNTY

Reputation: 457

Django - missing 1 required positional argument: '_id'

im getting an error

BlogDetailView() missing 1 required positional argument: '_id'

when im trying to access the function BlogDetailView.

views.py :

from django.http.response import Http404
from .models import BlogModel,CommentModel
from .forms import SearchForm,CommentForm
from django.shortcuts import render,redirect

def BlogDetailView(request,_id):
try:
    data = BlogModel.objects.get(id = _id)
    comments = CommentModel.objects.filter(blog = data)
except BlogModel.DoesNotExist:
    raise Http404('Data does not exist')
 
if request.method == 'POST':
    form = CommentForm(request.POST)
    if form.is_valid():
        Comment = CommentModel(your_name= form.cleaned_data['your_name'],
        comment_text=form.cleaned_data['comment_text'],
        blog=data)
        Comment.save()
        return redirect(f'/blog/{_id}')
else:
    form = CommentForm()

context = {
        'data': data,
        'form': form,
        'comments': comments,
    }
return render(request,'Test_one/detailview.html',context)

urls.py :

from django.conf.urls import url
from django.urls.conf import path
from blogapp.views import BlogDetailView, BlogListView
from . import views

app_name = "Blogapp"

urlpatterns = [
url(r'^blogs/', views.BlogDetailView, name="blogs"),
url(r'^blog/<int:_id>', views.BlogListView, name="blog"),
]

Can anyone solve this problem?

Upvotes: 1

Views: 11711

Answers (1)

Alireza
Alireza

Reputation: 2123

I think you wrote code wrongly, logically a blog list doesn't need an id to fetch (you want all blog posts so probably don't need id) and you need to fetch a specific blog post so you need an id to fetch this. so I think this is the right code that you tried to write:

from django.conf.urls import url
from django.urls.conf import path
from blogapp.views import BlogDetailView, BlogListView
from . import views

app_name = "Blogapp"

urlpatterns = [
    url(r'^blogs/<int:_id>', views.BlogDetailView, name="blogs"),
    url(r'^blog/', views.BlogListView, name="blog"),
]

Upvotes: 2

Related Questions