Home > Blockchain >  How to specify a URL slug in django template?
How to specify a URL slug in django template?

Time:12-03

I am trying to create a django template that has links to other pages (static images in particular, each with their own html template). I am trying to move away from hard-coding a URL and view for each one. Instead I want to capture them all with a general slug URL, and a view that takes the slug as input.

My slug URL in urls.py is working fine - when I manually input the slug field in the full URL it links to the correct template and directs me to the correct page. However l, when I try to reference any of links as slugs from the 'cside' template I keep getting the following error:

NoReverseMatch at /plots/cside
Reverse for '2E_C' not found. '2E_C' is not a valid view function or pattern name.

Basically, I want the 'cside' page to have links that are slugs.

Can anyone tell me what I am missing? I have tried everything!

Here is my urls.py:

from django.urls import re_path, path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
from .views import mode

urlpatterns = [
    re_path(r'^cside$', views.cside, name='cside'),
    re_path(r'^lside$', views.lside, name='lside'),
    re_path(r'^home$', views.home, name='home'),

    #re_path(r'^2E_C$', views.m2E_C),
    re_path(r'^4M_C$', views.m4M_C),
    re_path(r'^6E_C$', views.m6E_C),
    re_path(r'^6M_C$', views.m6M_C),
    re_path(r'^8E_C$', views.m8E_C),

    re_path(r'^2E_L$', views.m2E_L),
    re_path(r'^4M_L$', views.m4M_L),
    re_path(r'^6E_L$', views.m6E_L),
    re_path(r'^6M_L$', views.m6M_L),
    re_path(r'^8E_L$', views.m8E_L),
    #re_path(r'^(?P<slug>[-\w] )/$', views.mode, name='mode'),
    path('<slug:slugIn>/', views.mode, name='mode')   
]

Here is my views.py:

from django.views.generic import TemplateView, ListView
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader

from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic, View
from .models import Mode

def cside(request):
        template = loader.get_template('cside.html')
        context = {}
        return HttpResponse(template.render(context, request))

def lside(request):
        template = loader.get_template('lside.html')
        context = {}
        return HttpResponse(template.render(context, request))

def home(request):
        template = loader.get_template('home.html')
        context = {}
        return HttpResponse(template.render(context, request))


# Slug Solution
#======================================

def mode(request, slugIn=None):
        model = Mode
        #print(slugIn)
        slugOut = Mode.objects.all()
        #print(slugOut)
        template = loader.get_template(slugIn '.html')
        context = {"slug": slugOut}
        return HttpResponse(template.render(context, request))


# Hardcoded Old Solution
#======================================

# def m2E_C(request):
#         template = loader.get_template('2E_C.html')
#         context = {}
#         return HttpResponse(template.render(context, request))
def m400M_C(request):
        template = loader.get_template('4M.html')
        context = {}
        return HttpResponse(template.render(context, request))
def m6E_C(request):
        template = loader.get_template('6E_C.html')
        context = {}
        return HttpResponse(template.render(context, request))
def m6M_C(request):

...

And here is the html template for the page I am having issues with:

<!DOCTYPE html
<html><head>
    <style type="text/css">
    a { color:#005fce; text-decoration:none; font-weight:normal;}
    c { color:#000000; text-decoration:none; font-weight:bold;}

</style>
</head>
<body><div >
    <h1>C SIDE</h1><br>
    <a href="{% url 'home' %}">Home</a><br><br>
    
    <c>TYPES:</c><br>   
    
    <a href="{% url '2E_C' slug.slug %}">2E</a><br>

    <a href="{% url '4M_C' %}">4M</a><br>    
    <a href="{% url '6E_C' %}">6E</a><br>    
    <a href="{% url '6M_C' %}">6M</a><br>    
    <a href="{% url '8E_C' %}">8E</a><br>

I think my issue is that I can’t seem to pass the input slug field from the URL to the template file, to then know where to go.

Or maybe I need to use a model to save the slug, but I couldn't figure out how to do this either.

CodePudding user response:

Your url is named mode so you need to change your link to use {% url ‘mode’ ‘2E_C’ %}.

  • Related