Django DetailView without 404 redirect - django

I have a little news app with a DetailView class like the following:
class DetailView(LoginRequiredMixin,generic.DetailView):
model = NewsItem
template_name = 'news/detail.html'
def get_object(self):
obj = super().get_object()
if self.request.user.is_superuser or obj.published:
return obj
and an urls.py config like this:
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:itemId>/publish', views.publish, name='publish'),
]
Now when i pass an invalid ID to the detail view it automatically redirects to 404. I wanted to know if it's possible to just pass an empty object to the DetailView instead. The template then would handle the 404 on its own.
Also:Even though it works so far I feel like the way i handled the permission requirements (overriding the get_object method) isn't the correct way/Django way to do things
Solution: i changed the overriding of get_object like this:
class DetailView(LoginRequiredMixin,generic.DetailView):
model = NewsItem
template_name = 'news/detail.html'
def get_object(self):
queryset = self.get_queryset()
pk = self.kwargs.get(self.pk_url_kwarg)
if pk is not None:
queryset = queryset.filter(pk=pk)
else:
raise AttributeError("No article id provided")
try:
return queryset.get()
except queryset.model.DoesNotExist:
return None
it is basically the same get_object method as the original just without the check for a slug value, and if the queryset.get() call catches the queryset.model.DoesNotExist Exception it returns None

Classy CBVs, as usual, makes all clear.
If you don't like 404 for a bad id, you need to override get_object to a greater extent than you have done, because it raises the exception Http404 for a bad id. The code in question is
try:
# Get the single item from the filtered queryset
obj = queryset.get()
except queryset.model.DoesNotExist:
raise Http404(_("No %(verbose_name)s found matching the query") %
{'verbose_name': queryset.model._meta.verbose_name})
return obj
You could catch the exception when you call obj = super().get_object()
try:
obj = super().get_object()
except Http404:
obj = NewsItem( ... ) # a dummy empty NewsItem
Or you could simply not call super() at all, and supply your own code to return an appropriate object. The empty object might exist in the database as a special entity (best created by a data migration), or it might (as here) be created on the fly and never saved. In either case it would have some distinguishing characteristic that the template can easily test.

Related

HttpResponse error django generic template

