Proble understanding DetailView url doesn't match the slug

79 views
Skip to first unread message

inoyon artlover KLANGRAUSCH

unread,
Feb 14, 2015, 4:28:40 AM2/14/15
to django...@googlegroups.com
Hi there, there is something I obviously don't understand. According to django reference,
the DetailView has to be provided a slug or pk. I do it in the url but django constantly doesn't
recognize the slug in the url.  'current url doesn't match etc.' what is wrong here? I do everything
like it is explaind and it doesn't work.. very frustrating sometimes...


My models.py:

class List01(models.Model):
    name = models.CharField(max_length=100, default='name')
    surename = models.CharField(max_length=100, default='surename')
    slug = models.SlugField()


views.py

class List(DetailView):
    template_name = 'index.html'
    model =List01

urls.py

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^(?P<slug>)$',List.as_view()),
)

Daniel Roseman

unread,
Feb 14, 2015, 9:57:32 AM2/14/15
to django...@googlegroups.com
On Saturday, 14 February 2015 04:28:40 UTC, inoyon artlover KLANGRAUSCH wrote:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^(?P<slug>)$',List.as_view()),
)

That regex will never match anything. You need to give it some character classes to match:

     url(r'^(?P<slug>[\w-]+)$',List.as_view()),

which will match any alphanumeric characters or a hyphen, which are the usual components of a slug.
--
DR.

Adailton (Dhelbegor)

unread,
Feb 14, 2015, 10:30:06 AM2/14/15
to django...@googlegroups.com
models.py

slug
= models.SlugField(verbose_name='Slug', unique=True)

@models.permalink
def get_absolute_url(self):
    return ('myapp:List', (), {'slug': self.slug})


admin
.py
from .models import List01


class ArticleAdmin(admin.ModelAdmin):
    prepopulated_fields
= {"slug": ("name",)}

admin
.site.register(List01, ArticleAdmin)

views
.py

from django.shortcuts import render, get_object_or_404
from .models import List01

def List(request, slug):
   list
= get_object_or_404(List01, slug=slug, released=True)
   context
= {'list': list} #[1]
   
return render(request, 'index.html', context)


urls
.py

url
(r'^(?P<slug>[\w-]+)/$', 'List', name='index'),

template
{% for lists in list %} # [1]
    <a href="{{lists.get_absolute_url}}"><h2>{{ lists.name }}</h2></a>
{% endfor %)
 

inoyon artlover KLANGRAUSCH

unread,
Feb 20, 2015, 1:58:49 AM2/20/15
to django...@googlegroups.com
Many thanks! Now I got it! :)

严超

unread,
Feb 20, 2015, 2:30:59 AM2/20/15
to django...@googlegroups.com
What is the specific error Django printed out ? We can narrow down the cause by checking the errors maybe.

Best Regards!
Chao Yan
--------------
About me http://about.me/chao_yan

My twitter@yanchao727
My Weibohttp://weibo.com/herewearenow
--------------

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/671c9f52-a48d-4529-a126-31542515dbf2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages