vim
vim

Reputation: 1118

Problem with django urls which have a hyphen in them

I have a simple set of urls in a Django url conf file which points to some object detail generic views.

urlpatterns = patterns('',  
url(r'^projects/(?P<slug>[\w-]+)/$', ProjectDetailView.as_view(), name='view_project'),
url(r'^roles/(?P<slug>[\w-]+)/$', RoleDetailView.as_view(), name='view_role'),
)

The problem is whenever there's a hyphen in the urls (eg:-/projects/new-project/) slug, Djangos development server get stuck. I've checked with pdb and there isn't a problem with parsing the url and getting the object from the database based on the slug. But it gets stuck somewhere when the template gets rendered. I can't figure out the source of the problem. any idea what the problem is?

The view code is,

class ProjectDetailView(DetailView):
  model=Project
  context_object_name='project_obj'
  slug_field='slug'

  @method_decorator(login_required)
  def dispatch(self, *args, **kwargs):
    return super(ProjectDetailView, self).dispatch(*args, **kwargs)

The template code is,

 {% extends "base.html" %}
    {% load static %}
    {% block static %}
    <link rel="stylesheet" type="text/css" href="{% get_static_prefix %}css/demo_table.css">
    <script type="application/javascript" src="{% get_static_prefix %}js/users-index.js"></script>
    {% endblock %}

        {% block content %}
        <div id="itemlist">    

          {% if project_obj %}
          <div>
            <p>{{ project_obj.title }}</p>
            <p>{{ project_obj.description }}</p>
          </div>
          {% else %}
          <p>No Details available.</p>
          {% endif %}
        <div>
        {% endblock %}

After removing some of the tags from the template it started working,

<div id="itemlist">    

      {% if project_obj %}
      <div>
        <p>{{ project_obj.title }}</p>
        <p>{{ project_obj.description }}</p>
      </div>
      {% else %}
      <p>No Details available.</p>
      {% endif %}
    <div>

Upvotes: 3

Views: 3077

Answers (2)

bradley.ayers
bradley.ayers

Reputation: 38392

My guess is that you wrote a custom template tag, but it's broken. You're using it in base.html :P

Upvotes: 1

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13496

Change [\w-]+ to [-\w]+. For me [\w-]+ never works with python regexp's.

Upvotes: 5

Related Questions