Hello!
Im getting an error to
Reverse for ‘article’ not found. ‘article’ is not a valid view function or pattern name/
When Im trying to edit an article, after editing it shows error. But the article will be changed.
When I press “edit”
VIEWS.PY
from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.urls import reverse_lazy
from .models import *
from .forms import ArticleForm
from django.views.generic import ListView,DetailView,UpdateView,CreateView,DeleteView
from django import forms
class HomeView(ListView):
model = Article
template_name = 'blog/index.html'
context_object_name = 'articles'
extra_context = {
"main_page":'Main page'
}
class CategoryView(ListView):
model = Article
template_name = 'blog/blogs.html'
context_object_name = 'articles'
def get_queryset(self):
return Article.objects.filter(category_id=self.kwargs['pk'])
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data()
category= Category.objects.get(pk=self.kwargs['pk'])
context['title'] = f'Category: {category.title}'
return context
class ArticleView(DetailView):
model = Article
template_name = 'blog/article.html'
context_object_name = 'article'
def get_context_data(self, **kwargs):
context = super().get_context_data()
article = Article.objects.get(pk=self.kwargs['pk'])
context['title'] = f'Title: {article.title}'
return context
class BlogView(ListView):
model = Article
template_name = 'blog/blogs.html'
context_object_name = 'articles'
extra_context = {
"main_page":'Main page'
}
class AddArticle(CreateView):
model = Article
form_class = ArticleForm
template_name = 'blog/article_form.html'
extra_context = {
'title':f'Create an article'
}
class ArticleUpdate(UpdateView):
model = Article
form_class = ArticleForm
template_name = 'blog/article_form.html'
extra_context = {
'title':'Edit the article'
}
URLS.PY
from django.urls import path
from .views import *
urlpatterns = [
path('', HomeView.as_view() ,name='homescreen'),
path('category/<int:pk>/',CategoryView.as_view(),name='category-view'),
path('article/<int:pk>/',ArticleView.as_view(),name='article-view'),
path('blogs/',BlogView.as_view(),name='blogs'),
path('add-article',AddArticle.as_view(),name='add-article'),
path('article/<int:pk>/edit-article/',ArticleUpdate.as_view(),name='edit-article'),
path('article/<int:pk>/delete/',ArticleDelete.as_view(),name='delete')
]
MODELS.PY:
from django.db import models
from django.urls import reverse
# Create your models here.
class Category(models.Model):
title = models.CharField(blank=True,max_length=255,verbose_name='Name of the category')
def get_absolute_url(self):
return reverse('category',kwargs={"pk":self.pk})
def __str__(self):
return self.title
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
class Article(models.Model):
title = models.CharField(max_length=255,blank=False)
description = models.TextField(default='Type your text',verbose_name='description')
created_at = models.DateTimeField(auto_now_add=True,verbose_name='Added at ')
updated_at=models.DateTimeField(auto_now=True,verbose_name='Updated at ')
image = models.ImageField(upload_to='images/',blank=True)
views = models.PositiveIntegerField(default=0,verbose_name='Views')
category = models.ForeignKey(Category,on_delete=models.CASCADE,verbose_name='Category',null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('article',kwargs={'pk':self.pk})
class Meta:
verbose_name='Article'
verbose_name_plural='Articles'
article.html
{% extends 'base.html' %}
{% load static %}
{% block title %}
{{ title }}
{% endblock title %}
{% block main %}
<div class="container">
{% if article.image %}
<img height="200px" src="{{ article.image.url }}" alt="">
{% else %}
<img height="200px" src="https://media.istockphoto.com/id/1354826805/vector/data-science.jpg?s=612x612&w=0&k=20&c=FKYxpsaWX7H5_DEX4t0dV2o837_aqgoavlIRdIz1jwE=" alt="">
{% endif %}
<h3>{{ article.title }}</h3>
<span>{{ article.description|linebreaks }}</span>
<p> {{ article.created_at }} </p>
<span> | Views: {{ article.views }} </span>
<a href="{% url 'edit-article' article.pk %}" class="btn btn-outline-success">Edit</a>
<a href="{% url 'delete' article.pk %}" class="btn btn-outline-success">Delete</a>
</div>
{% endblock main %}
However, getting an error: