Author Picture

Kim Majali


Django Filter/ Search (No Package)

Author Avatar wrote on 03/06/2022

Urls.py


path('search', views.search, name='search'),
Read more

Django Search in and Results Page

Author Avatar wrote on 03/06/2022

Search Box on any template
Read more

CRUD Function-Based Views

Author Avatar wrote on 02/06/2022

1. Models.py


from django.db import models  
from django.urls import reverse  
from django.contrib.auth.models import User  
  
  
class Movies(models.Model):  
  
   user = models.ForeignKey(User, on_delete=models.CASCADE)  
   title = models.CharField(max_length=200)  
   genre = models.CharField(max_length=200)  
  
   def __unicode__(self):  
     return self.title  
  
   def get_absolute_url(self):  
     return reverse('CRUD_FBVs:movies_edit', kwargs={'pk': self.pk})
Read more

Django Pagination

Author Avatar wrote on 02/06/2022

Views.py


from django.shortcuts import render
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from .models import Listing

def index(request):
    listings = Listing.objects.order_by('-list_date').filter(is_published=True)
    paginator = Paginator(listings, 6)
    page = request.GET.get('page')
    paged_listings = paginator.get_page(page)
    context = {
        'listings' : paged_listings
    }
    return render(request, 'listings/listings.html', context)
   
Read more

Delete Objects

Author Avatar wrote on 02/06/2022

View

Short

record = User.objects.get(id=request.POST['id'])
record.delete()

# or
User.objects.get(id=request.POST['id']).delete()

# With try
try:
    record = User.objects.get(id=request.POST['id'])
    record.delete()
except Exception as e:
    print("Exception Update balance ==========================")
    print(e)
    print("End of Exception  ==========================")
Read more

Topics: ViewsModels

CRUDgen Django CRUD generator