M
M
Maxim Maximov2020-02-20 13:55:23
Django
Maxim Maximov, 2020-02-20 13:55:23

Where is the not found 404 error?

Please help me figure out where I made a mistake?

Page not found (404)
Request Method:	GET
Request URL:	http://127.0.0.1:8000/detail/9
Using the URLconf defined in iswork.urls, Django tried these URL patterns, in this order:

[name='home']
detail/ [name='detail_page']
edit-page [name='edit_page']
update-page/ [name='update_page']
delete-page/ [name='delete_page']
admin/
The current path, detail/9, didn't match any of these


urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('', include('core.urls')),
    path('admin/', admin.site.urls),
]


core/urls.py

from django.contrib import admin
from django.urls import path
from core import views

urlpatterns = [
    path('', views.HomeListView.as_view(), name='home'),
    path('detail/', views.HomeDetailView.as_view(), name='detail_page'),
    path('edit-page', views.edit_page, name='edit_page'),
    path('update-page/', views.update_page, name='update_page'),
    path('delete-page/', views.delete_page, name='delete_page'),

]


core/views

from django.shortcuts import render,redirect

from .models import Articles
from django.views.generic import ListView, DetailView
from .forms import ArticleForm
from django.urls import reverse


class HomeListView(ListView):
    model = Articles
    template_name = 'index.html'
    context_object_name = 'list_articles'


class HomeDetailView(DetailView):
    model = Articles
    template_name = 'detail.html'
    context_object_name = 'get_article'

def edit_page(request):
    success = False
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            success = True
 
    template = 'edit_page.html'
    context = {
        'list_articles': Articles.objects.all().order_by('-id'),
        'form':ArticleForm(),
        'success':success
    }
    return render(request,template,context)
 
 
def update_page(request,pk):
    success_update = False
    get_article = Articles.objects.get(pk=pk)
    if request.method == 'POST':
        form = ArticleForm(request.POST,instance = get_article)
        if form.is_valid():
            form.save()
            success_update = True
            template = 'edit_page.html'
            context = {
                'get_article': get_article,
                'update':True,
                'form':ArticleForm(instance = get_article),
                'success_update':success_update
    }
    return render(request, template, context)
 

def delete_page(request,pk):
    get_article = Articles.objects.get(pk=pk)
    get_article.delete()
    
    return redirect(reverse('edit_page'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-02-20
@bacon

Missing path c <int:pk>for DetailView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question