from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('', include('core.urls')),
path('admin/', admin.site.urls),
]
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'),
]
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'))
Поменял вроде все как вы подсказали, но почему то все осталось так же. Вот исправленный код.