0

Hi I am trying to create a link with paremeter but I am getting an error (Page not found)

error

Reverse for 'hexcode' with arguments '('#d4cbd0',)' not found. 1 pattern(s) tried: ['hexcode/(?P<color>\\w)$']

template

{% for color in palette_dominant_color %}
<a href="{% url 'brandcolors:hexcode' color %}" style="text-decoration:none;color:inherit">
  {{color}}
</a>
<br>
{% endfor %}

urls.py

url(r'^hexcode/(?P<color>\w)/$', ThemeView.as_view(), name="hexcode"),

views.py

class ThemeView(TemplateView):
    template_name='fabric/theme.html'

    def get_context_data(self, **kwargs):
        context = super(ThemeView, self).get_context_data(**kwargs)
        colors = Color.objects.filter(color=kwargs['color']).all()
        return context
Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
  • Possible duplicate of [how to add url parameters to django template url tag](https://stackoverflow.com/questions/25345392/how-to-add-url-parameters-to-django-template-url-tag) – Clément Denoix Nov 14 '17 at 16:28
  • 2
    BTW, your context is not added you need to set `context['colors'] = Color....` – mohammedgqudah Nov 14 '17 at 16:29
  • 1
    Note that trying to include `#` in the URL could cause problems, because it is used for [fragment identifiers](https://en.wikipedia.org/wiki/Fragment_identifier). It's OK when you reverse the URL, because Django will encode the hash as `%23`. However, if your users try to use the URL `/hexcode/#d4cbd0`, then their browser will strip the anchor and only send `/hexcode/` to the server. – Alasdair Nov 14 '17 at 17:22

2 Answers2

2

Your regex expects a single alphanumeric character, not a hash followed by several characters.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

Your link should be:

<a href="{% url 'brandcolors:hexcode' color=color %}" style="text-decoration:none;color:inherit">{{color}}</a>
Clément Denoix
  • 1,504
  • 11
  • 18
  • Thanks #clément but I am still having this error `Reverse for 'hexcode' with keyword arguments '{'color': '#d4cbd0'}' not found. 1 pattern(s) tried: ['hexcode/(?P\\w)$']` – Papouche Guinslyzinho Nov 14 '17 at 16:35
  • 1
    It's because your regex url pattern for the color parameter (`\w`) don't match the provided color parameter (`#d4cbd0`). You could use another regex to match color hexa code like this one: https://gist.github.com/LunaCodeGirl/8746738 – Clément Denoix Nov 14 '17 at 16:41