CSSS / old-csss-site

The previous website for Simon Fraser University's CSSS.
http://sfucsss.org/
3 stars 9 forks source link

Category names with spaces in them break routing regex #3

Closed fosskers closed 9 years ago

fosskers commented 9 years ago

Django gives:

Reverse for 'posts_in_category' with arguments '('Comp Sci Guide',)'
and keyword arguments '{}' not found. 1 pattern(s) tried: ['cms/(?P<name>\\w+)/$']
asabaya3 commented 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

fosskers commented 9 years ago

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.

asabaya3 commented 9 years ago

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

asabaya3 commented 9 years ago

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 -