Issue
I have just started working in Django. My problem in addressing with slug I get an error when sending a request to Slug.
This is my error: Articles matching query does not exist.
I also get an error when migrate.
This is my error:
django.db.utils.OperationalError:table "bloges_category" already exists
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.text import slugify
class Articles(models.Model):
Writer = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.ManyToManyField(Category)
Title = models.CharField(max_length=40, unique=True)
Body = models.TextField()
Image = models.ImageField(upload_to='articles')
Time = models.DateTimeField(editable=False, auto_now_add=True)
Update = models.DateTimeField(auto_now=True)
slug = models.SlugField(blank=True, unique=True)
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None):
self.slug = slugify(self.Title)
super(Articles, self).save()
def get_absolute_url(self):
return reverse("bloges:articles_detail", args=[self.slug])
my view:
from django.shortcuts import render
from bloges.models import Articles
def post(request, slug):
articles = Articles.objects.get(slug=slug)
return render(request, 'bloges/post-details.html', {"postartic": articles})
my urls:
from django.urls import path
from . import views
app_name = 'bloges'
urlpatterns = [
path("post/<slug:slug>", views.post, name="articles_detail")
]
Error after sending request:
Articles matching query does not exist.
Solution
I get an error when sending a request to Slug It is my error: Articles matching query does not exist.
This occurs based on your posted view called post
not having a URL param named slug
that matches the call in your view here: articles = Articles.objects.get(slug=slug)
.
This errors out because you use get()
, which throws an error if the object searched for is not found. A common solution would be to use get_object_or_404(Articles, slug=slug)
, which would throw an HTTP 404
when the object is not found or to filter
, i.e. articles = Articles.objects.filter(slug=slug)
in which case if the variable articles
is not available (i.e. if not articles
, None
, or Articles.objects.none()
, you do what you wish.
Notice your URL param here:
urlpatterns = [
path("post/<slug:slug>", views.post, name="articles_detail")
]
Which likely calls a URL like: http://localhost:8000/post/foo
Where foo
is not a slug
found in Articles
here: Articles.objects.get(slug='foo')
And I get an error when migrate It is my error: django.db.utils.OperationalError: table "bloges_category" already exists
We need to see how your migrations are, and your current DB structure. Overall this error is stating you are running a migration that cannot complete because it is trying to create a table that is already there.
My first thought is that you accidentally deleted your migration files and re-ran makemigrations
, or you removed your data from your migration table in the DB. But without more info it is hard to tell.
Answered By - ViaTech
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.