You can also do:
if not RealEstateListing.objects.filter(slug_url=slug).exists():
# do stuff...
Sometimes it’s more clear to use try: except:
block and other times one-liner exists()
makes the code looking clearer… all depends on your application logic.
More Related Contents:
- View doesn’t return HttpResponse
- Hi I am new to Django Framework.getting error :No Module Named
- Why does DEBUG=False setting make my django Static Files Access fail?
- How to use permission_required decorators on django class-based views
- Django – after login, redirect user to his custom page –> mysite.com/username
- Django : How can I see a list of urlpatterns?
- Django get the static files URL in view
- Execute code in Django after response has been sent to the client
- django – get() returned more than one topic
- How to handle request.GET with multiple variables for the same parameter in Django
- django: Purpose of django.utils.functional.SimpleLazyObject?
- Provide extra context to all views
- Can I call a view from within another view?
- Raw SQL queries in Django views
- How can I list urlpatterns (endpoints) on Django?
- Example of Django Class-Based DeleteView
- django @login_required decorator for a superuser
- Redirect / return to same (previous) page in Django?
- Django – show loading message during long processing
- How to handle “matching query does not exist” when getting an object
- Forbidden (403) CSRF verification failed. Request aborted
- CSRF verification failed. Request aborted. on django
- Multiple form classes in django generic (class) views
- Django Admin Show Image from Imagefield
- Django: Get model from string?
- How to change field name in Django REST Framework
- ImageField overwrite image file with same name
- Django: accessing the model instance from within ModelAdmin?
- Django templates: verbose version of a choice
- How to add Check Constraints for Django Model fields?