S_alj
S_alj

Reputation: 447

Cannot have any URLs with slugs. NoReverseMatch

I'm a begginer grasping at straws with difficulty dealing with the django slug url system and these NoReverseMatch errors that make no sense to me even after reading the docs.

I have a django project. In one of the views, I pass a list of geoJSON features into a template, and show them on a map. I want to have each feature act as a clickable 'link' to a view that will show stuff about it. The following is part of the template that has those features that I want to click on:

//part of the template:
<script type="text/javascript">
...
function onEachFeature(feature, layer) {
        layer.on('click', function (e) {
        window.location.href = "{% url 'polls:areadetail' feature.properties.myslug%}";
        });
     }

(I have confirmed that feature.properties.myslug does in fact contain the slug I want).

The url pattern I want to go to:

urlpatterns = [...
url(r'^areadetail/(?P<areaslug>[-\w]+)/$', views.AreaDetail, name='areadetail'),]

And the view it relates to:

def AreaDetail(request, areaslug):
    area = get_object_or_404(Area, nameslug=areaslug)
    return render(request, 'polls/areadetail.html', {'area':area})

The issue I get is, by doing what I show and placing that url reference inside that template I show above, that I want to be able click on, that template won't even work at all, giving me a 'Error during template rendering' full page error info that starts with:

NoReverseMatch at /polls/areas/ Reverse for 'areadetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/areadetail/(?P[-\w]+)/$']

Any help would be immensely appreciated


EDIT part1: As I've said in response to falsetru, I'm sure feature.properties.myslug has in fact got a slug expression in it. EDIT2: Based on something I found in a django ticket, I've made a slight change in the url regex at urls.py, from (?P<areaslug>[-\w]+)/$ to (?P<areaslug>[-\w]+)?/$ and now the error is:

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/polls/areadetail// Raised by: polls.views.AreaDetail

Is it possible that because the "{% url 'polls:areadetail' feature.properties.myslug%}" bit is inside javascript, that feature.properties.myslug is not being inserted there correctly? Like some sort of brackets are needed here?

Upvotes: 1

Views: 283

Answers (2)

S_alj
S_alj

Reputation: 447

After some more digging around I've found the answer to why doesn't this work in another question at:

How to pass javascript variable to django custom filter

The answer to it by Ludwik Trammer says:

Django templates are build on the server side, while JavaScript is executed on the client side. That means that template code is always executed before JavaScript (as it is executed by the server, before the page is sent to the client). As a consequence it is absolutely impossible to mix JavaScript and Django code the way you want to.

Which clearly applies here. I was focused on problems with the URL template, regex on the urls.py file etc. when the problem was that no matter what I did, because it's in a javascript section, run client-side, that URL template will always be incomplete no matter what I do, therefore being an impossible solution to what I want.

Upvotes: 1

falsetru
falsetru

Reputation: 368894

According to the error message, feature.properties.myslug is empty or has no value.

Make sure the feature.properties.myslug is passed correctly from view.

  • Comment out {% url .. %} temporarily.
  • Print {{ feature }}, {{ feature.properties }}, {{ feature.properties.myslug }} to see if which part is missing.
  • Fix view accordingly.
  • Uncomment {% url .. %}.

Upvotes: 1

Related Questions