Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - url structure
    text
    copied!<p>How should I design url's or views to keep url structure like in the example below?</p> <pre><code>example.com/location/ example.com/category/ example.com/location/category/ url(r'^$', IndexView.as_view(), name='index'), url(r'^(?P&lt;town&gt;[\w\-_]+)/$', TownView.as_view(), name="town_detail"), url(r'^(?P&lt;category_slug&gt;[\w\-_]+)/$', CategoryView.as_view(), name="category_list"), </code></pre> <p>When I try to access url under category I am getting routed to TownView which is acceptable because the url patterns are almost the same.</p> <p>Should the category be put under example.com/c/category/ ?</p> <p>Edit:</p> <p>I did resolve my problem in a way I'm showing below. All answers are really good and helpful.</p> <p>I will have to validate how this solution will act and check if it's causing any problems.</p> <pre><code>url(r'^(?P&lt;slug&gt;[\w\-_]+)/$', BrowseView.as_view(), name="category_list"), url(r'^(?P&lt;slug&gt;[\w\-_]+)/$', BrowseView.as_view(), name="town_detail"), class BaseView(ListView): queryset = Advert.objects.all().select_related('category', ) template_name = "adverts/category_view.html" class BrowseView(BaseView): def get_queryset(self, *args, **kwargs): qs = super(BrowseView, self).get_queryset(*args, **kwargs) try: category = Category.objects.get(slug=self.kwargs['slug']) except ObjectDoesNotExist: object_list = qs.filter(location__slug=self.kwargs['slug']) else: category_values_list = category.get_descendants(include_self=True).values_list('id', flat=True) object_list = qs.filter(category__in=category_values_list) return object_list </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload