Posts

Showing posts from July, 2021

Search

http://127.0.0.1:8000/search/?q=Django html         < form   action = "{% url 'search' %}"   class = "d-flex"   method = "GET">           < input   class = "form-control me-2"   type = "text"   placeholder = "Search" aria-label="Search" name="q">           < button   class = "btn btn-outline-success"   type = "submit" >Sea rch</button>         </ form > views.py def   search ( request ):   q = request. GET .get( 'q' )   posts = Post.objects.filter(     Q( title__icontains = q)  |  Q( overview__icontains = q)   ).distinct()   param = {      'posts' :posts,      'pop_post' :Post.objects.order_by(...

Query set

  product = Product.objects.all() topwears  =  Product.objects.filter( category = 'TW' ) userCart=Cart.objects.filter( user = request.user) listUsed=Product.objects.filter( id__in = ids)# When We take list in 'ids' product  =  Product.objects.get( pk = pk) from  django.db.models  import  Q item_already_in_cart  =  Cart.objects.filter(Q( product = product.id)  &  Q( user = request.user)).exists() trends = Post.objects.filter( time_upload__gte = week_ago).order_by( '-read' )[: 5 ] trends = Post.objects.filter().order_by( '-read' )[: 5 ] TopAuthors = Author.objects.order_by( '-rate' )[: 4 ] < div   class = "owl-carousel mt-3" >   {% for post in posts|slice:":5" %}   < div >     < div   class = "row" >       < div   class = "col-md-6" >         < im...

Django With Document

Image
 model.py  my_file  =  models.FileField( upload_to = 'doc' ,  blank = True ) views.py class  HomeView( View ):     def   get ( self ,  request ):       form  =  ResumeForm()       candidates  =  Resume.objects.all()        return  render(request,  'myapp/home.html' , {  'candidates' :candidates,  'form' :form})         def   post ( self ,  request ):       form  =  ResumeForm(request. POST , request. FILES )        if  form.is_valid():          form.save()           return  render(request,  'myapp/home.html' , { 'form' :form}) urls.py from  django.conf  import  set...

filter and simple_tag

Image
***Basically   filter is used for 1 argument, and simple_tag is used more argument filter {% load cart %} {% load carancy %}  < td >{{product.price|currency}}</ td >   < td >{{ product |cart_quantity: request.session.cartt |currency}}</ td >       ** product ---------------------->First Value       ** request.session.cartt --------->Second Argument templatetags      carancy.py from  django  import  template register  =  template.Library() @register.filter ( name = 'currency' ) def   currency ( number ):    return   "$" + str (number) cart.py @register.filter ( name = 'cart_quantity' ) def   cart_quantity ( product , cart ):     keys  =  cart.keys()      for   id   in  keys:          if   int ( id )...

Session

def   home ( request ):   ct  =  request.session.get( 'count' ,  0 )  # Session jodi thaka tahola get hoba, na hola set hoya get hoba    print (ct)   newcount  =  ct  +   1   request.session[ 'count' ]  =  newcount  # Session set kora ho66a    return  render(request,  'mycount/home.html' , { 'c' :newcount}) Session Set request.session[ 'customar' ] = customer.id Session Get request.session.get( ' customar ' ) Session Clear request.session.clear() Set empty dictionary into session request.session[ ' customar ' ] = {} ** Session store key value pair Session Access in HTML request.session. customar {'1':10,'2':6,'3':4,'5':12}

Django Table(Bootstrap)

Image
    < div   class = "col-sm-7 offset-1" >   < h4   class = "text-center alert alert-info" >Show Student Information</ h4 >   {% if stu %}    < table   class = "table table-hover" >      < thead >        < tr >          < th   scope = "col" >ID</ th >          < th   scope = "col" >Name</ th >          < th   scope = "col" >Email</ th >          < th   scope = "col" >Password</ th >          < th   scope = "col" >Action</ th >        </ tr > ...

Common

from  django.shortcuts  import  render,HttpResponseRedirect,redirect def   skill ( request ):   context  =  { 'skill' :  'active' }    return  render(request,  'edu/skill.html' , context)     return  HttpResponseRedirect( '/' )   return  redirect( '/cart' ) http://127.0.0.1:8000/course/Express_course?lecture=2 < a   href = "/?category={{category.id}}"   class = "list-group-item list-gr oup-item-action" >{{category.name}}</ a > serial_number = request. GET .get( 'lecture' ) #2  

Django_(Login, Registration...)

forms.py  from  django  import  forms from  django.contrib.auth.forms  import  UserCreationForm from  django.contrib.auth.forms  import  UserCreationForm, AuthenticationForm, PasswordChangeForm, UsernameField, PasswordResetForm, SetPasswordForm from  django.contrib.auth  import  password_validation from  django.utils.translation  import  gettext, gettext_lazy  as  _ from  django.contrib.auth.models  import  User # Customer Registration class  CustomerRegistrationForm( UserCreationForm ):   password1  =  forms.CharField( label = 'Password' ,  widget = forms.PasswordInput( attrs = { 'class' : 'form-control' }))   password2  =  forms.CharField( label = 'Confirm Password (again)' ,  widget = forms.PasswordInput( attrs = { 'class' : 'form-control' }))   email  =  fo...

Django_Import

from django.contrib import admin from django.urls import path, include from django.views import View from django.shortcuts import render,redirect, HttpResponse,  HttpResponseRedirect from django  import template import math from django.conf import settings from django.conf.urls.static import static from django.contrib.auth.models import User from django.http import Http404 from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger from django.db.models import Q import datetime from django.contrib.auth.hashers import make_password,check_password from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.contrib import messages from django.http import JsonResponse from django import forms from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, UsernameField, PasswordResetForm, SetPasswordForm from django.contrib.auth.models import User from django.utils.translation...

Django_model & form field

     name = models.CharField( max_length = 50 ,  null = False )   slug = models.CharField( max_length = 50 ,  null = False ,  unique = True )   discount = models.IntegerField( null = False ,  default = 0 )   active = models.BooleanField( default = False )   thumbnail = models.ImageField( upload_to = "thumbnail" )   date = models.DateTimeField( auto_now_add = True )   resource = models.FileField( upload_to = "resource" )   length = models.IntegerField( null = False )    # myimage is a folder   photo = models.ImageField( upload_to = "myimage" )    # auto_now_add=True ---->akhon kato somoy sata add hoya jaba   date = models.DateTimeField( auto_now_add = True )   email = models.EmailField( max_length = 50 )    publish = models.BooleanField( default = 0 )  ...

Django_with_Image

models.py   thumbnail=models.ImageField(upload_to="thumbnail") settings.py  MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / 'media' urls.py from django.conf import settings from django.conf.urls.static import static urlpatterns = [ ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) models.py from  django.db  import  models # Create your models here. class  Image( models . Model ):    # myimage is a folder   photo = models.ImageField( upload_to = "myimage" )    # auto_now_add=True ---->akhon kato somoy sata add hoya jaba   date = models.DateTimeField( auto_now_add = True ) forms.py from  django  import  forms from  .models  import  Image class  ImageForm( forms . ModelForm ):    class  Meta:     model = Image     fields = '__all__' ...

Django_Views

FunctionBased View def add_show(request):  if request.method == 'POST':   fm = StudentRegistration(request.POST)   if fm.is_valid():    nm = fm.cleaned_data['name']    em = fm.cleaned_data['email']    pw = fm.cleaned_data['password']    reg = User(name=nm, email=em, password=pw)    reg.save()    fm = StudentRegistration()  else:   fm = StudentRegistration()  stud = User.objects.all()  return render(request, 'enroll/addandshow.html', {'form':fm, 'stu':stud}) def   update_data ( request ,  id ):   if  request.method  ==   'POST' :   pi  =  User.objects.get( pk = id )   fm  =  StudentRegistration(request. POST ,  instance = pi)    if  fm.is_valid():    fm.save()   else :   pi  =  User.objects.get( pk = id )   fm  =  StudentRegistration( instance = p...

Django_ModelForm

 Models.py from  django.db  import  models # Create your models here. class  User( models . Model ):   cname  =  models.CharField( max_length = 70 )   jtitle  =  models.CharField( max_length = 70 )   salary = models.IntegerField()   location = models.CharField( max_length = 70 )   dec =  models.TextField()   photo = models.ImageField( upload_to = "myimage" ,  default = '' )    Forms.py # Create your models here. from  django  import  forms from  .models  import  User class  CustomarForm( forms . ModelForm ):    class  Meta:     model = User     fields  =  [ 'cname' , 'jtitle' , 'salary' , 'location' , 'dec' , 'photo' ]     labels  =  { 'cname' : 'Company Name' ,  'jtitle' :  'Job...