Monday, September 19, 2022

Git Commands

Git Version 

To check the git version

Git -v

 



 

 Git Clone

To clone the repository, use the following command:

Git clone [url]

 


 

Get into the directory using the following commands:

Cd [folder name]

Git status


 

Now let’s create a new file to add it to our GitHub repo using the following command:

Fsutil file createnew sample.txt 0 [where 0 is the file length]

 



Git Status

By using the next command, we can check which all files are newly created/modified but not yet added into the repo

Git status

*The files marked in red are untracked files which are yet to be added into the git for commit



Git Add

To add the files for commit we need to make use of the following command

Git add filename

Git status



 Git Restore [To remove the files not yet committed but staged]

To remove the files added to the stage but not yet committed use the following commands:

Git restore --staged filename

Git status




Git Commit

To commit the file into the GitHub, use the following command:

Git commit –m ‘your comments go here’



Git Push

To add the file into the main repository, use the following command

Git push



Git Pull

To get the latest changes use the following command

Git pull

 


Git Log

To get the log info use the following command

Git log



 

Git rm [To remove the file]

To remove the files from repo

Git rm filename

Git status



Git commit –m ‘reason to remove’

Git push

 



Git Branch 

To list all the branches

Git branch

 



To create new branch

To create a new branch

Git branch [branch name]

Git branch


 


Git Switch

To switch between branches

Git switch [branch name]

 



Saturday, January 1, 2022

To create your first Django project

Requirements: Python , VSCode [Or any other code editor]

Use the following steps to create a project in Django.

  • Create a folder and open that in VSCode

  • Create virtual environment using the following command

python -m venv env

  • Here venv is the command to create the virtual environment and env is the name of the environment.

  • After that activate the environment by going into the scripts folder and use the activate command

(env):\scripts>activate

  • Once the virtual environment is active (your virtual environment name (env): indicates you are in virtual environment) then install the Django using the following command

pip install django

  • Now we can create our project using the below command

django-admin startproject project-name .

  • The (.) here indicated that it will create manage.py in the root folder else it will create one more folder inside the root with the project-name and add the manage.py file inside it.

Friday, April 17, 2020

How to loop through HTML table columns?

To loop through each table columns follow the below code.

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <br>
    <div class="container">
        <div class="col-12">
            <table class="table table-bordered">
                <tr>
                    <td>1</td>
                    <td>2</td>
                </tr>
                <tr>
                    <td>a</td>
                    <td>b</td>
                </tr>
                <tr>
                    <td>c</td>
                    <td>d</td>
                </tr>
            </table>
        </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"
        integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>

    <script>
        var nrow = $("table")[0].rows.length;
        var ncol = $("table")[0].rows[0].cells.length;

        for (let c = 0c < ncolc++) {

            for (let r = 0r < nrowr++) {

                console.log($($("table")[0].rows[r].cells[c]).text());
            }
        }

    </script>
</body>
</html>

Output:





How to run HTML file from VSCode?

We can easily run the HTML file by using an extention called Live Server by Ritwick Dey.

Note: To run the file it must be inside a directory or else it wont run.

https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer 

Sunday, February 2, 2020

Disable the browser back button

Use this script to disable the browser back button once the user has logged out. So that even if he tries to click the back button and go back to the previous page he will not be able to go.



<script>
    history.pushState(null, null, location.href);
    window.onpopstate = function () {
        history.go(1);
    };
</script>

Forms Authentication in MVC

index.cshtml

@using (Html.BeginForm("index","home",FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <span>Enter Name</span>
    <input type="text" name="name"  required />

    <input type="submit"  value="Submit" />

}

HomeController.cs
        

        using System.Web.Security;

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(FormCollection formCollection)
        {

            if (ModelState.IsValid)
            {
                  var authTicket = new FormsAuthenticationTicket(
                    1,// version
                    formCollection["name"].ToString(), // user name
                    DateTime.Now, // created
                    DateTime.Now.AddMinutes(20), // expires
                    false// persistent?
                    "User"  // can be used to store roles
                    );

                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);         

                return RedirectToAction("about");

            }
            else
            {
                return View();
            }
        }

      
 [Authorize(Roles ="User")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

       
[Authorize]
        public ActionResult logout()
        {

            FormsAuthentication.SignOut();
            return RedirectToAction("index");      
       
        }


Inside the Global.asax.cs


using System.Security.Principal;
using System.Web.Security;

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie == null || authCookie.Value == "")
                return;

            FormsAuthenticationTicket authTicket;

            try
            {
                authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            }
            catch
            {
                return;
            }

            // retrieve roles from UserData
            string[] roles = authTicket.UserData.Split(';');

            if (Context.User != null)
                Context.User = new GenericPrincipal(Context.User.Identity, roles);

        }




Inside the web.config


  <authentication mode="Forms" >
    <forms defaultUrl="home/index" loginUrl="home/index"  protection="All"></forms>
  </authentication>

  <authorization>
    <allow users="*"/>
    <deny users="?"/>
  </authorization>

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...