There are two models: groups
and posts
. The post can be of one of two kinds: assigned to the group, and not assigned to the group. The foreign key field in posts
model, which assigns the group to the post, is optional. The post, which belongs to a group, should have a different address than one, which does not belong to the group. Here is the general rule. The former address is when a post does not belong to any group and the latter one if it does.
.../posts/[year]/[month]/[day]/[slug of the post]/
.../groups/[name of the group]/posts/[year]/[month]/[day]/[slug of the post]/
The problem appears. After creating the post both url addresses work.
It is not a surprise, because I include
the urls from posts
app in urls.py
in groups
app.
What I want to do is to make the url addresses optional regarding to the group
parameter (whether it is passed or not). It can work as something like:
- if groups parameter is not None choose the url from
groups.urls
- if groups parameter is not None exclude url from
posts.urls
Basically I want to choose just one url, not two.
Is it possible in Django to do it in a simple way? Or is the only solution creating two models/two apps instead of one?
Excerpt of my code:
groups/urls.py
app_name = 'groups'
urlpatterns = [
...
path('<slug:slug>/posts/', include('posts.urls', namespace='grouppost')),
...
]
posts/urls.py
app_name = 'posts'
urlpatterns = [
path('', views.PostList.as_view(), name='list'),
path('create', views.CreatePost.as_view(), name='create'),
path('<int:year>/<int:month>/<int:day>/<slug:slug>/', views.PostDetail.as_view(), name='detail'),
path('<int:year>/<int:month>/<int:day>/<slug:slug>/update/', views.UpdatePost.as_view(), name='update'),
path('<int:year>/<int:month>/<int:day>/<slug:slug>/delete/', views.DeletePost.as_view(), name='delete'),
]
I am using Django 2.0.7.
Update 16.08
I also include
the path
in mu root urls.py
. Sorry for the omission of this important information.
urls.py
urlpatterns = [
...
path('groups/', include('groups.urls')),
path('posts/', include('posts.urls')),
...
]