I have a series of urls tied to Django's generic date views. In the extra_context parameter, I'd like to pass in a queryset based off the year/ month variables in the URLs, but I'm not sure how to access them. For example, in
url(r'^archive/(?P<year>20[1-2][0-9])/?$', archive_year,
{'queryset': Article.objects.all(),
'date_field': 'publication_date',
'template_name': 'articles/archive-date-list.html',
'extra_context': {'content': 'articles'}},
name='article_archive'),
I'd like to add in the 5 most recent articles where the publication date's year is gte year and lt year + 1. Ideally, the collection would be looked up on each request and not just cached at compile time. Am I better off writing a context processor for this/ extending the view?
You create a wrapper around the generic view:
# myapp/views.py
def my_archive_year(request, year):
# Logic to get the articles here
return archive_year(request,
year=year,
date_field='publication_date',
template_name='articles/archive-date-list.html',
extra_context = {'content': articles}
)
# urls.py
url(r'^archive/(?P<year>20[1-2][0-9])/?$', 'myapp.views.my_archive_year', name='article_archive'),
Related
I have a logging system, where users can log several symptoms for any given day. Im getting the date as slug in my url, but I need to validate it. Which one is best practice, and why?
make a validator function in the class view and use it there
add a hidden form field, and write a custom DateValidator for it?
You can define a path converter that will parse date objects. You can define a custom pattern with:
# app_name/converters.py
class DateConverter:
regex = '[0-9]{4}-[0-9]{2}-[0-9]{2}'
format = '%Y-%m-%d'
def to_python(self, value):
return datetime.strptime(value, self.format).date()
def to_url(self, value):
return value.strftime(self.format)
Next we can register that path converter [Django-doc] and work with:
from app_name.converters import DateConverter
from django.urls import path, register_converter
register_converter(DateConverter, 'date')
urlpatterns = [
# …
path('some/path/<date:date>/', some_view),
# …
]
This will pass a single date parameter to the view, which is a date object, you thus can work with:
def some_view(request, date):
# …
If you thus visit the path /some/path/2021-10-17, date will be a date(2021, 10, 17) object.
I have a workout calendar app where if a user goes to /workoutcal, I want them to be redirected to workoutcal/<today's year>/<today's month>. That means I want them to be redirected to this route, should they go to /workoutcal:
url(r'^(?P<year>[0-9]+)/(?P<month>[1-9]|1[0-2])$', views.calendar, name='calendar'),
So how can I write a new url pattern in urls.py that does something like:
url(r'^$', RedirectView().as_view(url=reverse_lazy(),todays_year, todays_month)),
You can subclass RedirectView, override get_redirect_url, and reverse the url there.
class MonthRedirectView(RedirectView):
def get_redirect_url(*args, **kwargs):
today = timezone.now()
return reverse(calendar, args=[today.year, today.month])
Then include your view in the URL pattern:
url(r'^$', MonthRedirectView().as_view()),
I found another solution to my problem, which is making a view that simply calls the calendar with the right arguments:
the url:
url(r'^$', views.redirect_to_calendar),
the redirect view:
def redirect_to_calendar(request):
today = timezone.now()
return calendar(request, year = today.year, month = today.month)
the view we serve to the user:
def calendar(request, year = None, month = None):
## A bunch of server logic
return HttpResponse(template.render(context, request))
Assuming your RedirectView is in your app's urls.py:
url(r'^(?P<year>[0-9]+)/(?P<month>[1-9]|1[0-2])$', views.calendar, name='calendar'),
url(r'^$', RedirectView().as_view(url='{}/{}'.format(todays_year, todays_month)),
EDIT: This assumes that todays_year and todays_month are calculated when a user goes to /workoutcal/, which they aren't (they're loaded at URL load time). See Alasdair's answer.
I am writing an application in Django, which uses [year]/[month]/[title-text] in the url to identitfy news items. To manage the items I have defined a number of urls, each starting with the above prefix.
urlpatterns = patterns('msite.views',
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/edit/$', 'edit'),
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/$', 'show'),
(r'^(?P<year>[\d]{4})/(?P<month>[\d]{1,2})/(?P<slug>[\w]+)/save$', 'save'),
)
I was wondering, if there is a mechanism in Django, which allows me to preprocess a given request to the views edit, show and save. It could parse the parameters e.g. year=2010, month=11, slug='this-is-a-title' and extract a model object out of them.
The benefit would be, that I could define my views as
def show(news_item):
'''does some stuff with the news item, doesn't have to care
about how to extract the item from request data'''
...
instead of
def show(year, month, slug):
'''extract the model instance manually inside this method'''
...
What is the Django way of solving this?
Or in a more generic way, is there some mechanism to implement request filters / preprocessors such as in JavaEE and Ruby on Rails?
You need date based generic views and create/update/delete generic views maybe?
One way of doing this is to write a custom decorator. I tested this in one of my projects and it worked.
First, a custom decorator. This one will have to accept other arguments beside the function, so we declare another decorator to make it so.
decorator_with_arguments = lambda decorator: lambda * args, **kwargs: lambda func: decorator(func, *args, **kwargs)
Now the actual decorator:
#decorator_with_arguments
def parse_args_and_create_instance(function, klass, attr_names):
def _function(request, *args, **kwargs):
model_attributes_and_values = dict()
for name in attr_names:
value = kwargs.get(name, None)
if value: model_attributes_and_values[name] = value
model_instance = klass.objects.get(**model_attributes_and_values)
return function(model_instance)
return _function
This decorator expects two additional arguments besides the function it is decorating. These are respectively the model class for which the instance is to be prepared and injected and the names of the attributes to be used to prepare the instance. In this case the decorator uses the attributes to get the instance from the database.
And now, a "generic" view making use of a show function.
def show(model_instance):
return HttpResponse(model_instance.some_attribute)
show_order = parse_args_and_create_instance(Order, ['order_id'])(show)
And another:
show_customer = parse_args_and_create_instance(Customer, ['id'])(show)
In order for this to work the URL configuration parameters must contain the same key words as the attributes. Of course you can customize this by tweaking the decorator.
# urls.py
...
url(r'^order/(?P<order_id>\d+)/$', 'show_order', {}, name = 'show_order'),
url(r'^customer/(?P<id>\d+)/$', 'show_customer', {}, name = 'show_customer'),
...
Update
As #rebus correctly pointed out you also need to investigate Django's generic views.
Django is python after all, so you can easily do this:
def get_item(*args, **kwargs):
year = kwargs['year']
month = kwargs['month']
slug = kwargs['slug']
# return item based on year, month, slug...
def show(request, *args, **kwargs):
item = get_item(request, *args, **kwargs)
# rest of your logic using item
# return HttpResponse...
I have multiple models that I want to create generic inputs for. My first pass used two separate urls:
url(r'^create_actor/$, create_object, {'model': Actor, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Actor'}, 'login_required': 'True'}),
url(r'^create_movie/$, create_object, {'model': Movie, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Movie'}, 'login_required': 'True'}),
I assume it would be much better to combine these into one statement. I'm not sure how to pass a variable from the url into the parameters such that the line would dynamically select the model based on the variable.
I haven't tried this, but you can use a variable to capture the value after create_ and have it automatically sent to the create_object view:
url(r'url(r'^create_(?P<model>\w+)/$, create_object, {'template_name': 'create.html', 'post_save_redirect': '/library/', 'login_required': 'True'})
You'll have to access this url as /create_Actor/ instead of /creat_actor/. I'm not sure how to get the extra_context key to work.
Hope this helps.
from django.db import models
url(r'^create_(?P<modelname>\w+)/$', generic_add),
def generic_add(request, modelname):
mdlnm_model = models.get_model('catalog',modelname)
return create_object(request,
model = mdlnm_model,
template_name = 'create.html',
post_save_redirect = '/library/',
login_required = 'True'
)
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.