Wednesday, December 18, 2019

Python Django : Handling Html Forms

Python Django : Handling Html Forms

forms are an important component in web programming. They are used to create a resource in the server when the form is submitted. A form can request differents type of data from the user that can be passed to the server and use later.

In this article, we create an HTML form which requests some data from user and after submitting this form, data are save in database which we can use later.

First, create a basic form template which asks for user some information, In this example we  just stick with basic details like name and age but you can write more ( you should know the basic HTML).



Now look at the below code how to handle this form.




There are lots of details in the above image. Let cover one by one

In the index.html, we create a form which has labels, the input field, and a submit button. two things new in this form, action attribute, and {% csrf_token %}.
action attribute in form is just like href in an anchor tag, which contains the path/url that is followed when form is submitted.


Next is {% csrf_token %}, this one is essential for Cross Site Request Forgery Protection. Django provides many security features out of the box, csrf_token is one of these.


{% csrf_token %} is compulsory whenever you try to submit the form via post method. If you want to exercise just change the method to GET and omit the {% csrf_token %} and try to submit, yes it works, but look at the urlbar it looks like this.



It contains the data which user submitted via the form If details are private like password and userID you should always use POST method instead of GET. To use POST method you need to include {% csrf_token %} in your form.

In the views we are returning just the HttpResponse instead of html page.

Now save the data to the database which user send via form submission and render a page with all user data.




In the above image, we create a model, Myuser with name and age field, In temp function of views.py we make some edit, first check for
request method if not POST then return an error message. ( if someone tries to goes /formSubmit path by typing in browser urlbar then it is a GET request and form won%u2019t be submitted).

All the data which user submit is store in a request.POST dictionary with the key is name attribute of the input field of HTML form 
After extracting name and age from form, we can print and store data in database using Myuser model we have created and return a success HttpResponse.

Now there is one more view userlist which extract all user from the database and pass in list.html via context dictionary. In
urlpatterns we add path for this view which name is userlist.

So here is our list.html, with the use of template language we can iterate and print every element in userlist.



Now if you want you can add link in this page to go to form page and add new user, you should be now able to do this.

Python Django : login, registration and logout

Python Django : login, registration and logout

we are going to explore how to handle user authentication related feature in this article.

Before going further you should know about django template language, form handling, and django ORM, if not go to the previous articles about them.

Our Aim to create three fully working functional page as shown below



Code for the signup and login pages is given below. Both pages contain looks similar except for one email field in signup page and URL for signup in the login page.

If msg is passed to these page in context then they both page show msg first.



Now look at the index page and urls.py below which is quite simple, a user is passed to index and show its username and email associated with the user.



Here for storing users details, we use Django built-in User model and hence first importing this. We also need to import authentication and login as auth_login, logout as auth_logout because we use the function name as login and logout hence it conflicts if we import normally.

One new thing is request.user.is_authenticated that simply returns boolean whether the user is login or not?


Here authenticate function takes three argument request, username and password and tries to search data in User daatabase. If match occurs the return the user else return None.



If a match occurs the auth_login simply store the user in session means now the user is logged In and redirects to the index which returns index page now.

In logout function, first, check that the user is logged in or not. If logged in the auth_logout remove the user from session means logout user. Note that auth_logout only take request as an argument, not user.


So, now only signUp page view is required, look at the below code


After basic checking, extract the data from the form which is sent by the user and check that username is exists or not in User database.

If the username is not present means it is unique then except block code is run and create a new user with create_user function and save to database with user.save(), then log in the user and redirects to index.

If the username exists in database then return signup page with error message

Saturday, October 19, 2019

Facebook login using python Script[Selenium]

Facebook login using python [Script]

What Is Selenium WebDriver?

Selenium WebDriver is an API that allows us to write automated tests for web applications. The automated tests that use Selenium WebDriver are run by using a web browser. In other words, Selenium WebDriver helps us to verify that our application is working as expected when it is used by a real user.

The key features of Selenium WebDriver are:

It supports many common programming languages such as C#, Java, JavaScript, Python, and so on.

It supports all common web browsers.

It supports headless browsers such as HtmlUnit and PhantomJS.

