Django - reversing wrapped view functions - django

I am trying to incorporate django-schedule into my project. Django-schedule's source is here. I don't like the urls, because they all capture a slug. My project will only allow one calendar per user, so it doesn't make sense to capture the slug. So, I wrapped the django-schedule views like this (look up the slug using the current user, and pass it to django-schedule's views):
from schedule.views import calendar_by_periods
from schedule.models import Calendar
from schedule.periods import Month
def cal_by_periods_wrapper(view):
def new_view(request, *args, **kwargs):
kwargs['calendar_slug'] = Calendar.objects.get_calendars_for_object(obj=request.user, distinction="owner")[0].slug
return view(request, *args, **kwargs)
return new_view
And here is the relevant section from urls.py:
urlpatterns = patterns('',
url(r'^$',
cal_by_periods_wrapper(calendar_by_periods),
name = "month_calendar",
kwargs={'periods': [Month], 'template_name': 'schedule/calendar_month.html'}),
This works fine until it hits one of the template tags included with django-schedule, prev_url:
#register.simple_tag
def prev_url(target, slug, period):
return '%s%s' % (
reverse(target, kwargs=dict(calendar_slug=slug)),
querystring_for_date(period.prev().start))
This function raises:
TemplateSyntaxError at /teacher/calendar/
Caught an exception while rendering: Reverse for 'month_calendar' with arguments
'()' and keyword arguments '{'calendar_slug': u'asdf'}' not found.
How can I wrap this view and still make the reverse call work?

This has nothing to do with wrapping the function. It's just that you no longer have a URL with the name 'month_calendar' which takes a 'calendar_slug' argument. Either define one in your urlconf, or edit the templatetag.
Edit after comment Yes but the 'reverse' call is still passing a slug argument, and there's no 'month_calendar' url which takes one, so the reverse match fails.

Related

How do I change the view a URL resolves to after a certain date?

I'm writing a contest app. The contest closes at midnight on a particular date. I want to have the app automatically switch from: using a CookieWizardView, from formtools; to a normal TemplateView, from the generic view library.
Currently the relevant part of my urlpatterns looks like this:
urlpatterns += patterns('',
url(r'^$', 'appname.views.contest'), # the CookieWizardView
)
and I'd like it, after a certain date, to act as though it looks like this:
urlpatterns += patterns('',
url(r'^$', 'appname.views.contestclosed'), # a TemplateView
)
I am totally, totally fine with having a hard-coded magic number, I just don't want to be up at midnight that day!
~~
I solved this but can't answer my own question because I'm too new.
I made a function in my views.py:
def contest_switcher(request):
if datetime.datetime.now() < datetime.datetime(YEAR_OVER, MONTH_OVER, DAY_OVER):
return contest(request)
else:
return contestclosed(request)
This does the trick, now my urlpattern is:
urlpatterns += patterns('',
url(r'^$', 'appname.views.contest_switcher'),
)
I did have to add a function to my contest closed view, though, because it wasn't expecting a POST, which could happen if someone is trying to fill out the contest form at midnight:
class ContestClosedView(TemplateView):
template_name = "appname/closed.html"
def post(self, *args, **kwargs):
return self.get(*args, **kwargs)
contestclosed = ContestClosedView.as_view()
You don't have to try to hack your urls.py to pull this off. Set one URL pattern that points to a view that looks like this:
def contest_page(request, contest_id):
try:
contest = Contest.objects.get(pk=contest_id)
except Contest.DoesNotExist:
raise Http404 # minimum necessary - you can do better
if datetime.datetime.now() < contest.end_date: # model field rather than module constants
return contest(request, contest_id) # CookieWizardView
else:
return contestclosed(request, contest_id) # TemplateView
This is basically your contest_switcher with improvements:
Applies to multiple contests
Contests know their own end date so you don't clutter your module scope with constants
Simple urls.py and the view does the work of delegating what is shown (you know, the view)
(Note that this example implies that you would change your models correspondingly and import all the correct libraries and such.)

Getting #permalink decorator to work with django generic views?

Maybe I am missing something, but according to the django docs (1.2), I have setup my URLS models exactly as specified to ensure I am not hard-coding urls returned for get_absolute_url.
Here's what I have:
in urls.py
urlpatterns = patterns('django.views.generic.list_detail',
url(r'^$','object_list',
{ 'queryset': product.objects.all(),
'template_name': 'products/list.html',
},
name='product_list'),
url(r'^(?P<slug>[-\w]+)/$','object_detail',
{ 'queryset': product.objects.all(),
'template_name': 'products/detail.html',
},
name='product_detail'),
)
in models.py
#models.permalink
def get_absolute_url(self):
return ('product_detail', (), {'slug': str(self.slug)})
The method returns an empty string in the templates, and from the shell it gives an error.
NoReverseMatch: Reverse for 'product_detail' with arguments '()' and keyword arguments '{'slug': 'dd-d--'}' not found.
This should resolve should it not, since urls.py has a name : product_detail?
Syntax seems to be correct, are you sure your urls.py gets included? Try stepping in debuggin in view code and use reverse function first to generate the url.
My blind guess would be, something is wrong with your urls.py file in general.
Try changing this line:
url(r'(?P<slug>[-\w]+)/^$','object_detail',
to
url(r'^(?P<slug>[-\w]+)/$','object_detail',
Carret (^) stands for beginning of the line, so it is illogical in the context you wrote it since it means the line has content before it even begins.

How to write a request filter / preprocessor in Django

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...

Problem using generic views in django

I'm currently working with django generic views and I have a problem I can't figure out.
When using delete_object I get a TypeError exception:
delete_object() takes at least 3 non-keyword arguments (2 given)
Here is the code (I have ommited docstrings and imports):
views.py
def delete_issue(request, issue_id):
return delete_object(request,
model = Issue,
object_id = issue_id,
template_name = 'issues/delete.html',
template_object_name = 'issue')
urls.py
urlpatterns = patterns('issues.views',
(r'(?P<issue_id>\d+)/delete/$', 'delete_issue'),
)
The other generic views (object_list, create_object, etc.) work fine with those parameters. Another problem I have is when using the create_object() function, it says something about a CSRF mechanism, what is that?
You need to provide post_delete_redirect, this means url, where user should be redirected after object is deleted. You can find this in view signature:
def delete_object(request, model, post_delete_redirect, object_id=None,
slug=None, slug_field='slug', template_name=None,
template_loader=loader, extra_context=None, login_required=False,
context_processors=None, template_object_name='object'):

TypeError when passing dictionary arguments to a view through urls.py

I'm trying to pass keyword arguments to a Django view using a dictionary, but I keep running into a TypeError when I try to access the URL (The error is: "add_business_contact() got an unexpected keyword argument 'info_models'"). The code is:
urlpatterns = patterns('business.views',
# ...
url(r'^(?P<business_id>[\w\._-]+)/edit_contact$', 'add_business_contact', {
'info_models': [Email, PhoneNumber, URL] }, name='business_contact'),
# ...
)
and the corresponding view:
#login_required
def add_business_contact(request, business_id, *args, **kwargs):
# ...
info_models = kwargs.pop('info_models', None)
# ....
If I remove the dictionary argument from the url() function, it happily reaches and runs the view (albeit incorrectly since it doesn't have that argument). Any ideas as to why it's doing this? I'm following an example from the Django Book ( http://djangobook.com/en/2.0/chapter08/ ) if that helps at all.
Wooops. A bit embarassing but I copy/pasted the function while working on it and didn't rename the original. It's working now as expected...