Combining views

I have two views that work well seperately, there is a view which accepts a ‘tag’ and shows only the results that match the tag. This is using the ‘ArchiveIndexView’ generic view, mostly so I can use the date_list to show over which weeks those tags have occured.

I also have a view which takes a specific week (using the WeekArchiveView generic view) and shows results for that week.

urlpatterns = [
  path('tag/<slug>/', TagDetailView.as_view(),name="tag_view"),

Conceptually, how am I best to combine these so I can go to a week using the archive_week view, AND then drill into one the tags? Do I need to change the tagging so that it’s provided as a GET variable in the URL, rather than having it’s own specific URL? Something along the lines of localhost:8000/2022/week/7/tag=news

Just wanting to be pointed in the right direction to the pertinent django documentation that covers this please.

Many thanks

What specifically do you mean by this? Are you saying that you want to create the archive_week view, and then have the links take you to another page that is also filtered by tag?

If so, you could create a path:
path('<int:year>/week/<int:week>/<tag_name>/', ...)

Hi Ken, that’s exactly what I’m after. I’d like to have it support more than just tag, is a view per url the only way to achieve this?

I’ve only just starting to get my feet wet with class based views and I was under the impression doing it this way would save having to rewrite a lot of the same things.

path('<int:year>/week/<int:week>/<tag_name>/', ...)
path('<int:year>/week/<int:week>/<source_name>/', ...) (what is the source of the object)
path('<int:year>/week/<int:week>/<saved>/', ...) (has the user saved it). 

Each of these require only slight changes to the query filters. I guess I’m getting a bit lost in the number of potential combinations here, ie has the user filtered on tag or source, and then do they only want to see a particular date, and only objects they have saved?


Is a possible approach to use GET variables in view self.request.GET.get("saved") which are optional? That way rather than bucket lots of views, I can have the same view with logic to look for the presence of these optional variables?


You’re going to need to be a lot more clear, and specific, as to what all you’re trying to do here.

You’re touching on at least three different topics so far in this discussion, each of which are potentially conversations of their own. But yes, using GET variables is a common method of providing different combinations of parameters to be available to a view.

I’ll have a crack at re-explaining the predicament. I’m building a news aggregator, amongst the things it collects is the news source, tags (based on keywords I’ve defined), and the published_date. I then allow a user to save an article if it’s of interest to them.

I built views for:

localhost:800/tag/Microsoft/ (show all articles that match a tag)
localhost:800/source/TheRegister/ (show all articles that match a source)
localhost:800/saved/ (show all articles saved by that user)
localhost:800/2022/week/7/ (show all articles that occur in a particular week).

All of these views work great on their own, but I’m struggling to combine them all given the possible number of combinations. What if we want a tag and a date, how about a tag, a date AND saved articles? How about a source and saved (no date?) etc etc. I’m using ArchiveIndexView on some of the views, and WeekArchiveView on some of date related, but I suppose that doesn’t matter too much in thc context of this discussion.

Wondering if there are any shortcuts with setting these up? I loved django’s simplicity but I feel like this could get a little unwieldy if I write a different view for every different combination.


For this, yes, query variables would be the way to go - especially if you want to allow for combinations.

Note that the GET attribute on the HttpRequest object works like a dict, making it really easy to build your query from whatever parameters are supplied.