I am able to render class based view generic ListView template using parameter hard coded in views.py.
class ResourceSearchView(generic.ListView):
model = creations
context_object_name = 'reviews'
template_name = 'reviews.html'
query = 'theory'
# def get(self, request):
# if request.GET.get('q'):
# query = request.GET.get('q')
# print(query)
queryset = creations.objects.filter(narrative__contains=query).order_by('-post_date')
However, when parameter is sent via form by GET method (below),
class ResourceSearchView(generic.ListView):
model = creations
context_object_name = 'reviews'
template_name = 'reviews.html'
query = 'theory'
def get(self, request):
if request.GET.get('q'):
query = request.GET.get('q')
print(query)
queryset = creations.objects.filter(narrative__contains=query).order_by('-post_date')
I receive this error
The view creations.views.ResourceSearchView didn't return an
HttpResponse object. It returned None instead.
Note that the parameter name q and associated value is being retrieved successfully (confirmed using print(query)).
So with CBV in Django, you have to return some kind of valid response that the interpreter can use to perform an actual HTTP action. Your GET method isn't returning anything and that's what is making Django angry. You can render a template or redirect the user to a view that renders a template but you must do something. One common pattern in CBV is to do something like:
return super().get(request, *args, **kwargs)
...which continues up the chain of method calls that ultimately renders a template or otherwise processes the response. You could also call render_to_response() directly yourself or if you're moving on from that view, redirect the user to get_success_url or similar.
Have a look here (http://ccbv.co.uk) for an easy-to-read layout of all the current Django CBVs and which methods / variables they support.
Thanks for the responses. Here is one solution.
class ResourceSearchView(generic.ListView):
model = creations
context_object_name = 'reviews'
template_name = 'reviews.html'
def get_queryset(self):
query = self.request.GET.get('q')
queryset = creations.objects.filter(narrative__contains=query).order_by('-post_date')
return queryset

Django: 1.5: Overriding generic.DetailView returns 404

I have a generic.DetailView which I'm trying to override the function get_queryset(self) I'm doing the following:
class MyView(generic.DetailView):
model = MyModel
slug_field='guid'
template_name = 'myhtml.html'
def get_queryset(self):
guid = uuid.UUID(self.kwargs.get('slug'))
x = MyModel.objects.all().filter(guid=guid)
pprint(x)
return MyModel.objects.all().filter(guid=guid)
Here is the urls.py:
urlpatterns = patterns('',
url (r'^(?P<slug>[A-Fa-f0-9]{30,32})$', view.MyView.as_view(), name='myview'),
)
When I run the page, I keep getting a 404 error. However, I know MyModel returns something because pprint returns:
[<MyModel: mydata>]
PS If I do this same modification w/ PK: I still get a 404:
def get_queryset(self):
return MyModel.objects.all().filter(id=pk)
...and I know MyModel.objects.all().filter(id=pk) returns data
What am I missing?
Thanks
You're getting 404 because get_object() is run right after get_queryset(), which filters the queryset again (using slug url kwarg), giving you no results.
You should instead override get_object() method and return a single object.
Something like this should work:
def get_object(self, queryset=None):
slug = self.kwargs.get(self.slug_url_kwarg, None)
guid = uuid.UUID(slug)
return queryset.get(guid=guid)
NOTE:
This is just a quick example, you should definitely look into original get_object() method and see how to handle all the edge cases and exceptions.

Django: extend get_object for class-based views

Being a non-expert Python programmer, I'm looking for feedback on the way I extended the get_object method of Django's SingleObjectMixin class.
For most of my Detail views, the lookup with a pk or slugfield is fine - but in some cases, I need to retrieve the object based on other (unique) fields, e.g. "username". I subclassed Django's DetailView and modified the get_object method as follows:
# extend the method of getting single objects, depending on model
def get_object(self, queryset=None):
if self.model != mySpecialModel:
# Call the superclass and do business as usual
obj = super(ObjectDetail, self).get_object()
return obj
else:
# add specific field lookups for single objects, i.e. mySpecialModel
if queryset is None:
queryset = self.get_queryset()
username = self.kwargs.get('username', None)
if username is not None:
queryset = queryset.filter(user__username=username)
# If no username defined, it's an error.
else:
raise AttributeError(u"This generic detail view %s must be called with "
u"an username for the researcher."
% self.__class__.__name__)
try:
obj = queryset.get()
except ObjectDoesNotExist:
raise Http404(_(u"No %(verbose_name)s found matching the query") %
{'verbose_name': queryset.model._meta.verbose_name})
return obj
Is this good practise? I try to have one Subclass of Detailview, which adjusts to differing needs when different objects are to be retrieved - but which also maintains the default behavior for the common cases. Or is it better to have more subclasses for the special cases?
Thanks for your advice!
You can set the slug_field variable on the DetailView class to the model field that should be used for the lookup! In the url patterns it always has to be named slug, but you can map it to every model field you want.
Additionally you can also override the DetailView's get_slug_field-method which only returns self.slug_field per default!
Can you use inheritance?
class FooDetailView(DetailView):
doBasicConfiguration
class BarDetailView(FooDetailView):
def get_object(self, queryset=None):
doEverythingElse

How to do a DetailView in django 1.3?

I'm currently learning how to use the class-based views in django 1.3. I'm trying to update an application to use them, but I still don't uderstand very well how they work (and I read the entire class-based views reference like two or three times EVERY day).
To the question, I have an space index page that needs some extra context data, the url parameter is a name (no pk, and that can't be changed, it's the expected behaviour) and the users that don't have that space selected in their profiles can't enter it.
My function-based code (working fine):
def view_space_index(request, space_name):
place = get_object_or_404(Space, url=space_name)
extra_context = {
'entities': Entity.objects.filter(space=place.id),
'documents': Document.objects.filter(space=place.id),
'proposals': Proposal.objects.filter(space=place.id).order_by('-pub_date'),
'publication': Post.objects.filter(post_space=place.id).order_by('-post_pubdate'),
}
for i in request.user.profile.spaces.all():
if i.url == space_name:
return object_detail(request,
queryset = Space.objects.all(),
object_id = place.id,
template_name = 'spaces/space_index.html',
template_object_name = 'get_place',
extra_context = extra_context,
)
return render_to_response('not_allowed.html', {'get_place': place},
context_instance=RequestContext(request))
My class-based view (not working, and no idea how to continue):
class ViewSpaceIndex(DetailView):
# Gets all the objects in a model
queryset = Space.objects.all()
# Get the url parameter intead of matching the PK
slug_field = 'space_name'
# Defines the context name in the template
context_object_name = 'get_place'
# Template to render
template_name = 'spaces/space_index.html'
def get_object(self):
return get_object_or_404(Space, url=slug_field)
# Get extra context data
def get_context_data(self, **kwargs):
context = super(ViewSpaceIndex, self).get_context_data(**kwargs)
place = self.get_object()
context['entities'] = Entity.objects.filter(space=place.id)
context['documents'] = Document.objects.filter(space=place.id)
context['proposals'] = Proposal.objects.filter(space=place.id).order_by('-pub_date')
context['publication'] = Post.objects.filter(post_space=place.id).order_by('-post_pubdate')
return context
urls.py
from e_cidadania.apps.spaces.views import GoToSpace, ViewSpaceIndex
urlpatterns = patterns('',
(r'^(?P<space_name>\w+)/', ViewSpaceIndex.as_view()),
)
What am I missing for the DetailView to work?
The only problem I see in your code is that your url's slug parameter is named 'space_name' instead of 'slug'. The view's slug_field attribute refers to the model field that will be used for slug lookup, not the url capture name. In the url, you must name the parameter 'slug' (or 'pk', when it's used instead).
Also, if you're defining a get_object method, you don't need the attributes queryset, model or slug_field, unless you use them in your get_object or somewhere else.
In the case above, you could either use your get_object as you wrote or define the following, only:
model = Space
slug_field = 'space_name'

The default "delete selected" admin action in Django

How can I remove or change the verbose name of the default admin action "delete selected X item" in the Django admin panel?
Alternatively to Googol's solution, and by waiting for delete_model() to be implemented in current Django version , I suggest the following code.
It disables the default delete action for current AdminForm only.
class FlowAdmin(admin.ModelAdmin):
actions = ['delete_model']
def get_actions(self, request):
actions = super(MyModelAdmin, self).get_actions(request)
del actions['delete_selected']
return actions
def delete_model(self, request, obj):
for o in obj.all():
o.delete()
delete_model.short_description = 'Delete flow'
admin.site.register(Flow, FlowAdmin)
You can disable the action from appearing with this code.
from django.contrib import admin
admin.site.disable_action('delete_selected')
If you chose, you could then restore it on individual models with this:
class FooAdmin(admin.ModelAdmin):
actions = ['my_action', 'my_other_action', admin.actions.delete_selected]
Not sure if this sort of monkey-patching is a good idea, but shoving this in one of my admin.py works for me:
from django.contrib.admin.actions import delete_selected
delete_selected.short_description = u'How\'s this for a name?'
This will change the verbose name for all your admin sites. If you want to change it just for one particular model's admin, I think you'll need to write a custom admin action.
Tested with Django version 1.1:
>>> import django
>>> django.VERSION
(1, 1, 0, 'beta', 1)
For globally changing delete_selected's short_description Dominic Rodger's answer seems best.
However for changing the short_description on the admin for a single model I think this alternative to Stéphane's answer is better:
def get_actions(self, request):
actions = super().get_actions(request)
actions['delete_selected'][0].short_description = "Delete Selected"
return actions
In order to replace delete_selected I do the following:
Copy the function delete_selected from contrib/admin/actions.py to your admin.py and rename it. Also copy the template contrib/admin/templates/delete_selected_confirmation.html to your template directory and rename it. Mine looks like this:
def reservation_bulk_delete(modeladmin, request, queryset):
"""
Default action which deletes the selected objects.
This action first displays a confirmation page whichs shows all the
deleteable objects, or, if the user has no permission one of the related
childs (foreignkeys), a "permission denied" message.
Next, it delets all selected objects and redirects back to the change list.
"""
opts = modeladmin.model._meta
app_label = opts.app_label
# Check that the user has delete permission for the actual model
if not modeladmin.has_delete_permission(request):
raise PermissionDenied
# Populate deletable_objects, a data structure of all related objects that
# will also be deleted.
# deletable_objects must be a list if we want to use '|unordered_list' in the template
deletable_objects = []
perms_needed = set()
i = 0
for obj in queryset:
deletable_objects.append([mark_safe(u'%s: %s' % (escape(force_unicode(capfirst(opts.verbose_name))), obj.pk, escape(obj))), []])
get_deleted_objects(deletable_objects[i], perms_needed, request.user, obj, opts, 1, modeladmin.admin_site, levels_to_root=2)
i=i+1
# The user has already confirmed the deletion.
# Do the deletion and return a None to display the change list view again.
if request.POST.get('post'):
if perms_needed:
raise PermissionDenied
n = queryset.count()
if n:
for obj in queryset:
obj_display = force_unicode(obj)
obj.delete()
modeladmin.log_deletion(request, obj, obj_display)
#queryset.delete()
modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
"count": n, "items": model_ngettext(modeladmin.opts, n)
})
# Return None to display the change list page again.
return None
context = {
"title": _("Are you sure?"),
"object_name": force_unicode(opts.verbose_name),
"deletable_objects": deletable_objects,
'queryset': queryset,
"perms_lacking": perms_needed,
"opts": opts,
"root_path": modeladmin.admin_site.root_path,
"app_label": app_label,
'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
}
# Display the confirmation page
return render_to_response(modeladmin.delete_confirmation_template or [
"admin/%s/%s/reservation_bulk_delete_confirmation.html" % (app_label, opts.object_name.lower()),
"admin/%s/reservation_bulk_delete_confirmation.html" % app_label,
"admin/reservation_bulk_delete_confirmation.html"
], context, context_instance=template.RequestContext(request))
As you can see I commented out
queryset.delete()
and rather use:
obj.delete()
That's not optimal yet - you should apply something to the entire queryset for better performance.
In admin.py I disable the default action delete_selected for the entire admin site:
admin.site.disable_action('delete_selected')
Instead I use my own function where needed:
class ReservationAdmin(admin.ModelAdmin):
actions = [reservation_bulk_delete, ]
In my model I define the delete() function:
class Reservation(models.Model):
def delete(self):
self.status_server = RESERVATION_STATUS_DELETED
self.save()
http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-action
from django.contrib.admin import sites
from django.contrib.admin.actions import delete_selected
class AdminSite(sites.AdminSite):
"""
Represents the administration, where only authorized users have access.
"""
def __init__(self, *args, **kwargs):
super(AdminSite, self).__init__(*args, **kwargs)
self.disable_action('delete_selected')
self.add_action(self._delete_selected, 'delete_selected')
#staticmethod
def _delete_selected(modeladmin, request, queryset):
_delete_qs = queryset.delete
def delete():
for obj in queryset:
modeladmin.delete_model(request, obj)
_delete_qs()
queryset.delete = delete
return delete_selected(modeladmin, request, queryset)
class FooAdmin(sites.AdminSite):
not_deleted = ['value1', 'value2']
actions = ['delete_selected_values']
def delete_selected_values(self, request, queryset):
# my custom logic
exist = queryset.filter(value__in=self.not_deleted).exists()
if exist:
error_message = "Error"
self.message_user(request, error_message, level=messages.ERROR)
else:
delete_action = super().get_action('delete_selected')[0]
return delete_action(self, request, queryset)
delete_selected_values.short_description = 'delete selected'
admin.site.register(Foo, FooAdmin)