Common start of urls after the creation of a team account

(Mark) #1

I’m just working on a project in Django 1.11. I have a problem with how to make a common beginning of the url. For example, after creating a team, the address for all members should look like this:

Main url file in project:

urlpatterns = [
    url(r'blog', include('blog.urls', namespace='blog')),
    url(r'topics', include('topics.urls', namespace='topics')),
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^$', login_required(BlogHome.as_view()), name='blog_list'),
url(r'create/$', login_required(BlogCreate.as_view()), name='blog_create'),
url(r'^(?P<pk>[0-9]+)/delete/$', login_required(BlogDelete.as_view()), name='blog_delete'),
url(r'^(?P<pk>[0-9]+)/update/$', login_required(BlogUpdate.as_view()), name='blog_update'),
url(r'^(?P<pk>\d+)?/?$', login_required(BlogDetail.as_view()), name='blog_detail'),
    url(r'^$', login_required(TopicListView.as_view()), name='topic_list'),
    url(r'^create_topic/$', login_required(TopicCreateView.as_view()), name='topic_create'),
    url(r'^(?P<pk>\d+)?/?$', login_required(TopicDetailView.as_view()), name='topic_detail'),
    url(r'^(?P<pk>[0-9]+)/update/$', login_required(TopicUpdateView.as_view()), name='topic_update'),
    url(r'^(?P<pk>[0-9]+)/delete/$', login_required(TopicDeleteView.as_view()), name='topic_delete'),
    url(r'^follow/$', login_required(TopicFollowView.as_view()), name='topic_follow'),

Should I use here mixin?

(Vitor Freitas) #2

Ideally your Team model would need a slug field like this:

class Team(models.Model):
    name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=50, unique=True)

    def __str__(self):

Slug is a type of string field that doesn’t contain any space or special character. It’s also all lower case (like the URL of this page for instance).

So if your team name is Team Piña Colada the slug would be team-pina-colada.

Then you can do something like this on your URL patters:

urlpatterns = [
    url(r'(?P<slug>[-\w]+)/blog/', include('blog.urls', namespace='blog')),
    url(r'(?P<slug>[-\w]+)/topics/', include('topics.urls', namespace='topics')),
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),

(Mark) #3

Thanks for your help Vitor. My problem is also how to should looks a view with team slug and list of posts on the blog.