Get pass Function  :  getpass function is used for take password without displaying the window .

#facebook login python script.

from selenium import webdriver # selenium tool for web activtie
from getpass import getpass #To take password without displaying. user = input('Enter your username or email id: ') pwd = getpass('Enter your password : ') driver = webdriver.Chrome() driver.get("https://www.facebook.com/") username_box = driver.find_element_by_id('email') username_box.send_keys(user) password_box = driver.find_element_by_id('pass') . password_box.send_keys(pwd) login_btn = driver.find_element_by_id('u_0_2') login_btn.submit()

 



Saturday, October 12, 2019

Java program to Convert Integer Number to Roman Number.{By Hottest Way).💣👌 Please Check out .


class Main {
public static void main(String[] args) {
int num;
System.out.println("please the number below 5000");
Scanner obj = new Scanner(System.in);
num = obj.nextInt();
String str = String.valueOf(num);
char []arr = str.toCharArray();
int len = str.length();
int count[] = new int[10];
for(int i=0;i<len;i++){
count[i] = (int)arr[i]-48;    }
String ones[] = new String[]{"","I","II","III","IV","V","VI","VII","VIII",
"IX","X"};
String tens[] = new String[]{"","X","XX","XXX","XL","L","LX","LXX","LXXX",
"XC","C"};
String hundred[] = new String[]{"","C","CC","CCC","CD","D","DC","DCC",
"DCCC","CM","M"};

String thousand[] = new String[]{"","M","MM","MMM","MMMM","~V"};
if(len==4 && num<=5000){
String romancounting = thousand[count[0]]+ hundred[count[1]] + tens[count[2]] + ones[count[3]];
System.out.println(romancounting);
}
else if(len==3) {
String romancounting = hundred[count[0]]+
tens[count[1]]+ones[count[2]];
System.out.println(romancounting);  }
else if(len==2){
String romancounting = tens[count[0]]+
ones[count[1]];
System.out.println(romancounting);   }
else     {
System.out.println("Sorrt But number is invalid!
OutOfRange👌");
} } }







Monday, September 16, 2019

Write a java program to compare two strings lexicographicall

Write a java program to compare two strings lexicographically.
Using the compareTo method for comparing the String .
compareTo method is compare the string is based on the unicode value of character .
It return the positive negative and zero value.
if the string are same it return the zero value otherwise it positive or negative value depend on ASCII value of the character .
for example
1st string is s1 = middle
2nd string is s2 = niddle
so it return the value is -1.
bcoz the value of m is 77 and value of n is 78 (ASCII)
so 77 -78 = -1
so the return value is -1 .
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
String s1;
String s2;
s1 = obj.nextLine();
s2 =obj.nextLine();
if(s1.compareTo(s2)==0){
System.out.println("string are equal"); }
else{
System.out.println("not "+s1.compareTo(s2)+" is difference ");
} } }

Output:  
  oneo
  oneof
  not -1 is difference.
                

Saturday, September 14, 2019

Amazing and gorgeous calculator theme.

Calculator Vikas Tomar's DEV Profile

Amazing and gorgeous calculator theme.

% UR x2 1/x
CE C BK /
7 8 9 *
4 5 6 -
1 2 3 +
% 0 . =
Calculator

If You wanna to show the coding of this theme click on this

Monday, September 9, 2019

Turtle (python)

clever Vikas Tomar's DEV Profile
                    import turtle 
                    # give the turtle name;
                    clever = turtle.Turtle()
                    clever.color('green')
                    clever.speed(0)
  
                   def draw_square():
                   for side in range(4):
                   clever.forward(100)
                   clever.right(90)
                   for anothersquare in range(4):
                   clever.forward(50)
                   clever.right(90)
                   
                   draw_square()
                   def lotofsquare():
                   for square in range(30):
    
                   clever.color('green')
                   draw_square()
                   clever.penup()
                   clever.forward(20)
                   clever.left(50)
                   clever.pendown()
                   lotofsquare()

IF WE WANT TO SEE OUTPUT CLICK THE BELOW LINK

Arrays in Solidity Programming Language.

Arrays Solidity supports both generic and byte arrays. It supports both fixed size and dynamic arrays. It also supports multidimensional ...