Closed fosskers closed 9 years ago
Name uses the \w+ to search for a word. Multiple word names don't work. Do URLs even accept spaces in links? I think a slug field would be better than just using a string name for urls. So it'd direct it to 'cms/Comp-Sci-Guide' instead of 'cms/Comp Sci Guide'. Would probably need to change the regex to [-\w]+ instead of just \w, which only matches A-Z,a-z,0-9
We should have multi-word categories. For the name to be foo bar baz
but for it to accept foo-bar-baz
for the routing, what has to change?
Usually urls can have spaces, but they have to be escaped to some code no human bothers remembering.
Would just have to change the model to:
class Category(models.Model):
# same as before just add
slug = models.SlugField()
And change the view to
class PostListView(generic.ListView):
def get_queryset(self):
return Post.objects.filter(category__slug = self.kwargs['slug'])
And edit the urls.py abit to
url(r'^(?P<slug>[-\w]+)/$', views.PostListView.as_view(), name='posts_in_category'),
ez pz lemon squeezy
We can even add to admin.py
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("name",)}
so that slug is automatically a sluged version of the name. i.e. spaces are converted to -
Django gives: