Home > Blockchain >  I get a 404 error when passing as a parameter to the path an int that is a foreign key
I get a 404 error when passing as a parameter to the path an int that is a foreign key

Time:11-03

This is the url i am trying:

http://localhost:8000/blog/categoria/1/

The foreign key is categoria_id that comes from relationship many to many of Post and Categoria.

I am using Sqlite3.

This file is the models.py

from django.db import models
from django.contrib.auth.models import User


class Categoria(models.Model):
    nombre=models.CharField(max_length=50)
    created=models.DateTimeField(auto_now_add=True)
    updated=models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name='categoria'
        verbose_name_plural='categorias'
    
    def __str__(self):
        return self.nombre


class Post(models.Model):
    titulo=models.CharField(max_length=50)
    contenido=models.CharField(max_length=50)
    imagen=models.ImageField(upload_to='blog', null=True, blank=True)
    autor=models.ForeignKey(User, on_delete=models.CASCADE)
    categorias=models.ManyToManyField(Categoria)
   

    created=models.DateTimeField(auto_now_add=True)
    updated=models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name='post'
        verbose_name_plural='posts'
    
    def __str__(self):
        return self.titulo

This file is the views.py:

from django.shortcuts import render
from blog.models import Post, Categoria




def blog(request):

    posts=Post.objects.all()
    return render(request, "blog/blog.html",{"posts":posts})

def categoria(request, categoria_id):

    categoria=Categoria.objects.get(id=categoria_id)
    posts=Post.objects.filter(categorias=categoria)
    return render(request, "blog/categoria.html",{'categoria': categoria, "posts":posts })

This file is the urls.py

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    
    path('', views.blog, name='Blog'),
    path('categoria/<int:categoria_id>/', views.categoria, name="categoria")
    

]

CodePudding user response:

I change the database to postgresql and I added a / before categoria in the file urls.py. For now it works. It’s strange, because I’ve made these changes before and it didn't work.

Despite this I would appreciate any suggestion. Thank you.

This file is the urls.py after the change:

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    
    path('', views.blog, name='Blog'),
    path('/categoria/<int:categoria_id>/', views.categoria, name='categoria'),
    

]

CodePudding user response:

Parameters would usually look something like this: https://www.google.com/search?q=java.net.Socken: Socket&rlz=1C1C&oq=java.net. etc..

Note the first one has a ? and thereafter an & ...

Each parameter has a name and a value. The URL http://localhost:8000/blog/categoria/1/ calls the index.html page in the folder on categoria/1/ That page would need to get a parameter like categoria/1/index.html?me=joe&key=23423423

  • Related