How to customize the URL of Date-based generic views?
Here is my URL pattern:
news_info_month_dict = {
'queryset': Entry.published.filter(is_published=True),
'date_field': 'pub_date',
'month_format': '%m',
}
and
(r'^(?P<category>[-\w]+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+).html$',
'object_开发者_如何学编程detail', news_info_month_dict, 'news_detail'),
But they have an error likes this:
object_detail() got an unexpected keyword argument 'category'
Please help me. Thanks!
I think you'll have to write your own view in place of the generic object_detail
, something like this (untested)
import datetime
def view_entry(request, category, year, month, day, slug):
date = datetime.date(int(year), int(month), int(day))
entry = get_object_or_404(Entry, slug=slug, date=date, is_published=True, category=category)
return render_to_response('news_detail', {'object': entry})
Though it may be possible to do it with object_detail
I don't know - I very rarely use generic views.
In your URL regex, everything in <brackets>
is getting passed to the generic view as a keyword argument.
The problem is that the generic view you're using (object_detail
) doesn't support all of those arguments (namely, category
).
More information about the object_detail generic view and the arguments it accepts.
If you need a category
argument, just wrap the view as Nick suggested above and call that from your URLconf.
精彩评论