Home > front end >  Django URL Path from DB file value
Django URL Path from DB file value

Time:10-19

I am trying to create 'project' pages that have their paths generated with the {{ project.title }} values, rather than the current method I have which uses ints. I don't quite understand how I can do this, but feel I am close?

Models.py

from django.db import models

# Create your models here.
class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    technology = models.CharField(max_length=20)
    image = models.FilePathField(path='projects/static/img/')
    live = models.URLField()
    source = models.URLField()

    def __str__(self):
        return self.title

Urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.project_index, name="projects"),
    path("<int:pk>/", views.project_details, name="project_details"),  # PK for Primary Key
]

Views.py

from django.shortcuts import render
from .models import Project

# Create your views here.
def project_index(request):
    projects = Project.objects.all()
    context = {'projects': projects}
    return render(request, 'projects/project_index.html', context)

def project_details(request, pk):
    project = Project.objects.get(pk=pk)
    context = {'project': project}
    return render(request, 'projects/project_details.html', context)

I figure path("<int:pk>/", will need to be a slug, but I just cannot figure out how to tie in the DB data. Potentially context = {'project': project}?

Currently the url is http://127.0.0.1:8000/projects/1/ - I am looking for http://127.0.0.1:8000/projects/EXAMPLE/

Thanks

CodePudding user response:

urls.py

    path("<title>/", views.project_details, name="project_details"),

views.py

from django.shortcuts import get_object_or_404
def project_details(request, title: str):
    project = get_object_or_404(Project, title=title)

CodePudding user response:

You have to add a SlugField to your models.py file:

Models.py

from django.db import models
from django.utils.text import slugify

# Create your models here.
class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    technology = models.CharField(max_length=20)
    image = models.FilePathField(path='projects/static/img/')
    live = models.URLField()
    source = models.URLField()
    slug = models.SlugField(default="", blank=True, null=False, db_index=True)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super().save(*args, **kwargs)

Views.py

from django.shortcuts import render
from .models import Project

# Create your views here.
def project_index(request):
    projects = Project.objects.all()
    context = {'projects': projects}
    return render(request, 'projects/project_index.html', context)

def project_details(request, slug):
    project = Project.objects.get(slug=slug)
    context = {'project': project}
    return render(request, 'projects/project_details.html', context)

Urls.py


from django.urls import path
from . import views

urlpatterns = [
    path("", views.project_index, name="projects"),
    path("<slug:slug>/", views.project_details, name="project_details"),
]

Make sure to run makemigrations and then migrate.

  • Related